@ibiliaze/stringman 3.16.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,4 +1,6 @@
1
1
  export * from './seat';
2
+ import { fishyMatchesAll } from './ticket';
3
+ import { decryptString, encryptString } from './crypto';
2
4
  /**
3
5
  * Clean up extra whitespace in a string.
4
6
  *
@@ -159,6 +161,11 @@ export declare const ticket: {
159
161
  dashed?: boolean;
160
162
  }) => string;
161
163
  validateTicketCode: (code: string) => boolean;
164
+ fishyMatchesAll: typeof fishyMatchesAll;
165
+ };
166
+ export declare const crypto: {
167
+ encryptString: typeof encryptString;
168
+ decryptString: typeof decryptString;
162
169
  };
163
170
  export declare const order: {
164
171
  createNumericOrderId: () => string;
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
  *
@@ -306,5 +307,10 @@ exports.ticket = {
306
307
  getTicketId: ticket_1.getTicketId,
307
308
  buildTicketCode: ticket_1.buildTicketCode,
308
309
  validateTicketCode: ticket_1.validateTicketCode,
310
+ fishyMatchesAll: ticket_1.fishyMatchesAll,
311
+ };
312
+ exports.crypto = {
313
+ encryptString: crypto_1.encryptString,
314
+ decryptString: crypto_1.decryptString,
309
315
  };
310
316
  exports.order = { createNumericOrderId: order_1.createNumericOrderId };
package/dist/ticket.d.ts CHANGED
@@ -59,6 +59,15 @@ export declare const buildTicketCodeNumeric: (args: {
59
59
  * Checks: numeric, correct total length (17), and Luhn checksum.
60
60
  */
61
61
  export declare const validateTicketCodeNumeric: (code: string) => boolean;
62
+ /**
63
+ * Check if a fishy ticket's core digits (last 6 before the final digit)
64
+ * match ALL ticket codes in the list.
65
+ *
66
+ * @param {string[]} tickets - list of ticket codes
67
+ * @param {string} fishyCode - the suspect ticket code
68
+ * @returns {boolean} true if fishyCode matches all, false otherwise
69
+ */
70
+ export declare function fishyMatchesAll(tickets: string[], fishyCode: string): boolean;
62
71
  /**
63
72
  * A stable, human-readable internal ID for a seat within a fixture.
64
73
  * (Not used in the printed/scanned code — just handy elsewhere.)
package/dist/ticket.js CHANGED
@@ -5,6 +5,7 @@
5
5
  // =====================================================================================
6
6
  Object.defineProperty(exports, "__esModule", { value: true });
7
7
  exports.validateTicketCode = exports.buildTicketCode = exports.getTicketId = exports.validateTicketCodeNumeric = exports.buildTicketCodeNumeric = exports.rndBlockNum = exports.fixtureBlockNum = exports.seatBlockNum = exports.fixtureIdBlockNum = exports.venueBlockNum = exports.luhn10 = void 0;
8
+ exports.fishyMatchesAll = fishyMatchesAll;
8
9
  const node_crypto_1 = require("node:crypto");
9
10
  // ---------- config: block lengths (easy to tweak in one place) ----------
10
11
  const CODE_LEN = {
@@ -176,6 +177,32 @@ const validateTicketCodeNumeric = (code) => {
176
177
  }
177
178
  };
178
179
  exports.validateTicketCodeNumeric = validateTicketCodeNumeric;
180
+ /**
181
+ * Check if a fishy ticket's core digits (last 6 before the final digit)
182
+ * match ALL ticket codes in the list.
183
+ *
184
+ * @param {string[]} tickets - list of ticket codes
185
+ * @param {string} fishyCode - the suspect ticket code
186
+ * @returns {boolean} true if fishyCode matches all, false otherwise
187
+ */
188
+ function fishyMatchesAll(tickets, fishyCode) {
189
+ try {
190
+ // Need at least 7 chars to have "XXXXXX" + last digit
191
+ if (typeof fishyCode !== 'string' || fishyCode.length < 7)
192
+ return false;
193
+ const fishyCore = fishyCode.slice(-7, -1); // last 6 chars before very last
194
+ return tickets.every(code => {
195
+ if (typeof code !== 'string' || code.length < 7)
196
+ return false;
197
+ const core = code.slice(-7, -1);
198
+ return core === fishyCore;
199
+ });
200
+ }
201
+ catch (e) {
202
+ console.error(e);
203
+ return false;
204
+ }
205
+ }
179
206
  // =====================================================================================
180
207
  /**
181
208
  * A stable, human-readable internal ID for a seat within a fixture.
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "@ibiliaze/stringman",
3
- "version": "3.16.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.16.0 -m '3.16.0'; git push origin 3.16.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
- import { buildTicketCode, getTicketId, validateTicketCode } from './ticket';
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.
@@ -282,6 +283,12 @@ export const ticket = {
282
283
  getTicketId,
283
284
  buildTicketCode,
284
285
  validateTicketCode,
286
+ fishyMatchesAll,
287
+ };
288
+
289
+ export const crypto = {
290
+ encryptString,
291
+ decryptString,
285
292
  };
286
293
 
287
294
  export const order = { createNumericOrderId };
package/src/ticket.ts CHANGED
@@ -187,6 +187,32 @@ export const validateTicketCodeNumeric = (code: string) => {
187
187
  }
188
188
  };
189
189
 
190
+ /**
191
+ * Check if a fishy ticket's core digits (last 6 before the final digit)
192
+ * match ALL ticket codes in the list.
193
+ *
194
+ * @param {string[]} tickets - list of ticket codes
195
+ * @param {string} fishyCode - the suspect ticket code
196
+ * @returns {boolean} true if fishyCode matches all, false otherwise
197
+ */
198
+ export function fishyMatchesAll(tickets: string[], fishyCode: string): boolean {
199
+ try {
200
+ // Need at least 7 chars to have "XXXXXX" + last digit
201
+ if (typeof fishyCode !== 'string' || fishyCode.length < 7) return false;
202
+
203
+ const fishyCore = fishyCode.slice(-7, -1); // last 6 chars before very last
204
+
205
+ return tickets.every(code => {
206
+ if (typeof code !== 'string' || code.length < 7) return false;
207
+ const core = code.slice(-7, -1);
208
+ return core === fishyCore;
209
+ });
210
+ } catch (e) {
211
+ console.error(e);
212
+ return false;
213
+ }
214
+ }
215
+
190
216
  // =====================================================================================
191
217
  /**
192
218
  * A stable, human-readable internal ID for a seat within a fixture.