react-native-aes-lite 1.0.1 → 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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/keygen.js +16 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-aes-lite",
3
- "version": "1.0.1",
3
+ "version": "1.0.2",
4
4
  "description": "AES encryption for React Native",
5
5
  "main": "src/index.js",
6
6
  "keywords": [
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
- if (global.crypto?.getRandomValues) {
10
+ // RN + Expo + Node secure RNG
11
+ if (globalThis.crypto?.getRandomValues) {
11
12
  crypto.getRandomValues(bytes);
12
13
  } else {
13
- throw new Error("Secure random generator not available");
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) => Buffer.from(buf).toString("base64");
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,15 +33,16 @@ 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("Invalid format");
45
+ throw new Error("Format must be 'hex', 'base64', or 'ascii'");
36
46
  }
37
47
  }
38
48