forked from trackforce/react-native-crypto
-
Notifications
You must be signed in to change notification settings - Fork 25
/
index.js
157 lines (132 loc) · 4.05 KB
/
index.js
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
'use strict';
import base64js from 'base64-js';
import hexLite from 'hex-lite';
import { NativeModules } from 'react-native';
function convertArrayBufferToUtf8(arrayBuffer) {
const array = new Uint8Array(arrayBuffer);
const chars = [];
let i = 0;
while (i < array.length) {
const byte = array[i];
if (byte < 128) {
chars.push(String.fromCharCode(byte));
i++;
} else if (byte > 191 && byte < 224) {
chars.push(
String.fromCharCode(((byte & 0x1f) << 6) | (array[i + 1] & 0x3f))
);
i += 2;
} else {
chars.push(
String.fromCharCode(
((byte & 0x0f) << 12) |
((array[i + 1] & 0x3f) << 6) |
(array[i + 2] & 0x3f)
)
);
i += 3;
}
}
return chars.join('');
}
function convertUtf8ToArrayBuffer(utf8) {
const bytes = [];
let i = 0;
utf8 = encodeURI(utf8);
while (i < utf8.length) {
const byte = utf8.charCodeAt(i++);
if (byte === 37) {
bytes.push(parseInt(utf8.substr(i, 2), 16));
i += 2;
} else {
bytes.push(byte);
}
}
const array = new Uint8Array(bytes);
return array.buffer;
}
function convertArrayBufferToBase64(arrayBuffer) {
return base64js.fromByteArray(new Uint8Array(arrayBuffer));
}
function convertBase64ToArrayBuffer(base64) {
return base64js.toByteArray(base64).buffer;
}
const convertArrayBufferToHex = hexLite.fromBuffer;
const convertHexToArrayBuffer = hexLite.toBuffer;
async function randomBytes(length) {
return convertBase64ToArrayBuffer(await NativeModules.RNSCRandomBytes.randomBytes(length));
}
async function SHAWrapper(data, algorithm) {
if (typeof data === 'string') {
return NativeModules.RNSCSha.shaUtf8(data, algorithm);
} else {
const dataBase64 = convertArrayBufferToBase64(data);
const result = await NativeModules.RNSCSha.shaBase64(dataBase64, algorithm);
return convertBase64ToArrayBuffer(result);
}
}
const AES = {
encrypt: async function (textArrayBuffer, keyArrayBuffer, ivArrayBuffer) {
const textBase64 = convertArrayBufferToBase64(textArrayBuffer);
const keyHex = convertArrayBufferToHex(keyArrayBuffer);
const ivHex = convertArrayBufferToHex(ivArrayBuffer);
return convertBase64ToArrayBuffer(await NativeModules.RNSCAes.encrypt(textBase64, keyHex, ivHex));
},
decrypt: async function (cipherTextArrayBuffer, keyArrayBuffer, ivArrayBuffer) {
const cipherTextBase64 = convertArrayBufferToBase64(cipherTextArrayBuffer);
const keyHex = convertArrayBufferToHex(keyArrayBuffer);
const ivHex = convertArrayBufferToHex(ivArrayBuffer);
return convertBase64ToArrayBuffer(await NativeModules.RNSCAes.decrypt(cipherTextBase64, keyHex, ivHex));
}
};
const SHA = {
sha1: data => SHAWrapper(data, 'SHA-1'),
sha256: data => SHAWrapper(data, 'SHA-256'),
sha512: data => SHAWrapper(data, 'SHA-512')
};
const HMAC = {
hmac256: async function (textArrayBuffer, keyArrayBuffer) {
const textHex = convertArrayBufferToHex(textArrayBuffer);
const keyHex = convertArrayBufferToHex(keyArrayBuffer);
const signatureHex = await NativeModules.RNSCHmac.hmac256(textHex, keyHex);
return convertHexToArrayBuffer(signatureHex);
}
};
const PBKDF2 = {
hash: async function (password, salt, iterations, keyLength, algorithm) {
let passwordToHash = password;
let saltToHash = salt;
if (typeof password === 'string') {
passwordToHash = convertUtf8ToArrayBuffer(password);
}
if (typeof salt === 'string') {
saltToHash = convertUtf8ToArrayBuffer(salt);
}
const digest = await NativeModules.RNSCPbkdf2.hash(
convertArrayBufferToBase64(passwordToHash),
convertArrayBufferToBase64(saltToHash),
iterations,
keyLength,
algorithm
);
return convertBase64ToArrayBuffer(digest);
}
};
const RSA = NativeModules.RNSCRsa;
const utils = {
randomBytes,
convertArrayBufferToUtf8,
convertUtf8ToArrayBuffer,
convertArrayBufferToBase64,
convertBase64ToArrayBuffer,
convertArrayBufferToHex,
convertHexToArrayBuffer
};
export default {
AES,
SHA,
HMAC,
PBKDF2,
RSA,
utils
};