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

相关文章

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

Python中的getopt模块用法小结

《Python中的getopt模块用法小结》getopt.getopt()函数是Python中用于解析命令行参数的标准库函数,该函数可以从命令行中提取选项和参数,并对它们进行处理,本文详细介绍了Pyt... 目录getopt模块介绍getopt.getopt函数的介绍getopt模块的常用用法getopt模

HTML5中的Microdata与历史记录管理详解

《HTML5中的Microdata与历史记录管理详解》Microdata作为HTML5新增的一个特性,它允许开发者在HTML文档中添加更多的语义信息,以便于搜索引擎和浏览器更好地理解页面内容,本文将探... 目录html5中的Mijscrodata与历史记录管理背景简介html5中的Microdata使用M

html5的响应式布局的方法示例详解

《html5的响应式布局的方法示例详解》:本文主要介绍了HTML5中使用媒体查询和Flexbox进行响应式布局的方法,简要介绍了CSSGrid布局的基础知识和如何实现自动换行的网格布局,详细内容请阅读本文,希望能对你有所帮助... 一 使用媒体查询响应式布局        使用的参数@media这是常用的

HTML5表格语法格式详解

《HTML5表格语法格式详解》在HTML语法中,表格主要通过table、tr和td3个标签构成,本文通过实例代码讲解HTML5表格语法格式,感兴趣的朋友一起看看吧... 目录一、表格1.表格语法格式2.表格属性 3.例子二、不规则表格1.跨行2.跨列3.例子一、表格在html语法中,表格主要通过< tab

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

python logging模块详解及其日志定时清理方式

《pythonlogging模块详解及其日志定时清理方式》:本文主要介绍pythonlogging模块详解及其日志定时清理方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录python logging模块及日志定时清理1.创建logger对象2.logging.basicCo

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行