react-native-aes-lite 1.0.3 → 1.0.5

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 CHANGED
@@ -1,32 +1,21 @@
1
1
  {
2
2
  "name": "react-native-aes-lite",
3
- "version": "1.0.3",
3
+ "version": "1.0.5",
4
4
  "description": "AES encryption for React Native + Expo Web",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
7
- "react-native": "src/index.js",
8
- "browser": "dist/index.js",
9
-
7
+ "react-native": "dist/index.js",
8
+ "types": "dist/index.d.ts",
10
9
  "files": [
11
- "dist",
12
- "src"
10
+ "dist"
13
11
  ],
14
-
15
12
  "keywords": [
16
13
  "aes",
17
14
  "encryption",
18
15
  "react-native",
19
16
  "expo",
20
- "web",
21
- "key",
22
- "crypto",
23
- "aes128",
24
- "aes256"
17
+ "web"
25
18
  ],
26
19
  "author": "Ammachi",
27
- "license": "MIT",
28
-
29
- "scripts": {
30
- "build": "mkdir -p dist && cp src/*.js dist/"
31
- }
20
+ "license": "MIT"
32
21
  }
package/src/AES.js DELETED
@@ -1,296 +0,0 @@
1
- class AES {
2
- constructor() {
3
- this.SBOX = [
4
- 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b,
5
- 0xfe, 0xd7, 0xab, 0x76, 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0,
6
- 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0, 0xb7, 0xfd, 0x93, 0x26,
7
- 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
8
- 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2,
9
- 0xeb, 0x27, 0xb2, 0x75, 0x09, 0x83, 0x2c, 0x1a, 0x1b,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x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f,
25
- 0xb0, 0x54, 0xbb, 0x16,
26
- ];
27
- this.INV_SBOX = new Array(256);
28
- for (let i = 0; i < 256; i++) this.INV_SBOX[this.SBOX[i]] = i;
29
- this.RCON = [
30
- 0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36,
31
- ];
32
- }
33
-
34
- xtime(x) {
35
- x &= 0xff;
36
- return ((x << 1) ^ (((x >> 7) & 1) * 0x1b)) & 0xff;
37
- }
38
-
39
- parseKey(keyString) {
40
- if (keyString.length === 32) {
41
- // Assume hex
42
- const bytes = [];
43
- for (let i = 0; i < 32; i += 2) {
44
- bytes.push(parseInt(keyString.substr(i, 2), 16));
45
- }
46
- if (bytes.some(isNaN)) throw new Error("Invalid hex key");
47
- return bytes;
48
- } else if (keyString.length === 16) {
49
- // ASCII
50
- return keyString.split("").map((c) => c.charCodeAt(0));
51
- } else {
52
- throw new Error("Key must be 16 ASCII chars or 32 hex chars");
53
- }
54
- }
55
-
56
- keyExpansion(key) {
57
- const w = new Array(44); // 4 * (10 + 1)
58
- for (let i = 0; i < 4; i++) {
59
- w[i] = [key[4 * i], key[4 * i + 1], key[4 * i + 2], key[4 * i + 3]];
60
- }
61
- for (let i = 4; i < 44; i++) {
62
- let temp = w[i - 1].slice();
63
- if (i % 4 === 0) {
64
- // RotWord
65
- const t = temp[0];
66
- temp[0] = temp[1];
67
- temp[1] = temp[2];
68
- temp[2] = temp[3];
69
- temp[3] = t;
70
- // SubWord
71
- for (let j = 0; j < 4; j++) {
72
- temp[j] = this.SBOX[temp[j]];
73
- }
74
- // Rcon
75
- temp[0] ^= this.RCON[i / 4];
76
- }
77
- w[i] = [
78
- w[i - 4][0] ^ temp[0],
79
- w[i - 4][1] ^ temp[1],
80
- w[i - 4][2] ^ temp[2],
81
- w[i - 4][3] ^ temp[3],
82
- ];
83
- }
84
- return w;
85
- }
86
-
87
- cipher(input, w) {
88
- let state = [
89
- [0, 0, 0, 0],
90
- [0, 0, 0, 0],
91
- [0, 0, 0, 0],
92
- [0, 0, 0, 0],
93
- ];
94
- for (let r = 0; r < 4; r++)
95
- for (let c = 0; c < 4; c++) state[r][c] = input[r + 4 * c];
96
-
97
- state = this.addRoundKey(state, w, 0);
98
-
99
- for (let round = 1; round < 10; round++) {
100
- state = this.subBytes(state);
101
- state = this.shiftRows(state);
102
- state = this.mixColumns(state);
103
- state = this.addRoundKey(state, w, round);
104
- }
105
-
106
- state = this.subBytes(state);
107
- state = this.shiftRows(state);
108
- state = this.addRoundKey(state, w, 10);
109
-
110
- const output = new Array(16);
111
- for (let r = 0; r < 4; r++)
112
- for (let c = 0; c < 4; c++) output[r + 4 * c] = state[r][c];
113
- return output;
114
- }
115
-
116
- invCipher(input, w) {
117
- let state = [
118
- [0, 0, 0, 0],
119
- [0, 0, 0, 0],
120
- [0, 0, 0, 0],
121
- [0, 0, 0, 0],
122
- ];
123
- for (let r = 0; r < 4; r++)
124
- for (let c = 0; c < 4; c++) state[r][c] = input[r + 4 * c];
125
-
126
- state = this.addRoundKey(state, w, 10);
127
-
128
- for (let round = 9; round >= 1; round--) {
129
- state = this.invShiftRows(state);
130
- state = this.invSubBytes(state);
131
- state = this.addRoundKey(state, w, round);
132
- state = this.invMixColumns(state);
133
- }
134
-
135
- state = this.invShiftRows(state);
136
- state = this.invSubBytes(state);
137
- state = this.addRoundKey(state, w, 0);
138
-
139
- const output = new Array(16);
140
- for (let r = 0; r < 4; r++)
141
- for (let c = 0; c < 4; c++) output[r + 4 * c] = state[r][c];
142
- return output;
143
- }
144
-
145
- subBytes(s) {
146
- for (let r = 0; r < 4; r++)
147
- for (let c = 0; c < 4; c++) s[r][c] = this.SBOX[s[r][c]];
148
- return s;
149
- }
150
-
151
- invSubBytes(s) {
152
- for (let r = 0; r < 4; r++)
153
- for (let c = 0; c < 4; c++) s[r][c] = this.INV_SBOX[s[r][c]];
154
- return s;
155
- }
156
-
157
- shiftRows(s) {
158
- const t = s.map((r) => [...r]);
159
- s[1][0] = t[1][1];
160
- s[1][1] = t[1][2];
161
- s[1][2] = t[1][3];
162
- s[1][3] = t[1][0];
163
- s[2][0] = t[2][2];
164
- s[2][1] = t[2][3];
165
- s[2][2] = t[2][0];
166
- s[2][3] = t[2][1];
167
- s[3][0] = t[3][3];
168
- s[3][1] = t[3][0];
169
- s[3][2] = t[3][1];
170
- s[3][3] = t[3][2];
171
- return s;
172
- }
173
-
174
- invShiftRows(s) {
175
- const t = s.map((r) => [...r]);
176
- s[1][0] = t[1][3];
177
- s[1][1] = t[1][0];
178
- s[1][2] = t[1][1];
179
- s[1][3] = t[1][2];
180
- s[2][0] = t[2][2];
181
- s[2][1] = t[2][3];
182
- s[2][2] = t[2][0];
183
- s[2][3] = t[2][1];
184
- s[3][0] = t[3][1];
185
- s[3][1] = t[3][2];
186
- s[3][2] = t[3][3];
187
- s[3][3] = t[3][0];
188
- return s;
189
- }
190
-
191
- mixColumns(s) {
192
- for (let c = 0; c < 4; c++) {
193
- const a0 = s[0][c],
194
- a1 = s[1][c],
195
- a2 = s[2][c],
196
- a3 = s[3][c];
197
- s[0][c] = this.xtime(a0) ^ this.xtime(a1) ^ a1 ^ a2 ^ a3;
198
- s[1][c] = a0 ^ this.xtime(a1) ^ this.xtime(a2) ^ a2 ^ a3;
199
- s[2][c] = a0 ^ a1 ^ this.xtime(a2) ^ this.xtime(a3) ^ a3;
200
- s[3][c] = this.xtime(a0) ^ a0 ^ a1 ^ a2 ^ this.xtime(a3);
201
- }
202
- return s;
203
- }
204
-
205
- invMixColumns(s) {
206
- for (let c = 0; c < 4; c++) {
207
- const a = [s[0][c], s[1][c], s[2][c], s[3][c]];
208
- const xtime2 = (v) => this.xtime(v);
209
- const xtime4 = (v) => this.xtime(xtime2(v));
210
- const xtime8 = (v) => this.xtime(xtime4(v));
211
- const mul = (v, b) => {
212
- let res = 0;
213
- if (b & 1) res ^= v;
214
- if (b & 2) res ^= xtime2(v);
215
- if (b & 4) res ^= xtime4(v);
216
- if (b & 8) res ^= xtime8(v);
217
- return res & 0xff;
218
- };
219
- s[0][c] =
220
- mul(a[0], 0x0e) ^ mul(a[1], 0x0b) ^ mul(a[2], 0x0d) ^ mul(a[3], 0x09);
221
- s[1][c] =
222
- mul(a[0], 0x09) ^ mul(a[1], 0x0e) ^ mul(a[2], 0x0b) ^ mul(a[3], 0x0d);
223
- s[2][c] =
224
- mul(a[0], 0x0d) ^ mul(a[1], 0x09) ^ mul(a[2], 0x0e) ^ mul(a[3], 0x0b);
225
- s[3][c] =
226
- mul(a[0], 0x0b) ^ mul(a[1], 0x0d) ^ mul(a[2], 0x09) ^ mul(a[3], 0x0e);
227
- }
228
- return s;
229
- }
230
-
231
- addRoundKey(s, w, round) {
232
- for (let c = 0; c < 4; c++) {
233
- const word = w[round * 4 + c];
234
- s[0][c] ^= word[0];
235
- s[1][c] ^= word[1];
236
- s[2][c] ^= word[2];
237
- s[3][c] ^= word[3];
238
- }
239
- return s;
240
- }
241
-
242
- pkcs7Pad(bytes) {
243
- const padding = 16 - (bytes.length % 16);
244
- const result = bytes.slice();
245
- for (let i = 0; i < padding; i++) result.push(padding);
246
- return result;
247
- }
248
-
249
- pkcs7Unpad(bytes) {
250
- const padding = bytes[bytes.length - 1];
251
- if (padding < 1 || padding > 16 || bytes.length < padding) {
252
- throw new Error("Invalid PKCS#7 padding");
253
- }
254
- for (let i = 1; i <= padding; i++) {
255
- if (bytes[bytes.length - i] !== padding) {
256
- throw new Error("Corrupted PKCS#7 padding");
257
- }
258
- }
259
- return bytes.slice(0, -padding);
260
- }
261
-
262
- encrypt(text, keyString) {
263
- const key = this.parseKey(keyString);
264
- const w = this.keyExpansion(key);
265
- let inputBytes = text.split("").map((c) => c.charCodeAt(0));
266
- inputBytes = this.pkcs7Pad(inputBytes);
267
- let outputBytes = [];
268
- for (let i = 0; i < inputBytes.length; i += 16) {
269
- outputBytes.push(...this.cipher(inputBytes.slice(i, i + 16), w));
270
- }
271
- return outputBytes.map((b) => b.toString(16).padStart(2, "0")).join("");
272
- }
273
-
274
- decrypt(hexString, keyString) {
275
- if (hexString.length % 2 !== 0)
276
- throw new Error("Hex string must have even length");
277
- const key = this.parseKey(keyString);
278
- const w = this.keyExpansion(key);
279
- let inputBytes = [];
280
- for (let i = 0; i < hexString.length; i += 2) {
281
- inputBytes.push(parseInt(hexString.substr(i, 2), 16));
282
- }
283
- if (inputBytes.length % 16 !== 0)
284
- throw new Error("Ciphertext length must be multiple of 16");
285
- let decryptedBytes = [];
286
- for (let i = 0; i < inputBytes.length; i += 16) {
287
- decryptedBytes.push(...this.invCipher(inputBytes.slice(i, i + 16), w));
288
- }
289
- decryptedBytes = this.pkcs7Unpad(decryptedBytes);
290
- return String.fromCharCode.apply(null, decryptedBytes);
291
- }
292
- }
293
- export default AES;
294
-
295
- // Export singleton
296
- // export default new AES();
package/src/index.d.ts DELETED
@@ -1,4 +0,0 @@
1
- export class AES {
2
- encrypt(text: string, key: string): string;
3
- decrypt(hex: string, key: string): string;
4
- }
package/src/index.js DELETED
@@ -1,4 +0,0 @@
1
- import AES from "./AES";
2
- import generateKey from "./keygen";
3
-
4
- export { AES, generateKey };
package/src/keygen.js DELETED
@@ -1,49 +0,0 @@
1
- function generateKey(format = "hex", options = {}) {
2
- const { length = 16, readable = false } = options;
3
-
4
- if (![16, 24, 32].includes(length)) {
5
- throw new Error("Key length must be 16, 24, or 32 bytes (AES-128/192/256)");
6
- }
7
-
8
- const bytes = new Uint8Array(length);
9
-
10
- // RN + Expo + Node secure RNG
11
- if (globalThis.crypto?.getRandomValues) {
12
- crypto.getRandomValues(bytes);
13
- } else {
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
- }
18
- }
19
-
20
- const toHex = (buf) =>
21
- Array.from(buf, (b) => b.toString(16).padStart(2, "0")).join("");
22
-
23
- const toBase64 = (buf) => {
24
- if (typeof Buffer !== "undefined") {
25
- return Buffer.from(buf).toString("base64");
26
- }
27
- return btoa(String.fromCharCode(...buf));
28
- };
29
-
30
- const toReadable = (buf) => {
31
- const chars =
32
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()_+-=";
33
- return Array.from(buf, (b) => chars[b % chars.length]).join("");
34
- };
35
-
36
- switch (format.toLowerCase()) {
37
- case "hex":
38
- return toHex(bytes);
39
- case "base64":
40
- return toBase64(bytes);
41
- case "ascii":
42
- case "string":
43
- return readable ? toReadable(bytes) : String.fromCharCode(...bytes);
44
- default:
45
- throw new Error("Format must be 'hex', 'base64', or 'ascii'");
46
- }
47
- }
48
-
49
- export default generateKey;