React Native开源封装AES,MD5加密模块(react-native-encryption-library)

本文主要是介绍React Native开源封装AES,MD5加密模块(react-native-encryption-library),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

本文来自:江清清的技术专栏(http://www.lcode.org)

开源项目地址:https://github.com/jiangqqlmj/react-native-encryption-library

项目介绍

昨天自己封装了常用的加密方式例如:MD5,AES加密,供React Native进行使用,不过当前项目适配Android平台。

刚创建的React Native交流5群:386216878,欢迎各位大牛,React Native技术爱好者加入交流!

安装配置
?
1
npm install react-native-encryption-library --save

In android/setting.gradle

?
1
2
3
...
include ':react-native-encryption-library'
project( ':react-native-encryption-library' ).projectDir = new File(rootProject.projectDir, '../node_modules/react-native-encryption-library/android' )

In android/app/build.gradle

?
1
2
3
4
5
...
dependencies {
     ...
     compile project( ':react-native-encryption-library' )
}

register module (in MainActivity.java)

On newer versions of React Native (0.18+):

?
1
2
3
4
5
6
7
8
9
10
11
import com.chinaztt.encapsulation.EncryptionReactPackager;;  // <--- import
public class MainActivity extends ReactActivity {
   ......
     @Override
     protected List<ReactPackage> getPackages() {
       return Arrays.<ReactPackage>asList(
          new EncryptionReactPackager(), // <------ add here
         new MainReactPackage());
     }
}

On older versions of React Native:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import com.chinaztt.encapsulation.EncryptionReactPackager;;  // <--- import
public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
   ......
   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super .onCreate(savedInstanceState);
     mReactRootView = new ReactRootView( this );
     mReactInstanceManager = ReactInstanceManager.builder()
       .setApplication(getApplication())
       .setBundleAssetName( "index.android.bundle" )
       .setJSMainModuleName( "index.android" )
       .addPackage( new MainReactPackage())
       .addPackage( new EncryptionReactPackager())              // <------ add here
       .setUseDeveloperSupport(BuildConfig.DEBUG)
       .setInitialLifecycleState(LifecycleState.RESUMED)
       .build();
     mReactRootView.startReactApplication(mReactInstanceManager, "ExampleRN" , null );
     setContentView(mReactRootView);
   }
   ......
}
导入模块
?
1
2
import {NativeModules} from 'react-native' ;
var EncryptionModule=NativeModules.EncryptionModule
使用实例
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import React, { Component } from 'react' ;
import {
   AppRegistry,
   StyleSheet,
   Text,
   View,
   TouchableHighlight
} from 'react-native' ;
import {NativeModules} from 'react-native' ;
var EncryptionModule=NativeModules.EncryptionModule
//待加密的信息
var PASSWORD= '745r#x3g' ;
var KEY= 'wIEuw3kAGwVNl7BW' //16位AES加密私钥
class CustomButton extends React.Component {
   render() {
     return (
       <TouchableHighlight
         style={styles.button}
         underlayColor= "#a5a5a5"
         onPress={ this .props.onPress}>
         <Text style={styles.buttonText}>{ this .props.text}</Text>
       </TouchableHighlight>
     );
   }
}
class react_native_encryption_library extends Component {
   constructor(props){
      super (props);
      this .state={
         result: '' ,
         AES_Result: '' ,
      }
   }
   async _MD5ByPromise(){
      try {
         var result=await EncryptionModule.MD5ByPromise(PASSWORD);
         this .setState({result: 'Promise:' +result});
      } catch (e){
         this .setState({result: 'MD5加密失败-通过Promise回调' });
      }
   }
   async _AESEncryptByPromise(){
      try {
         var result=await EncryptionModule.AESEncryptByPromise(PASSWORD,KEY);
         this .setState({AES_Result:result});
      } catch (e){
         this .setState({AES_Result: 'AES加密失败-通过Promise回调' });
      }
   }
   async _AESDecryptByPromise(){
      try {
         var result=await EncryptionModule.AESDecryptByPromise( this .state.AES_Result,KEY);
         this .setState({AES_Result:result});
      } catch (e){
         this .setState({AES_Result: 'AES解密失败-通过Promise回调' });
      }
   }
   render() {
     return (
       <View style={styles.container}>
         <Text style={styles.welcome}>
            加密模块封装实例-Android端
         </Text>
         <Text style={{margin:10,fontSize:12}}>
            结果:{ this .state.result}
         </Text>
         <CustomButton
            text= "测试MD5加密封装-CallBack回调"
            onPress={()=>EncryptionModule.MD5ByCallBack(PASSWORD,(msg)=>{
                this .setState({result: 'CallBack:' +msg});
           },(error)=>{
                this .setState({result: 'MD5加密失败-通过Callback回调' });  
           })}
         />
         <CustomButton
            text= "测试MD5加密封装-Promise回调"
            onPress={()=> this ._MD5ByPromise()}
         />
          <Text style={{margin:10,fontSize:12}}>
            AES结果:{ this .state.AES_Result}
         </Text>
         <CustomButton
            text= "测试AES加密封装-CallBack回调"
            onPress={()=>EncryptionModule.AESEncryptByCallBack(PASSWORD,KEY,(msg)=>{
                this .setState({AES_Result:msg});
           },(error)=>{
                this .setState({AES_Result: 'AES加密失败-通过Callback回调' });  
           })}
         />
         <CustomButton
            text= "测试AES加密封装-Promise回调"
            onPress={()=> this ._AESEncryptByPromise()}
         />
         <CustomButton
            text= "测试AES解密封装-CallBack回调"
            onPress={()=>EncryptionModule.AESDecryptByCallBack( this .state.AES_Result,KEY,(msg)=>{
                this .setState({AES_Result:msg});
           },(error)=>{
                this .setState({AES_Result: 'AES解密失败-通过Callback回调' });  
           })}
         />
         <CustomButton
            text= "测试AES解密封装-Promise回调"
            onPress={()=> this ._AESDecryptByPromise()}
         />
       </View>
     );
   }
}
const styles = StyleSheet.create({
   welcome: {
     fontSize: 20,
     textAlign: 'center' ,
     margin: 10,
   },
   button: {
     margin:5,
     backgroundColor: 'white' ,
     padding: 15,
     borderBottomWidth: StyleSheet.hairlineWidth,
     borderBottomColor: '#cdcdcd' ,
   },
});
AppRegistry.registerComponent( 'encryption_library' , () => react_native_encryption_library);
运行效果

