forked from ghbutton/react-native-simple-crypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
168 lines (150 loc) · 4.55 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
158
159
160
161
162
163
164
165
166
167
168
"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;
}
}
const utf8 = chars.join("");
return utf8;
}
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);
const arrayBuffer = array.buffer;
return arrayBuffer;
}
function convertArrayBufferToBase64(arrayBuffer) {
const byteArray = new Uint8Array(arrayBuffer);
const result = base64js.fromByteArray(byteArray);
return result;
}
function convertBase64ToArrayBuffer(base64) {
const result = base64js.toByteArray(base64).buffer;
return result;
}
function convertArrayBufferToHex(arrayBuffer) {
const result = hexLite.fromBuffer(arrayBuffer);
return result;
}
function convertHexToArrayBuffer(hex) {
const result = hexLite.toBuffer(hex);
return result;
}
function randomBytes(length) {
return new Promise((resolve, reject) => {
NativeModules.RNRandomBytes.randomBytes(length, function(err, base64) {
if (err) {
reject(err);
} else {
const result = convertBase64ToArrayBuffer(base64);
resolve(result);
}
});
});
}
export const AES = {
encrypt: function(textArrayBuffer, keyArrayBuffer, ivArrayBuffer) {
const textString = convertArrayBufferToUtf8(textArrayBuffer);
const keyHex = convertArrayBufferToHex(keyArrayBuffer);
const ivHex = convertArrayBufferToHex(ivArrayBuffer);
return new Promise((resolve, reject) => {
NativeModules.Aes.encrypt(textString, keyHex, ivHex)
.then(cipherTextBase64 => {
const result = convertBase64ToArrayBuffer(cipherTextBase64);
resolve(result);
})
.catch(error => reject(error));
});
},
decrypt: function(cipherTextArrayBuffer, keyArrayBuffer, ivArrayBuffer) {
const cipherTextBase64 = convertArrayBufferToBase64(cipherTextArrayBuffer);
const keyHex = convertArrayBufferToHex(keyArrayBuffer);
const ivHex = convertArrayBufferToHex(ivArrayBuffer);
return new Promise((resolve, reject) => {
NativeModules.Aes.decrypt(cipherTextBase64, keyHex, ivHex)
.then(textString => {
const result = convertUtf8ToArrayBuffer(textString);
resolve(result);
})
.catch(error => reject(error));
});
}
};
export const SHA = NativeModules.Sha;
export const HMAC = {
hmac256: function(textArrayBuffer, keyArrayBuffer) {
const textHex = convertArrayBufferToHex(textArrayBuffer);
const keyHex = convertArrayBufferToHex(keyArrayBuffer);
return new Promise((resolve, reject) => {
NativeModules.Hmac.hmac256(textHex, keyHex)
.then(signatureHex => {
const result = convertHexToArrayBuffer(signatureHex);
resolve(result);
})
.catch(error => reject(error));
});
}
};
export const PBKDF2 = {
hash: function(password, saltArrayBuffer, iterations, keyLength, hash) {
const saltBase64 = convertArrayBufferToBase64(saltArrayBuffer);
return new Promise((resolve, reject) => {
NativeModules.Pbkdf2.hash(
password,
saltBase64,
iterations,
keyLength,
hash
)
.then(hashHex => {
const result = convertHexToArrayBuffer(hashHex);
resolve(result);
})
.catch(error => reject(error));
});
}
};
export const RSA = NativeModules.Rsa;
export const utils = {
randomBytes: randomBytes,
convertArrayBufferToUtf8: convertArrayBufferToUtf8,
convertUtf8ToArrayBuffer: convertUtf8ToArrayBuffer,
convertArrayBufferToBase64: convertArrayBufferToBase64,
convertBase64ToArrayBuffer: convertBase64ToArrayBuffer,
convertArrayBufferToHex: convertArrayBufferToHex,
convertHexToArrayBuffer: convertHexToArrayBuffer
};