react-native-aes-lite 1.0.0 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +9 -12
- package/src/index.js +3 -3
- package/src/keygen.js +17 -7
package/package.json
CHANGED
|
@@ -1,18 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-aes-lite",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "AES encryption for React Native",
|
|
5
5
|
"main": "src/index.js",
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
"
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
"keywords": [
|
|
7
|
+
"aes",
|
|
8
|
+
"encryption",
|
|
9
|
+
"react-native",
|
|
10
|
+
"key",
|
|
11
|
+
"decryption"
|
|
12
|
+
],
|
|
12
13
|
"author": "Ammachi",
|
|
13
|
-
"license": "MIT"
|
|
14
|
-
"devDependencies": {
|
|
15
|
-
"@types/node": "^25.0.3",
|
|
16
|
-
"typescript": "^5.9.3"
|
|
17
|
-
}
|
|
14
|
+
"license": "MIT"
|
|
18
15
|
}
|
package/src/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
import AES from "./AES";
|
|
2
|
+
import generateKey from "./keygen";
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
export { AES, generateKey };
|
package/src/keygen.js
CHANGED
|
@@ -2,21 +2,30 @@ function generateKey(format = "hex", options = {}) {
|
|
|
2
2
|
const { length = 16, readable = false } = options;
|
|
3
3
|
|
|
4
4
|
if (![16, 24, 32].includes(length)) {
|
|
5
|
-
throw new Error("Key length must be 16, 24, or 32 bytes");
|
|
5
|
+
throw new Error("Key length must be 16, 24, or 32 bytes (AES-128/192/256)");
|
|
6
6
|
}
|
|
7
7
|
|
|
8
8
|
const bytes = new Uint8Array(length);
|
|
9
9
|
|
|
10
|
-
|
|
10
|
+
// RN + Expo + Node secure RNG
|
|
11
|
+
if (globalThis.crypto?.getRandomValues) {
|
|
11
12
|
crypto.getRandomValues(bytes);
|
|
12
13
|
} else {
|
|
13
|
-
|
|
14
|
+
// very old RN fallback using Math.random (not cryptographically strong)
|
|
15
|
+
for (let i = 0; i < length; i++) {
|
|
16
|
+
bytes[i] = Math.floor(Math.random() * 256);
|
|
17
|
+
}
|
|
14
18
|
}
|
|
15
19
|
|
|
16
20
|
const toHex = (buf) =>
|
|
17
21
|
Array.from(buf, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
18
22
|
|
|
19
|
-
const toBase64 = (buf) =>
|
|
23
|
+
const toBase64 = (buf) => {
|
|
24
|
+
if (typeof Buffer !== "undefined") {
|
|
25
|
+
return Buffer.from(buf).toString("base64");
|
|
26
|
+
}
|
|
27
|
+
return btoa(String.fromCharCode(...buf));
|
|
28
|
+
};
|
|
20
29
|
|
|
21
30
|
const toReadable = (buf) => {
|
|
22
31
|
const chars =
|
|
@@ -24,16 +33,17 @@ function generateKey(format = "hex", options = {}) {
|
|
|
24
33
|
return Array.from(buf, (b) => chars[b % chars.length]).join("");
|
|
25
34
|
};
|
|
26
35
|
|
|
27
|
-
switch (format) {
|
|
36
|
+
switch (format.toLowerCase()) {
|
|
28
37
|
case "hex":
|
|
29
38
|
return toHex(bytes);
|
|
30
39
|
case "base64":
|
|
31
40
|
return toBase64(bytes);
|
|
32
41
|
case "ascii":
|
|
42
|
+
case "string":
|
|
33
43
|
return readable ? toReadable(bytes) : String.fromCharCode(...bytes);
|
|
34
44
|
default:
|
|
35
|
-
throw new Error("
|
|
45
|
+
throw new Error("Format must be 'hex', 'base64', or 'ascii'");
|
|
36
46
|
}
|
|
37
47
|
}
|
|
38
48
|
|
|
39
|
-
|
|
49
|
+
export default generateKey;
|