@ibiliaze/stringman 3.17.0 → 3.18.0

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.
@@ -0,0 +1,2 @@
1
+ export declare function encryptString(plainText: string, password: string): Promise<string>;
2
+ export declare function decryptString(cipherTextB64: string, password: string): Promise<string>;
package/dist/crypto.js ADDED
@@ -0,0 +1,55 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.encryptString = encryptString;
4
+ exports.decryptString = decryptString;
5
+ const enc = new TextEncoder();
6
+ const dec = new TextDecoder();
7
+ async function getKeyFromPassword(password, salt) {
8
+ const keyMaterial = await crypto.subtle.importKey('raw', enc.encode(password), 'PBKDF2', false, ['deriveKey']);
9
+ return crypto.subtle.deriveKey({
10
+ name: 'PBKDF2',
11
+ salt, // ArrayBuffer is valid BufferSource
12
+ iterations: 100_000,
13
+ hash: 'SHA-256',
14
+ }, keyMaterial, {
15
+ name: 'AES-GCM',
16
+ length: 256,
17
+ }, false, ['encrypt', 'decrypt']);
18
+ }
19
+ async function encryptString(plainText, password) {
20
+ const saltBytes = crypto.getRandomValues(new Uint8Array(16));
21
+ const iv = crypto.getRandomValues(new Uint8Array(12));
22
+ const key = await getKeyFromPassword(password, saltBytes.buffer);
23
+ const cipherBuffer = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, enc.encode(plainText));
24
+ const cipherBytes = new Uint8Array(cipherBuffer);
25
+ const combined = new Uint8Array(saltBytes.length + iv.length + cipherBytes.length);
26
+ combined.set(saltBytes, 0);
27
+ combined.set(iv, saltBytes.length);
28
+ combined.set(cipherBytes, saltBytes.length + iv.length);
29
+ return bufferToBase64(combined.buffer);
30
+ }
31
+ async function decryptString(cipherTextB64, password) {
32
+ const combined = new Uint8Array(base64ToBuffer(cipherTextB64));
33
+ const saltBytes = combined.slice(0, 16);
34
+ const iv = combined.slice(16, 28);
35
+ const cipherBytes = combined.slice(28);
36
+ // 👇 again, .buffer
37
+ const key = await getKeyFromPassword(password, saltBytes.buffer);
38
+ const plainBuffer = await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, key, cipherBytes);
39
+ return dec.decode(plainBuffer);
40
+ }
41
+ /* base64 helpers stay the same */
42
+ function bufferToBase64(buf) {
43
+ const bytes = new Uint8Array(buf);
44
+ let binary = '';
45
+ for (let i = 0; i < bytes.length; i++)
46
+ binary += String.fromCharCode(bytes[i]);
47
+ return btoa(binary);
48
+ }
49
+ function base64ToBuffer(b64) {
50
+ const binary = atob(b64);
51
+ const bytes = new Uint8Array(binary.length);
52
+ for (let i = 0; i < binary.length; i++)
53
+ bytes[i] = binary.charCodeAt(i);
54
+ return bytes.buffer;
55
+ }
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from './seat';
2
2
  import { fishyMatchesAll } from './ticket';
3
+ import { decryptString, encryptString } from './crypto';
3
4
  /**
4
5
  * Clean up extra whitespace in a string.
5
6
  *
@@ -162,6 +163,10 @@ export declare const ticket: {
162
163
  validateTicketCode: (code: string) => boolean;
163
164
  fishyMatchesAll: typeof fishyMatchesAll;
164
165
  };
166
+ export declare const crypto: {
167
+ encryptString: typeof encryptString;
168
+ decryptString: typeof decryptString;
169
+ };
165
170
  export declare const order: {
166
171
  createNumericOrderId: () => string;
167
172
  };
package/dist/index.js CHANGED
@@ -14,10 +14,11 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.order = exports.ticket = exports.luhn36 = exports.b36 = exports.invalidPw = exports.getPublicIP = exports.isVideoUrl = exports.getRandomString = exports.extractImageSrcs = exports.select = exports.query = exports.getCloudinaryPublicId = exports.dp = exports.megaTrim = exports.superTrim = void 0;
17
+ exports.order = exports.crypto = exports.ticket = exports.luhn36 = exports.b36 = exports.invalidPw = exports.getPublicIP = exports.isVideoUrl = exports.getRandomString = exports.extractImageSrcs = exports.select = exports.query = exports.getCloudinaryPublicId = exports.dp = exports.megaTrim = exports.superTrim = void 0;
18
18
  __exportStar(require("./seat"), exports);
19
19
  const ticket_1 = require("./ticket");
20
20
  const order_1 = require("./order");
21
+ const crypto_1 = require("./crypto");
21
22
  /**
22
23
  * Clean up extra whitespace in a string.
23
24
  *
@@ -308,4 +309,8 @@ exports.ticket = {
308
309
  validateTicketCode: ticket_1.validateTicketCode,
309
310
  fishyMatchesAll: ticket_1.fishyMatchesAll,
310
311
  };
312
+ exports.crypto = {
313
+ encryptString: crypto_1.encryptString,
314
+ decryptString: crypto_1.decryptString,
315
+ };
311
316
  exports.order = { createNumericOrderId: order_1.createNumericOrderId };
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@ibiliaze/stringman",
3
- "version": "3.17.0",
3
+ "version": "3.18.0",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
8
8
  "build": "tsc",
9
9
  "pub": "npm publish --access public",
10
- "git": "git add .; git commit -m 'changes'; git tag -a 3.17.0 -m '3.17.0'; git push origin 3.17.0; git push",
10
+ "git": "git add .; git commit -m 'changes'; git tag -a 3.18.0 -m '3.18.0'; git push origin 3.18.0; git push",
11
11
  "push": "npm run build; npm run git; npm run pub"
12
12
  },
13
13
  "author": "Ibi Hasanli",
package/src/crypto.ts ADDED
@@ -0,0 +1,69 @@
1
+ const enc = new TextEncoder();
2
+ const dec = new TextDecoder();
3
+
4
+ async function getKeyFromPassword(password: string, salt: ArrayBuffer): Promise<CryptoKey> {
5
+ const keyMaterial = await crypto.subtle.importKey('raw', enc.encode(password), 'PBKDF2', false, ['deriveKey']);
6
+
7
+ return crypto.subtle.deriveKey(
8
+ {
9
+ name: 'PBKDF2',
10
+ salt, // ArrayBuffer is valid BufferSource
11
+ iterations: 100_000,
12
+ hash: 'SHA-256',
13
+ },
14
+ keyMaterial,
15
+ {
16
+ name: 'AES-GCM',
17
+ length: 256,
18
+ },
19
+ false,
20
+ ['encrypt', 'decrypt']
21
+ );
22
+ }
23
+
24
+ export async function encryptString(plainText: string, password: string): Promise<string> {
25
+ const saltBytes = crypto.getRandomValues(new Uint8Array(16));
26
+ const iv = crypto.getRandomValues(new Uint8Array(12));
27
+
28
+ const key = await getKeyFromPassword(password, saltBytes.buffer);
29
+
30
+ const cipherBuffer = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, key, enc.encode(plainText));
31
+
32
+ const cipherBytes = new Uint8Array(cipherBuffer);
33
+ const combined = new Uint8Array(saltBytes.length + iv.length + cipherBytes.length);
34
+ combined.set(saltBytes, 0);
35
+ combined.set(iv, saltBytes.length);
36
+ combined.set(cipherBytes, saltBytes.length + iv.length);
37
+
38
+ return bufferToBase64(combined.buffer);
39
+ }
40
+
41
+ export async function decryptString(cipherTextB64: string, password: string): Promise<string> {
42
+ const combined = new Uint8Array(base64ToBuffer(cipherTextB64));
43
+
44
+ const saltBytes = combined.slice(0, 16);
45
+ const iv = combined.slice(16, 28);
46
+ const cipherBytes = combined.slice(28);
47
+
48
+ // 👇 again, .buffer
49
+ const key = await getKeyFromPassword(password, saltBytes.buffer);
50
+
51
+ const plainBuffer = await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, key, cipherBytes);
52
+
53
+ return dec.decode(plainBuffer);
54
+ }
55
+
56
+ /* base64 helpers stay the same */
57
+ function bufferToBase64(buf: ArrayBuffer): string {
58
+ const bytes = new Uint8Array(buf);
59
+ let binary = '';
60
+ for (let i = 0; i < bytes.length; i++) binary += String.fromCharCode(bytes[i]);
61
+ return btoa(binary);
62
+ }
63
+
64
+ function base64ToBuffer(b64: string): ArrayBuffer {
65
+ const binary = atob(b64);
66
+ const bytes = new Uint8Array(binary.length);
67
+ for (let i = 0; i < binary.length; i++) bytes[i] = binary.charCodeAt(i);
68
+ return bytes.buffer;
69
+ }
package/src/index.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export * from './seat';
2
2
  import { buildTicketCode, getTicketId, validateTicketCode, fishyMatchesAll } from './ticket';
3
3
  import { createNumericOrderId } from './order';
4
+ import { decryptString, encryptString } from './crypto';
4
5
 
5
6
  /**
6
7
  * Clean up extra whitespace in a string.
@@ -285,4 +286,9 @@ export const ticket = {
285
286
  fishyMatchesAll,
286
287
  };
287
288
 
289
+ export const crypto = {
290
+ encryptString,
291
+ decryptString,
292
+ };
293
+
288
294
  export const order = { createNumericOrderId };