这篇关于React Native开源封装AES,MD5加密模块(react-native-encryption-library)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/510104

相关文章

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

python实现对数据公钥加密与私钥解密

《python实现对数据公钥加密与私钥解密》这篇文章主要为大家详细介绍了如何使用python实现对数据公钥加密与私钥解密,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录公钥私钥的生成使用公钥加密使用私钥解密公钥私钥的生成这一部分,使用python生成公钥与私钥,然后保存在两个文

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

前端如何通过nginx访问本地端口

《前端如何通过nginx访问本地端口》:本文主要介绍前端如何通过nginx访问本地端口的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、nginx安装1、下载(1)下载地址(2)系统选择(3)版本选择2、安装部署(1)解压(2)配置文件修改(3)启动(4)

HTML中meta标签的常见使用案例(示例详解)

《HTML中meta标签的常见使用案例(示例详解)》HTMLmeta标签用于提供文档元数据,涵盖字符编码、SEO优化、社交媒体集成、移动设备适配、浏览器控制及安全隐私设置,优化页面显示与搜索引擎索引... 目录html中meta标签的常见使用案例一、基础功能二、搜索引擎优化(seo)三、社交媒体集成四、移动

HTML input 标签示例详解

《HTMLinput标签示例详解》input标签主要用于接收用户的输入,随type属性值的不同,变换其具体功能,本文通过实例图文并茂的形式给大家介绍HTMLinput标签,感兴趣的朋友一... 目录通用属性输入框单行文本输入框 text密码输入框 password数字输入框 number电子邮件输入编程框

HTML img标签和超链接标签详细介绍

《HTMLimg标签和超链接标签详细介绍》:本文主要介绍了HTML中img标签的使用,包括src属性(指定图片路径)、相对/绝对路径区别、alt替代文本、title提示、宽高控制及边框设置等,详细内容请阅读本文,希望能对你有所帮助... 目录img 标签src 属性alt 属性title 属性width/h

CSS3打造的现代交互式登录界面详细实现过程

《CSS3打造的现代交互式登录界面详细实现过程》本文介绍CSS3和jQuery在登录界面设计中的应用,涵盖动画、选择器、自定义字体及盒模型技术,提升界面美观与交互性,同时优化性能和可访问性,感兴趣的朋... 目录1. css3用户登录界面设计概述1.1 用户界面设计的重要性1.2 CSS3的新特性与优势1.

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF