-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.tsx
195 lines (160 loc) · 5.72 KB
/
App.tsx
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import React from "react";
import { StyleSheet, Text, View } from "react-native";
import RNSimpleCrypto from "react-native-simple-crypto";
async function Test() {
const toHex = RNSimpleCrypto.utils.convertArrayBufferToHex;
const toUtf8 = RNSimpleCrypto.utils.convertArrayBufferToUtf8;
// -- AES ------------------------------------------------------------- //
const message = "data to encrypt";
const messageArrayBuffer = RNSimpleCrypto.utils.convertUtf8ToArrayBuffer(
message
);
const keyArrayBuffer = await RNSimpleCrypto.utils.randomBytes(32);
console.log("randomBytes key", toHex(keyArrayBuffer));
const ivArrayBuffer = await RNSimpleCrypto.utils.randomBytes(16);
console.log("randomBytes iv", toHex(ivArrayBuffer));
const cipherTextArrayBuffer = await RNSimpleCrypto.AES.encrypt(
messageArrayBuffer,
keyArrayBuffer,
ivArrayBuffer
);
console.log("AES encrypt", toHex(cipherTextArrayBuffer));
const decryptedArrayBuffer = await RNSimpleCrypto.AES.decrypt(
cipherTextArrayBuffer,
keyArrayBuffer,
ivArrayBuffer
);
console.log("AES decrypt", toUtf8(decryptedArrayBuffer));
if (toUtf8(decryptedArrayBuffer) !== message) {
console.error("AES decrypt returned unexpected results");
}
// -- HMAC ------------------------------------------------------------ //
const keyHmac = await RNSimpleCrypto.utils.randomBytes(32);
const signatureArrayBuffer = await RNSimpleCrypto.HMAC.hmac256(messageArrayBuffer, keyHmac);
console.log("HMAC signature", toHex(signatureArrayBuffer));
// -- SHA ------------------------------------------------------------- //
const sha1Hash = await RNSimpleCrypto.SHA.sha1("test");
console.log("SHA1 hash", sha1Hash);
const sha256Hash = await RNSimpleCrypto.SHA.sha256("test");
console.log("SHA256 hash", sha256Hash);
const sha512Hash = await RNSimpleCrypto.SHA.sha512("test");
console.log("SHA512 hash", sha512Hash);
const arrayBufferToHash = RNSimpleCrypto.utils.convertUtf8ToArrayBuffer("test");
const sha1ArrayBuffer = await RNSimpleCrypto.SHA.sha1(arrayBufferToHash);
console.log("SHA1 hash bytes", toHex(sha1ArrayBuffer));
if (toHex(sha1ArrayBuffer) !== sha1Hash) {
console.error("SHA1 result mismatch!");
}
const sha256ArrayBuffer = await RNSimpleCrypto.SHA.sha256(arrayBufferToHash);
console.log("SHA256 hash bytes", toHex(sha256ArrayBuffer));
if (toHex(sha256ArrayBuffer) !== sha256Hash) {
console.error("SHA256 result mismatch!");
}
const sha512ArrayBuffer = await RNSimpleCrypto.SHA.sha512(arrayBufferToHash);
console.log("SHA512 hash bytes", toHex(sha512ArrayBuffer));
if (toHex(sha512ArrayBuffer) !== sha512Hash) {
console.error("SHA512 result mismatch!");
}
// -- PBKDF2 ---------------------------------------------------------- //
const password = "secret password";
const salt = "my-salt";
const iterations = 4096;
const keyInBytes = 32;
const hash = "SHA1";
const passwordKey = await RNSimpleCrypto.PBKDF2.hash(
password,
salt,
iterations,
keyInBytes,
hash
);
console.log("PBKDF2 passwordKey", toHex(passwordKey));
const passwordKeyArrayBuffer = await RNSimpleCrypto.PBKDF2.hash(
RNSimpleCrypto.utils.convertUtf8ToArrayBuffer(password),
RNSimpleCrypto.utils.convertUtf8ToArrayBuffer(salt),
iterations,
keyInBytes,
hash
);
console.log("PBKDF2 passwordKey bytes", toHex(passwordKeyArrayBuffer));
if (toHex(passwordKeyArrayBuffer) !== toHex(passwordKey)) {
console.error("PBKDF2 result mismatch!");
}
const password2 = messageArrayBuffer;
const salt2 = await RNSimpleCrypto.utils.randomBytes(8);
const iterations2 = 10000;
const keyInBytes2 = 32;
const hash2 = "SHA256";
const passwordKey2 = await RNSimpleCrypto.PBKDF2.hash(
password2,
salt2,
iterations2,
keyInBytes2,
hash2
);
console.log("PBKDF2 passwordKey2", toHex(passwordKey2));
// -- RSA ------------------------------------------------------------ //
const rsaKeys = await RNSimpleCrypto.RSA.generateKeys(1024);
console.log("RSA1024 private key", rsaKeys.private);
console.log("RSA1024 public key", rsaKeys.public);
// UTF-8
const rsaEncryptedMessage = await RNSimpleCrypto.RSA.encrypt(
message,
rsaKeys.public
);
console.log("rsa Encrypt:", rsaEncryptedMessage);
const rsaSignature = await RNSimpleCrypto.RSA.sign(
rsaEncryptedMessage,
rsaKeys.private,
"SHA256"
);
console.log("rsa Signature:", rsaSignature);
const validSignature = await RNSimpleCrypto.RSA.verify(
rsaSignature,
rsaEncryptedMessage,
rsaKeys.public,
"SHA256"
);
console.log("rsa signature verified:", validSignature);
// UTF-8
const rsaDecryptedMessage = await RNSimpleCrypto.RSA.decrypt(
rsaEncryptedMessage,
rsaKeys.private
);
console.log("rsa Decrypt:", rsaDecryptedMessage);
if (rsaDecryptedMessage !== message) {
console.error("RSA decrypt returned unexpected result");
}
// Base64
const binaryData = "Zm9vYmFy";
const rsaEncryptedMessage64 = await RNSimpleCrypto.RSA.encrypt64(
binaryData,
rsaKeys.public
);
console.log("rsa Encrypt 64:", rsaEncryptedMessage64);
// Base 64
const rsaDecryptedMessage64 = await RNSimpleCrypto.RSA.decrypt64(
rsaEncryptedMessage64,
rsaKeys.private
);
console.log("rsa Decrypt 64:", rsaDecryptedMessage64);
if (rsaDecryptedMessage64 !== binaryData) {
console.error("RSA 64 decrypt returned unexpected result");
}
}
export default function App() {
void Test().catch(console.error);
return (
<View style={styles.container}>
<Text>Check the console for debug output</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});