@strimz/shared-crypto 0.1.0 → 0.1.1

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/README.md CHANGED
@@ -1,8 +1,30 @@
1
1
  # @strimz/shared-crypto
2
2
 
3
- Cryptographic primitives for the Strimz platform. Built on the Web Crypto API so the same code runs unchanged in Node 22, Vercel Edge, Cloudflare Workers, and any modern browser.
3
+ > Web Crypto primitives for Strimz. HMAC webhook signing, API key generation, AES-256-GCM envelope encryption, and constant-time comparison. Runs anywhere Web Crypto runs.
4
4
 
5
- ## What lives here
5
+ [![npm version](https://img.shields.io/npm/v/@strimz/shared-crypto.svg?color=02C76A)](https://www.npmjs.com/package/@strimz/shared-crypto)
6
+ [![npm downloads](https://img.shields.io/npm/dm/@strimz/shared-crypto.svg)](https://www.npmjs.com/package/@strimz/shared-crypto)
7
+ [![bundle size](https://img.shields.io/bundlephobia/minzip/@strimz/shared-crypto.svg?label=size)](https://bundlephobia.com/package/@strimz/shared-crypto)
8
+ [![types](https://img.shields.io/npm/types/@strimz/shared-crypto.svg)](https://www.npmjs.com/package/@strimz/shared-crypto)
9
+ [![License: MIT](https://img.shields.io/badge/License-MIT-02C76A.svg)](./LICENSE)
10
+
11
+ Every primitive is built on the Web Crypto API
12
+ (`globalThis.crypto.subtle`), so the same code runs unchanged in
13
+ Node 22, Vercel Edge, Cloudflare Workers, Deno, Bun, and any modern
14
+ browser. There is no `node:crypto`, no Buffer, no platform-specific
15
+ shim.
16
+
17
+ ## Install
18
+
19
+ ```sh
20
+ pnpm add @strimz/shared-crypto
21
+ # npm install @strimz/shared-crypto
22
+ # yarn add @strimz/shared-crypto
23
+ ```
24
+
25
+ ## What's inside
26
+
27
+ Every group is also available as a subpath import.
6
28
 
7
29
  | Subpath | Contents |
8
30
  | -------------- | ------------------------------------------------------------------------------------------------------- |
@@ -11,7 +33,7 @@ Cryptographic primitives for the Strimz platform. Built on the Web Crypto API so
11
33
  | `/hmac` | `hmacSha256`, `hmacSha256Hex` |
12
34
  | `/random` | `randomBytes`, `randomHex`, `randomBase64Url`, `uuid` |
13
35
  | `/timing-safe` | `timingSafeEqualBytes`, `timingSafeEqualString` |
14
- | `/webhook` | `signWebhookPayload`, `verifyWebhookSignature` (Stripe-shaped scheme) |
36
+ | `/webhook` | `signWebhookPayload`, `verifyWebhookSignature` (timestamp + HMAC scheme) |
15
37
  | `/api-key` | `generateApiKey`, `hashApiKey`, `redactApiKey` |
16
38
  | `/aes-gcm` | `encryptAesGcm`, `decryptAesGcm`, `generateAesGcmKey` (envelope encryption for webhook secrets at rest) |
17
39
 
@@ -33,7 +55,10 @@ if (!result.valid) {
33
55
  }
34
56
  ```
35
57
 
36
- Signatures are HMAC-SHA256 over `"${t}.${payload}"`. The timestamp is bound to the signature so replays are detectable; verification rejects any timestamp outside the tolerance window (default 5 minutes, configured in `@strimz/shared-config/webhooks`).
58
+ Signatures are HMAC-SHA256 over `"${t}.${payload}"`. The timestamp is
59
+ bound to the signature so replays are detectable. Verification rejects
60
+ any timestamp outside the tolerance window (default 5 minutes,
61
+ configured in `@strimz/shared-config/webhooks`).
37
62
 
38
63
  ## API key generation
39
64
 
@@ -42,16 +67,51 @@ import { generateApiKey, hashApiKey } from '@strimz/shared-crypto'
42
67
 
43
68
  const { secret, hash, prefix, lastFour } = await generateApiKey('secret', 'test')
44
69
  // Persist { hash, prefix, lastFour, kind: 'secret', mode: 'test' }.
45
- // Return `secret` to the caller once, then drop it from memory.
70
+ // Return `secret` to the caller exactly once, then drop it from memory.
46
71
 
47
72
  // On every authenticated request:
48
73
  const incomingHash = await hashApiKey(req.headers.authorization.slice('Bearer '.length))
49
74
  const row = await db.apiKey.findUnique({ where: { hash: incomingHash } })
50
75
  ```
51
76
 
52
- ## Boundaries
77
+ ## AES-256-GCM envelope encryption
78
+
79
+ ```ts
80
+ import { encryptAesGcm, decryptAesGcm, generateAesGcmKey } from '@strimz/shared-crypto'
81
+
82
+ const key = await generateAesGcmKey() // 32 bytes, hex-encoded
83
+ const ciphertext = await encryptAesGcm('hello world', key)
84
+ const plaintext = await decryptAesGcm(ciphertext, key)
85
+ ```
86
+
87
+ The ciphertext encoding is `v1:<nonce-hex>:<ciphertext-and-tag-hex>`,
88
+ safe to store in a text column. Suitable for envelope-encrypting
89
+ small secrets where the key is held outside the data store.
90
+
91
+ ## Runtime support
92
+
93
+ | Runtime | Supported |
94
+ | ------------------ | --------- |
95
+ | Node 22+ | Yes |
96
+ | Browsers | Yes |
97
+ | Vercel Edge | Yes |
98
+ | Cloudflare Workers | Yes |
99
+ | Deno / Bun | Yes |
100
+
101
+ ## Design notes
53
102
 
54
103
  - **No secrets are logged.** `redactApiKey` exists specifically so any diagnostic that must include a key logs only the safe prefix and the last four characters.
55
104
  - **No Node `Buffer` or `node:crypto`.** Every primitive uses `globalThis.crypto.subtle` and `Uint8Array`.
56
- - **Constant-time compare on signatures.** Signature verification must never short-circuit on the first mismatched byte.
57
- - **The package does not decide policy.** Tolerance windows, key prefixes, and tier thresholds come from `@strimz/shared-config`; this package consumes them.
105
+ - **Constant-time compare on signatures.** Signature verification never short-circuits on the first mismatched byte.
106
+ - **Policy lives elsewhere.** Tolerance windows, key prefixes, and tier thresholds come from `@strimz/shared-config`; this package consumes them.
107
+
108
+ ## Links
109
+
110
+ - [Documentation](https://strimz.finance/docs)
111
+ - [Repository](https://github.com/StrimzLab/Strimz/tree/main/packages/shared-crypto)
112
+ - [Changelog](./CHANGELOG.md)
113
+ - [Issues](https://github.com/StrimzLab/Strimz/issues)
114
+
115
+ ## License
116
+
117
+ [MIT](./LICENSE) © Strimz
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/encoding.ts","../src/aes-gcm.ts"],"names":[],"mappings":";;;AAMA,IAAM,SAAA,GAAY,kBAAA;AAEX,SAAS,MAAM,KAAA,EAA2B;AAC/C,EAAA,IAAI,GAAA,GAAM,EAAA;AACV,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAA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AM,SAAA,CAAU,MAAM,CAAA;AAClC,EAAA,MAAM,iBAAiB,IAAI,UAAA;AAAA,IACzB,MAAM,OAAO,MAAA,CAAO,OAAA;AAAA,MAClB,EAAE,IAAA,EAAM,SAAA,EAAW,EAAA,EAAI,KAAA,EAAuB,WAAW,eAAA,EAAgB;AAAA,MACzE,GAAA;AAAA,MACA;AAAA;AACF,GACF;AACA,EAAA,OAAO,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,cAAc,CAAA;AAChD;AAOO,SAAS,iBAAA,GAA4B;AAC1C,EAAA,MAAM,QAAQ,MAAA,CAAO,eAAA,CAAgB,IAAI,UAAA,CAAW,gBAAgB,CAAC,CAAA;AACrE,EAAA,OAAO,MAAM,KAAK,CAAA;AACpB;AAEA,eAAe,UAAU,MAAA,EAAoC;AAC3D,EAAA,IAAI,CAAC,iBAAA,CAAkB,IAAA,CAAK,MAAM,CAAA,EAAG;AACnC,IAAA,MAAM,IAAI,MAAM,6CAA6C,CAAA;AAAA,EAC/D;AACA,EAAA,MAAM,KAAA,GAAQ,QAAQ,MAAM,CAAA;AAC5B,EAAA,IAAI,KAAA,CAAM,WAAW,gBAAA,EAAkB;AACrC,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,uBAAA,EAA0B,gBAAgB,CAAA,YAAA,EAAe,KAAA,CAAM,MAAM,CAAA,CAAA,CAAG,CAAA;AAAA,EAC1F;AACA,EAAA,OAAO,MAAA,CAAO,OAAO,SAAA,CAAU,KAAA,EAAO,OAAuB,EAAE,IAAA,EAAM,SAAA,EAAU,EAAG,KAAA,EAAO;AAAA,IACvF,SAAA;AAAA,IACA;AAAA,GACD,CAAA;AACH","file":"aes-gcm.cjs","sourcesContent":["/**\n * Byte encoders that work in every JavaScript runtime we target.\n * No Node `Buffer`, no dependency on the DOM — just Uint8Array, strings, and\n * the two TextEncoder/TextDecoder classes available everywhere.\n */\n\nconst HEX_CHARS = '0123456789abcdef'\n\nexport function toHex(bytes: Uint8Array): string {\n let out = ''\n for (let i = 0; i < bytes.length; i++) {\n const b = bytes[i] as number\n out += HEX_CHARS[b >>> 4]\n out += HEX_CHARS[b & 0x0f]\n }\n return out\n}\n\nexport function fromHex(hex: string): Uint8Array {\n const clean = hex.startsWith('0x') ? hex.slice(2) : hex\n if (clean.length % 2 !== 0) {\n throw new Error('fromHex: odd-length hex input')\n }\n const out = new Uint8Array(clean.length / 2)\n for (let i = 0; i < out.length; i++) {\n const byte = Number.parseInt(clean.substr(i * 2, 2), 16)\n if (Number.isNaN(byte)) {\n throw new Error('fromHex: non-hex character')\n }\n out[i] = byte\n }\n return out\n}\n\nexport function toBase64Url(bytes: Uint8Array): string {\n // Build a binary string without spreading (spread can overflow on large arrays).\n // `btoa` is global in Node 16+, all Edge runtimes, and browsers.\n let binary = ''\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i] as number)\n }\n return btoa(binary).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nexport function utf8ToBytes(str: string): Uint8Array {\n return new TextEncoder().encode(str)\n}\n\nexport function bytesToUtf8(bytes: Uint8Array): string {\n return new TextDecoder().decode(bytes)\n}\n","/**\n * AES-256-GCM authenticated symmetric encryption via Web Crypto.\n *\n * Used by the webhook layer to encrypt signing secrets at rest in\n * Postgres. Sized at the smallest interface that does the job —\n * encrypt one short string at a time, decrypt one short string at a\n * time, with the encryption key supplied as a 32-byte hex string from\n * environment configuration.\n *\n * Why Web Crypto over `node:crypto`: the rest of `@strimz/shared-crypto`\n * targets Node 22, Vercel Edge, and Cloudflare Workers uniformly.\n * Keeping this consistent so the package can be imported anywhere\n * without conditional branches.\n *\n * Why AES-256-GCM:\n * - Authenticated. The auth tag detects bit-flips or substitution\n * attacks; a tampered ciphertext fails closed at decrypt time.\n * - Standard. Available natively in Web Crypto with no third-party\n * dependency.\n * - 12-byte nonce + 16-byte tag is the canonical layout.\n *\n * Output format (string-safe so it goes into Postgres TEXT columns\n * without re-encoding):\n *\n * v1:<nonce-hex>:<ciphertext-and-tag-hex>\n *\n * Web Crypto returns the auth tag concatenated to the ciphertext,\n * so the on-the-wire format keeps them together rather than splitting\n * them out. The leading `v1:` lets future schemes coexist during\n * migration.\n *\n * What this is NOT:\n * - A KMS replacement. The key lives in env. Rotating it means\n * rotating every encrypted value (or supporting two keys for a\n * cutover window). Graduate to a hardware-backed envelope when\n * funded — the `KmsSigner` abstraction in the API is the parallel\n * pattern for sign keys.\n */\n\nimport { fromHex, toHex } from './encoding.js'\n\nconst ALGORITHM = 'AES-GCM'\nconst KEY_LENGTH_BYTES = 32\nconst NONCE_LENGTH_BYTES = 12\nconst TAG_LENGTH_BITS = 128\nconst VERSION_PREFIX = 'v1:'\n\n/**\n * Encrypt a plaintext string with the supplied 32-byte hex key.\n * Returns a versioned, colon-delimited ciphertext blob safe to store\n * in a Postgres TEXT column.\n */\nexport async function encryptAesGcm(plaintext: string, keyHex: string): Promise<string> {\n const key = await importKey(keyHex)\n const nonce = crypto.getRandomValues(new Uint8Array(NONCE_LENGTH_BYTES))\n const ciphertextWithTag = new Uint8Array(\n await crypto.subtle.encrypt(\n { name: ALGORITHM, iv: nonce as BufferSource, tagLength: TAG_LENGTH_BITS },\n key,\n new TextEncoder().encode(plaintext) as BufferSource,\n ),\n )\n return `${VERSION_PREFIX}${toHex(nonce)}:${toHex(ciphertextWithTag)}`\n}\n\n/**\n * Inverse of `encryptAesGcm`. Throws on any tampering or key mismatch\n * — there is no partial-decrypt path. The caller decides how to\n * surface the failure (log + fail closed for storage; reject the\n * request for hot-path use).\n */\nexport async function decryptAesGcm(blob: string, keyHex: string): Promise<string> {\n if (!blob.startsWith(VERSION_PREFIX)) {\n throw new Error('decryptAesGcm: unsupported version prefix in blob')\n }\n const parts = blob.slice(VERSION_PREFIX.length).split(':')\n if (parts.length !== 2) {\n throw new Error('decryptAesGcm: malformed blob; expected nonce:ciphertext')\n }\n const [nonceHex, ciphertextHex] = parts as [string, string]\n const nonce = fromHex(nonceHex)\n const ciphertextWithTag = fromHex(ciphertextHex)\n if (nonce.length !== NONCE_LENGTH_BYTES) {\n throw new Error(`decryptAesGcm: nonce must be ${NONCE_LENGTH_BYTES} bytes`)\n }\n const key = await importKey(keyHex)\n const plaintextBytes = new Uint8Array(\n await crypto.subtle.decrypt(\n { name: ALGORITHM, iv: nonce as BufferSource, tagLength: TAG_LENGTH_BITS },\n key,\n ciphertextWithTag as BufferSource,\n ),\n )\n return new TextDecoder().decode(plaintextBytes)\n}\n\n/**\n * Convenience helper for generating a fresh 32-byte hex key — used\n * by ops scripts that seed the env var. Returned as a hex string so\n * it can be pasted into a secrets vault without binary handling.\n */\nexport function generateAesGcmKey(): string {\n const bytes = crypto.getRandomValues(new Uint8Array(KEY_LENGTH_BYTES))\n return toHex(bytes)\n}\n\nasync function importKey(keyHex: string): Promise<CryptoKey> {\n if (!/^[0-9a-fA-F]+$/u.test(keyHex)) {\n throw new Error('encryption key must be a hex-encoded string')\n }\n const bytes = fromHex(keyHex)\n if (bytes.length !== KEY_LENGTH_BYTES) {\n throw new Error(`encryption key must be ${KEY_LENGTH_BYTES} bytes (got ${bytes.length})`)\n }\n return crypto.subtle.importKey('raw', bytes as BufferSource, { name: ALGORITHM }, false, [\n 'encrypt',\n 'decrypt',\n ])\n}\n"]}
1
+ {"version":3,"sources":["../src/encoding.ts","../src/aes-gcm.ts"],"names":[],"mappings":";;;AAMA,IAAM,SAAA,GAAY,kBAAA;AAEX,SAAS,MAAM,KAAA,EAA2B;AAC/C,EAAA,IAAI,GAAA,GAAM,EAAA;AACV,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAM,CAAA,GAAI,MAAM,CAAC,CAAA;AACjB,IAAA,GAAA,IAAO,SAAA,CAAU,MAAM,CAAC,CAAA;AACxB,IAAA,GAAA,IAAO,SAAA,CAAU,IAAI,EAAI,CAAA;AAAA,EAC3B;AACA,EAAA,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ACA;AAAA;AACF,GACF;AACA,EAAA,OAAO,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,cAAc,CAAA;AAChD;AAOO,SAAS,iBAAA,GAA4B;AAC1C,EAAA,MAAM,QAAQ,MAAA,CAAO,eAAA,CAAgB,IAAI,UAAA,CAAW,gBAAgB,CAAC,CAAA;AACrE,EAAA,OAAO,MAAM,KAAK,CAAA;AACpB;AAEA,eAAe,UAAU,MAAA,EAAoC;AAC3D,EAAA,IAAI,CAAC,iBAAA,CAAkB,IAAA,CAAK,MAAM,CAAA,EAAG;AACnC,IAAA,MAAM,IAAI,MAAM,6CAA6C,CAAA;AAAA,EAC/D;AACA,EAAA,MAAM,KAAA,GAAQ,QAAQ,MAAM,CAAA;AAC5B,EAAA,IAAI,KAAA,CAAM,WAAW,gBAAA,EAAkB;AACrC,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,uBAAA,EAA0B,gBAAgB,CAAA,YAAA,EAAe,KAAA,CAAM,MAAM,CAAA,CAAA,CAAG,CAAA;AAAA,EAC1F;AACA,EAAA,OAAO,MAAA,CAAO,OAAO,SAAA,CAAU,KAAA,EAAO,OAAuB,EAAE,IAAA,EAAM,SAAA,EAAU,EAAG,KAAA,EAAO;AAAA,IACvF,SAAA;AAAA,IACA;AAAA,GACD,CAAA;AACH","file":"aes-gcm.cjs","sourcesContent":["/**\n * Byte encoders that work in every JavaScript runtime we target.\n * No Node `Buffer`, no dependency on the DOM. Only Uint8Array, strings, and\n * the two TextEncoder/TextDecoder classes available everywhere.\n */\n\nconst HEX_CHARS = '0123456789abcdef'\n\nexport function toHex(bytes: Uint8Array): string {\n let out = ''\n for (let i = 0; i < bytes.length; i++) {\n const b = bytes[i] as number\n out += HEX_CHARS[b >>> 4]\n out += HEX_CHARS[b & 0x0f]\n }\n return out\n}\n\nexport function fromHex(hex: string): Uint8Array {\n const clean = hex.startsWith('0x') ? hex.slice(2) : hex\n if (clean.length % 2 !== 0) {\n throw new Error('fromHex: odd-length hex input')\n }\n const out = new Uint8Array(clean.length / 2)\n for (let i = 0; i < out.length; i++) {\n const byte = Number.parseInt(clean.substr(i * 2, 2), 16)\n if (Number.isNaN(byte)) {\n throw new Error('fromHex: non-hex character')\n }\n out[i] = byte\n }\n return out\n}\n\nexport function toBase64Url(bytes: Uint8Array): string {\n // Build a binary string without spreading (spread can overflow on large arrays).\n // `btoa` is global in Node 16+, all Edge runtimes, and browsers.\n let binary = ''\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i] as number)\n }\n return btoa(binary).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nexport function utf8ToBytes(str: string): Uint8Array {\n return new TextEncoder().encode(str)\n}\n\nexport function bytesToUtf8(bytes: Uint8Array): string {\n return new TextDecoder().decode(bytes)\n}\n","/**\n * AES-256-GCM authenticated symmetric encryption via Web Crypto.\n *\n * Encrypt one short string at a time, decrypt one short string at a\n * time. The encryption key is supplied as a 32-byte hex string by the\n * caller. Suitable for envelope-encrypting small secrets where the\n * key is held outside the data store.\n *\n * AES-GCM is authenticated, so the auth tag detects bit-flips and\n * substitution attacks. A tampered ciphertext fails closed at decrypt\n * time. The 12-byte nonce and 16-byte tag are the canonical layout\n * exposed by Web Crypto.\n *\n * Output format is string-safe and suitable for any text column:\n *\n * v1:<nonce-hex>:<ciphertext-and-tag-hex>\n *\n * Web Crypto returns the auth tag concatenated to the ciphertext, so\n * the on-the-wire format keeps them together rather than splitting\n * them out. The leading `v1:` lets future schemes coexist during\n * migration.\n *\n * This is not a KMS replacement. The key lives in caller-controlled\n * storage. Rotating it means rotating every encrypted value, or\n * supporting two keys for a cutover window. For sign keys, use a\n * hardware-backed signer pattern instead.\n */\n\nimport { fromHex, toHex } from './encoding.js'\n\nconst ALGORITHM = 'AES-GCM'\nconst KEY_LENGTH_BYTES = 32\nconst NONCE_LENGTH_BYTES = 12\nconst TAG_LENGTH_BITS = 128\nconst VERSION_PREFIX = 'v1:'\n\n/**\n * Encrypt a plaintext string with the supplied 32-byte hex key.\n * Returns a versioned, colon-delimited ciphertext blob safe to store\n * in any text column.\n */\nexport async function encryptAesGcm(plaintext: string, keyHex: string): Promise<string> {\n const key = await importKey(keyHex)\n const nonce = crypto.getRandomValues(new Uint8Array(NONCE_LENGTH_BYTES))\n const ciphertextWithTag = new Uint8Array(\n await crypto.subtle.encrypt(\n { name: ALGORITHM, iv: nonce as BufferSource, tagLength: TAG_LENGTH_BITS },\n key,\n new TextEncoder().encode(plaintext) as BufferSource,\n ),\n )\n return `${VERSION_PREFIX}${toHex(nonce)}:${toHex(ciphertextWithTag)}`\n}\n\n/**\n * Inverse of `encryptAesGcm`. Throws on any tampering or key mismatch\n * with no partial-decrypt path. The caller decides how to\n * surface the failure (log + fail closed for storage; reject the\n * request for hot-path use).\n */\nexport async function decryptAesGcm(blob: string, keyHex: string): Promise<string> {\n if (!blob.startsWith(VERSION_PREFIX)) {\n throw new Error('decryptAesGcm: unsupported version prefix in blob')\n }\n const parts = blob.slice(VERSION_PREFIX.length).split(':')\n if (parts.length !== 2) {\n throw new Error('decryptAesGcm: malformed blob; expected nonce:ciphertext')\n }\n const [nonceHex, ciphertextHex] = parts as [string, string]\n const nonce = fromHex(nonceHex)\n const ciphertextWithTag = fromHex(ciphertextHex)\n if (nonce.length !== NONCE_LENGTH_BYTES) {\n throw new Error(`decryptAesGcm: nonce must be ${NONCE_LENGTH_BYTES} bytes`)\n }\n const key = await importKey(keyHex)\n const plaintextBytes = new Uint8Array(\n await crypto.subtle.decrypt(\n { name: ALGORITHM, iv: nonce as BufferSource, tagLength: TAG_LENGTH_BITS },\n key,\n ciphertextWithTag as BufferSource,\n ),\n )\n return new TextDecoder().decode(plaintextBytes)\n}\n\n/**\n * Convenience helper for generating a fresh 32-byte hex key. Used\n * by ops scripts that seed the env var. Returned as a hex string so\n * it can be pasted into a secrets vault without binary handling.\n */\nexport function generateAesGcmKey(): string {\n const bytes = crypto.getRandomValues(new Uint8Array(KEY_LENGTH_BYTES))\n return toHex(bytes)\n}\n\nasync function importKey(keyHex: string): Promise<CryptoKey> {\n if (!/^[0-9a-fA-F]+$/u.test(keyHex)) {\n throw new Error('encryption key must be a hex-encoded string')\n }\n const bytes = fromHex(keyHex)\n if (bytes.length !== KEY_LENGTH_BYTES) {\n throw new Error(`encryption key must be ${KEY_LENGTH_BYTES} bytes (got ${bytes.length})`)\n }\n return crypto.subtle.importKey('raw', bytes as BufferSource, { name: ALGORITHM }, false, [\n 'encrypt',\n 'decrypt',\n ])\n}\n"]}
@@ -1,56 +1,45 @@
1
1
  /**
2
2
  * AES-256-GCM authenticated symmetric encryption via Web Crypto.
3
3
  *
4
- * Used by the webhook layer to encrypt signing secrets at rest in
5
- * Postgres. Sized at the smallest interface that does the job —
6
- * encrypt one short string at a time, decrypt one short string at a
7
- * time, with the encryption key supplied as a 32-byte hex string from
8
- * environment configuration.
4
+ * Encrypt one short string at a time, decrypt one short string at a
5
+ * time. The encryption key is supplied as a 32-byte hex string by the
6
+ * caller. Suitable for envelope-encrypting small secrets where the
7
+ * key is held outside the data store.
9
8
  *
10
- * Why Web Crypto over `node:crypto`: the rest of `@strimz/shared-crypto`
11
- * targets Node 22, Vercel Edge, and Cloudflare Workers uniformly.
12
- * Keeping this consistent so the package can be imported anywhere
13
- * without conditional branches.
9
+ * AES-GCM is authenticated, so the auth tag detects bit-flips and
10
+ * substitution attacks. A tampered ciphertext fails closed at decrypt
11
+ * time. The 12-byte nonce and 16-byte tag are the canonical layout
12
+ * exposed by Web Crypto.
14
13
  *
15
- * Why AES-256-GCM:
16
- * - Authenticated. The auth tag detects bit-flips or substitution
17
- * attacks; a tampered ciphertext fails closed at decrypt time.
18
- * - Standard. Available natively in Web Crypto with no third-party
19
- * dependency.
20
- * - 12-byte nonce + 16-byte tag is the canonical layout.
21
- *
22
- * Output format (string-safe so it goes into Postgres TEXT columns
23
- * without re-encoding):
14
+ * Output format is string-safe and suitable for any text column:
24
15
  *
25
16
  * v1:<nonce-hex>:<ciphertext-and-tag-hex>
26
17
  *
27
- * Web Crypto returns the auth tag concatenated to the ciphertext,
28
- * so the on-the-wire format keeps them together rather than splitting
18
+ * Web Crypto returns the auth tag concatenated to the ciphertext, so
19
+ * the on-the-wire format keeps them together rather than splitting
29
20
  * them out. The leading `v1:` lets future schemes coexist during
30
21
  * migration.
31
22
  *
32
- * What this is NOT:
33
- * - A KMS replacement. The key lives in env. Rotating it means
34
- * rotating every encrypted value (or supporting two keys for a
35
- * cutover window). Graduate to a hardware-backed envelope when
36
- * funded — the `KmsSigner` abstraction in the API is the parallel
37
- * pattern for sign keys.
23
+ * This is not a KMS replacement. The key lives in caller-controlled
24
+ * storage. Rotating it means rotating every encrypted value, or
25
+ * supporting two keys for a cutover window. For sign keys, use a
26
+ * hardware-backed signer pattern instead.
38
27
  */
39
28
  /**
40
29
  * Encrypt a plaintext string with the supplied 32-byte hex key.
41
30
  * Returns a versioned, colon-delimited ciphertext blob safe to store
42
- * in a Postgres TEXT column.
31
+ * in any text column.
43
32
  */
44
33
  declare function encryptAesGcm(plaintext: string, keyHex: string): Promise<string>;
45
34
  /**
46
35
  * Inverse of `encryptAesGcm`. Throws on any tampering or key mismatch
47
- * — there is no partial-decrypt path. The caller decides how to
36
+ * with no partial-decrypt path. The caller decides how to
48
37
  * surface the failure (log + fail closed for storage; reject the
49
38
  * request for hot-path use).
50
39
  */
51
40
  declare function decryptAesGcm(blob: string, keyHex: string): Promise<string>;
52
41
  /**
53
- * Convenience helper for generating a fresh 32-byte hex key — used
42
+ * Convenience helper for generating a fresh 32-byte hex key. Used
54
43
  * by ops scripts that seed the env var. Returned as a hex string so
55
44
  * it can be pasted into a secrets vault without binary handling.
56
45
  */
package/dist/aes-gcm.d.ts CHANGED
@@ -1,56 +1,45 @@
1
1
  /**
2
2
  * AES-256-GCM authenticated symmetric encryption via Web Crypto.
3
3
  *
4
- * Used by the webhook layer to encrypt signing secrets at rest in
5
- * Postgres. Sized at the smallest interface that does the job —
6
- * encrypt one short string at a time, decrypt one short string at a
7
- * time, with the encryption key supplied as a 32-byte hex string from
8
- * environment configuration.
4
+ * Encrypt one short string at a time, decrypt one short string at a
5
+ * time. The encryption key is supplied as a 32-byte hex string by the
6
+ * caller. Suitable for envelope-encrypting small secrets where the
7
+ * key is held outside the data store.
9
8
  *
10
- * Why Web Crypto over `node:crypto`: the rest of `@strimz/shared-crypto`
11
- * targets Node 22, Vercel Edge, and Cloudflare Workers uniformly.
12
- * Keeping this consistent so the package can be imported anywhere
13
- * without conditional branches.
9
+ * AES-GCM is authenticated, so the auth tag detects bit-flips and
10
+ * substitution attacks. A tampered ciphertext fails closed at decrypt
11
+ * time. The 12-byte nonce and 16-byte tag are the canonical layout
12
+ * exposed by Web Crypto.
14
13
  *
15
- * Why AES-256-GCM:
16
- * - Authenticated. The auth tag detects bit-flips or substitution
17
- * attacks; a tampered ciphertext fails closed at decrypt time.
18
- * - Standard. Available natively in Web Crypto with no third-party
19
- * dependency.
20
- * - 12-byte nonce + 16-byte tag is the canonical layout.
21
- *
22
- * Output format (string-safe so it goes into Postgres TEXT columns
23
- * without re-encoding):
14
+ * Output format is string-safe and suitable for any text column:
24
15
  *
25
16
  * v1:<nonce-hex>:<ciphertext-and-tag-hex>
26
17
  *
27
- * Web Crypto returns the auth tag concatenated to the ciphertext,
28
- * so the on-the-wire format keeps them together rather than splitting
18
+ * Web Crypto returns the auth tag concatenated to the ciphertext, so
19
+ * the on-the-wire format keeps them together rather than splitting
29
20
  * them out. The leading `v1:` lets future schemes coexist during
30
21
  * migration.
31
22
  *
32
- * What this is NOT:
33
- * - A KMS replacement. The key lives in env. Rotating it means
34
- * rotating every encrypted value (or supporting two keys for a
35
- * cutover window). Graduate to a hardware-backed envelope when
36
- * funded — the `KmsSigner` abstraction in the API is the parallel
37
- * pattern for sign keys.
23
+ * This is not a KMS replacement. The key lives in caller-controlled
24
+ * storage. Rotating it means rotating every encrypted value, or
25
+ * supporting two keys for a cutover window. For sign keys, use a
26
+ * hardware-backed signer pattern instead.
38
27
  */
39
28
  /**
40
29
  * Encrypt a plaintext string with the supplied 32-byte hex key.
41
30
  * Returns a versioned, colon-delimited ciphertext blob safe to store
42
- * in a Postgres TEXT column.
31
+ * in any text column.
43
32
  */
44
33
  declare function encryptAesGcm(plaintext: string, keyHex: string): Promise<string>;
45
34
  /**
46
35
  * Inverse of `encryptAesGcm`. Throws on any tampering or key mismatch
47
- * — there is no partial-decrypt path. The caller decides how to
36
+ * with no partial-decrypt path. The caller decides how to
48
37
  * surface the failure (log + fail closed for storage; reject the
49
38
  * request for hot-path use).
50
39
  */
51
40
  declare function decryptAesGcm(blob: string, keyHex: string): Promise<string>;
52
41
  /**
53
- * Convenience helper for generating a fresh 32-byte hex key — used
42
+ * Convenience helper for generating a fresh 32-byte hex key. Used
54
43
  * by ops scripts that seed the env var. Returned as a hex string so
55
44
  * it can be pasted into a secrets vault without binary handling.
56
45
  */
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/encoding.ts","../src/aes-gcm.ts"],"names":[],"mappings":";AAMA,IAAM,SAAA,GAAY,kBAAA;AAEX,SAAS,MAAM,KAAA,EAA2B;AAC/C,EAAA,IAAI,GAAA,GAAM,EAAA;AACV,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAM,CAAA,GAAI,MAAM,CAAC,CAAA;AACjB,IAAA,GAAA,IAAO,SAAA,CAAU,MAAM,CAAC,CAAA;AACxB,IAAA,GAAA,IAAO,SAAA,CAAU,IAAI,EAAI,CAAA;AAAA,EAC3B;AACA,EAAA,OAAO,GAAA;AACT;AAEO,SAAS,QAAQ,GAAA,EAAyB;AAC/C,EAAA,MAAM,KAAA,GAAQ,IAAI,UAAA,CAAW,IAAI,IAAI,GAAA,CAAI,KAAA,CAAM,CAAC,CAAA,GAAI,GAAA;AACpD,EAAA,IAAI,KAAA,CAAM,MAAA,GAAS,CAAA,KAAM,CAAA,EAAG;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AAM,KAAK,CAAA;AACpB;AAEA,eAAe,UAAU,MAAA,EAAoC;AAC3D,EAAA,IAAI,CAAC,iBAAA,CAAkB,IAAA,CAAK,MAAM,CAAA,EAAG;AACnC,IAAA,MAAM,IAAI,MAAM,6CAA6C,CAAA;AAAA,EAC/D;AACA,EAAA,MAAM,KAAA,GAAQ,QAAQ,MAAM,CAAA;AAC5B,EAAA,IAAI,KAAA,CAAM,WAAW,gBAAA,EAAkB;AACrC,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,uBAAA,EAA0B,gBAAgB,CAAA,YAAA,EAAe,KAAA,CAAM,MAAM,CAAA,CAAA,CAAG,CAAA;AAAA,EAC1F;AACA,EAAA,OAAO,MAAA,CAAO,OAAO,SAAA,CAAU,KAAA,EAAO,OAAuB,EAAE,IAAA,EAAM,SAAA,EAAU,EAAG,KAAA,EAAO;AAAA,IACvF,SAAA;AAAA,IACA;AAAA,GACD,CAAA;AACH","file":"aes-gcm.js","sourcesContent":["/**\n * Byte encoders that work in every JavaScript runtime we target.\n * No Node `Buffer`, no dependency on the DOM — just Uint8Array, strings, and\n * the two TextEncoder/TextDecoder classes available everywhere.\n */\n\nconst HEX_CHARS = '0123456789abcdef'\n\nexport function toHex(bytes: Uint8Array): string {\n let out = ''\n for (let i = 0; i < bytes.length; i++) {\n const b = bytes[i] as number\n out += HEX_CHARS[b >>> 4]\n out += HEX_CHARS[b & 0x0f]\n }\n return out\n}\n\nexport function fromHex(hex: string): Uint8Array {\n const clean = hex.startsWith('0x') ? hex.slice(2) : hex\n if (clean.length % 2 !== 0) {\n throw new Error('fromHex: odd-length hex input')\n }\n const out = new Uint8Array(clean.length / 2)\n for (let i = 0; i < out.length; i++) {\n const byte = Number.parseInt(clean.substr(i * 2, 2), 16)\n if (Number.isNaN(byte)) {\n throw new Error('fromHex: non-hex character')\n }\n out[i] = byte\n }\n return out\n}\n\nexport function toBase64Url(bytes: Uint8Array): string {\n // Build a binary string without spreading (spread can overflow on large arrays).\n // `btoa` is global in Node 16+, all Edge runtimes, and browsers.\n let binary = ''\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i] as number)\n }\n return btoa(binary).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nexport function utf8ToBytes(str: string): Uint8Array {\n return new TextEncoder().encode(str)\n}\n\nexport function bytesToUtf8(bytes: Uint8Array): string {\n return new TextDecoder().decode(bytes)\n}\n","/**\n * AES-256-GCM authenticated symmetric encryption via Web Crypto.\n *\n * Used by the webhook layer to encrypt signing secrets at rest in\n * Postgres. Sized at the smallest interface that does the job —\n * encrypt one short string at a time, decrypt one short string at a\n * time, with the encryption key supplied as a 32-byte hex string from\n * environment configuration.\n *\n * Why Web Crypto over `node:crypto`: the rest of `@strimz/shared-crypto`\n * targets Node 22, Vercel Edge, and Cloudflare Workers uniformly.\n * Keeping this consistent so the package can be imported anywhere\n * without conditional branches.\n *\n * Why AES-256-GCM:\n * - Authenticated. The auth tag detects bit-flips or substitution\n * attacks; a tampered ciphertext fails closed at decrypt time.\n * - Standard. Available natively in Web Crypto with no third-party\n * dependency.\n * - 12-byte nonce + 16-byte tag is the canonical layout.\n *\n * Output format (string-safe so it goes into Postgres TEXT columns\n * without re-encoding):\n *\n * v1:<nonce-hex>:<ciphertext-and-tag-hex>\n *\n * Web Crypto returns the auth tag concatenated to the ciphertext,\n * so the on-the-wire format keeps them together rather than splitting\n * them out. The leading `v1:` lets future schemes coexist during\n * migration.\n *\n * What this is NOT:\n * - A KMS replacement. The key lives in env. Rotating it means\n * rotating every encrypted value (or supporting two keys for a\n * cutover window). Graduate to a hardware-backed envelope when\n * funded — the `KmsSigner` abstraction in the API is the parallel\n * pattern for sign keys.\n */\n\nimport { fromHex, toHex } from './encoding.js'\n\nconst ALGORITHM = 'AES-GCM'\nconst KEY_LENGTH_BYTES = 32\nconst NONCE_LENGTH_BYTES = 12\nconst TAG_LENGTH_BITS = 128\nconst VERSION_PREFIX = 'v1:'\n\n/**\n * Encrypt a plaintext string with the supplied 32-byte hex key.\n * Returns a versioned, colon-delimited ciphertext blob safe to store\n * in a Postgres TEXT column.\n */\nexport async function encryptAesGcm(plaintext: string, keyHex: string): Promise<string> {\n const key = await importKey(keyHex)\n const nonce = crypto.getRandomValues(new Uint8Array(NONCE_LENGTH_BYTES))\n const ciphertextWithTag = new Uint8Array(\n await crypto.subtle.encrypt(\n { name: ALGORITHM, iv: nonce as BufferSource, tagLength: TAG_LENGTH_BITS },\n key,\n new TextEncoder().encode(plaintext) as BufferSource,\n ),\n )\n return `${VERSION_PREFIX}${toHex(nonce)}:${toHex(ciphertextWithTag)}`\n}\n\n/**\n * Inverse of `encryptAesGcm`. Throws on any tampering or key mismatch\n * — there is no partial-decrypt path. The caller decides how to\n * surface the failure (log + fail closed for storage; reject the\n * request for hot-path use).\n */\nexport async function decryptAesGcm(blob: string, keyHex: string): Promise<string> {\n if (!blob.startsWith(VERSION_PREFIX)) {\n throw new Error('decryptAesGcm: unsupported version prefix in blob')\n }\n const parts = blob.slice(VERSION_PREFIX.length).split(':')\n if (parts.length !== 2) {\n throw new Error('decryptAesGcm: malformed blob; expected nonce:ciphertext')\n }\n const [nonceHex, ciphertextHex] = parts as [string, string]\n const nonce = fromHex(nonceHex)\n const ciphertextWithTag = fromHex(ciphertextHex)\n if (nonce.length !== NONCE_LENGTH_BYTES) {\n throw new Error(`decryptAesGcm: nonce must be ${NONCE_LENGTH_BYTES} bytes`)\n }\n const key = await importKey(keyHex)\n const plaintextBytes = new Uint8Array(\n await crypto.subtle.decrypt(\n { name: ALGORITHM, iv: nonce as BufferSource, tagLength: TAG_LENGTH_BITS },\n key,\n ciphertextWithTag as BufferSource,\n ),\n )\n return new TextDecoder().decode(plaintextBytes)\n}\n\n/**\n * Convenience helper for generating a fresh 32-byte hex key — used\n * by ops scripts that seed the env var. Returned as a hex string so\n * it can be pasted into a secrets vault without binary handling.\n */\nexport function generateAesGcmKey(): string {\n const bytes = crypto.getRandomValues(new Uint8Array(KEY_LENGTH_BYTES))\n return toHex(bytes)\n}\n\nasync function importKey(keyHex: string): Promise<CryptoKey> {\n if (!/^[0-9a-fA-F]+$/u.test(keyHex)) {\n throw new Error('encryption key must be a hex-encoded string')\n }\n const bytes = fromHex(keyHex)\n if (bytes.length !== KEY_LENGTH_BYTES) {\n throw new Error(`encryption key must be ${KEY_LENGTH_BYTES} bytes (got ${bytes.length})`)\n }\n return crypto.subtle.importKey('raw', bytes as BufferSource, { name: ALGORITHM }, false, [\n 'encrypt',\n 'decrypt',\n ])\n}\n"]}
1
+ {"version":3,"sources":["../src/encoding.ts","../src/aes-gcm.ts"],"names":[],"mappings":";AAMA,IAAM,SAAA,GAAY,kBAAA;AAEX,SAAS,MAAM,KAAA,EAA2B;AAC/C,EAAA,IAAI,GAAA,GAAM,EAAA;AACV,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAM,CAAA,GAAI,MAAM,CAAC,CAAA;AACjB,IAAA,GAAA,IAAO,SAAA,CAAU,MAAM,CAAC,CAAA;AACxB,IAAA,GAAA,IAAO,SAAA,CAAU,IAAI,EAAI,CAAA;AAAA,EAC3B;AACA,EAAA,OAAO,GAAA;AACT;AAEO,SAAS,QAAQ,GAAA,EAAyB;AAC/C,EAAA,MAAM,KAAA,GAAQ,IAAI,UAAA,CAAW,IAAI,IAAI,GAAA,CAAI,KAAA,CAAM,CAAC,CAAA,GAAI,GAAA;AACpD,EAAA,IAAI,KAAA,CAAM,MAAA,GAAS,CAAA,KAAM,CAAA,EAAG;AAC1B,IAAA,MAAM,IAAI,MAAM,+BAA+B,CAAA;AAAA,EACjD;AACA,EAAA,MAAM,GAAA,GAAM,IAAI,UAAA,CAAW,KAAA,CAAM,SAAS,CAAC,CAAA;AAC3C,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,GAAA,CAAI,QAAQ,CAAA,EAA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CAAA;AAC5B,EAAA,IAAI,KAAA,CAAM,WAAW,gBAAA,EAAkB;AACrC,IAAA,MAAM,IAAI,KAAA,CAAM,CAAA,uBAAA,EAA0B,gBAAgB,CAAA,YAAA,EAAe,KAAA,CAAM,MAAM,CAAA,CAAA,CAAG,CAAA;AAAA,EAC1F;AACA,EAAA,OAAO,MAAA,CAAO,OAAO,SAAA,CAAU,KAAA,EAAO,OAAuB,EAAE,IAAA,EAAM,SAAA,EAAU,EAAG,KAAA,EAAO;AAAA,IACvF,SAAA;AAAA,IACA;AAAA,GACD,CAAA;AACH","file":"aes-gcm.js","sourcesContent":["/**\n * Byte encoders that work in every JavaScript runtime we target.\n * No Node `Buffer`, no dependency on the DOM. Only Uint8Array, strings, and\n * the two TextEncoder/TextDecoder classes available everywhere.\n */\n\nconst HEX_CHARS = '0123456789abcdef'\n\nexport function toHex(bytes: Uint8Array): string {\n let out = ''\n for (let i = 0; i < bytes.length; i++) {\n const b = bytes[i] as number\n out += HEX_CHARS[b >>> 4]\n out += HEX_CHARS[b & 0x0f]\n }\n return out\n}\n\nexport function fromHex(hex: string): Uint8Array {\n const clean = hex.startsWith('0x') ? hex.slice(2) : hex\n if (clean.length % 2 !== 0) {\n throw new Error('fromHex: odd-length hex input')\n }\n const out = new Uint8Array(clean.length / 2)\n for (let i = 0; i < out.length; i++) {\n const byte = Number.parseInt(clean.substr(i * 2, 2), 16)\n if (Number.isNaN(byte)) {\n throw new Error('fromHex: non-hex character')\n }\n out[i] = byte\n }\n return out\n}\n\nexport function toBase64Url(bytes: Uint8Array): string {\n // Build a binary string without spreading (spread can overflow on large arrays).\n // `btoa` is global in Node 16+, all Edge runtimes, and browsers.\n let binary = ''\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i] as number)\n }\n return btoa(binary).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nexport function utf8ToBytes(str: string): Uint8Array {\n return new TextEncoder().encode(str)\n}\n\nexport function bytesToUtf8(bytes: Uint8Array): string {\n return new TextDecoder().decode(bytes)\n}\n","/**\n * AES-256-GCM authenticated symmetric encryption via Web Crypto.\n *\n * Encrypt one short string at a time, decrypt one short string at a\n * time. The encryption key is supplied as a 32-byte hex string by the\n * caller. Suitable for envelope-encrypting small secrets where the\n * key is held outside the data store.\n *\n * AES-GCM is authenticated, so the auth tag detects bit-flips and\n * substitution attacks. A tampered ciphertext fails closed at decrypt\n * time. The 12-byte nonce and 16-byte tag are the canonical layout\n * exposed by Web Crypto.\n *\n * Output format is string-safe and suitable for any text column:\n *\n * v1:<nonce-hex>:<ciphertext-and-tag-hex>\n *\n * Web Crypto returns the auth tag concatenated to the ciphertext, so\n * the on-the-wire format keeps them together rather than splitting\n * them out. The leading `v1:` lets future schemes coexist during\n * migration.\n *\n * This is not a KMS replacement. The key lives in caller-controlled\n * storage. Rotating it means rotating every encrypted value, or\n * supporting two keys for a cutover window. For sign keys, use a\n * hardware-backed signer pattern instead.\n */\n\nimport { fromHex, toHex } from './encoding.js'\n\nconst ALGORITHM = 'AES-GCM'\nconst KEY_LENGTH_BYTES = 32\nconst NONCE_LENGTH_BYTES = 12\nconst TAG_LENGTH_BITS = 128\nconst VERSION_PREFIX = 'v1:'\n\n/**\n * Encrypt a plaintext string with the supplied 32-byte hex key.\n * Returns a versioned, colon-delimited ciphertext blob safe to store\n * in any text column.\n */\nexport async function encryptAesGcm(plaintext: string, keyHex: string): Promise<string> {\n const key = await importKey(keyHex)\n const nonce = crypto.getRandomValues(new Uint8Array(NONCE_LENGTH_BYTES))\n const ciphertextWithTag = new Uint8Array(\n await crypto.subtle.encrypt(\n { name: ALGORITHM, iv: nonce as BufferSource, tagLength: TAG_LENGTH_BITS },\n key,\n new TextEncoder().encode(plaintext) as BufferSource,\n ),\n )\n return `${VERSION_PREFIX}${toHex(nonce)}:${toHex(ciphertextWithTag)}`\n}\n\n/**\n * Inverse of `encryptAesGcm`. Throws on any tampering or key mismatch\n * with no partial-decrypt path. The caller decides how to\n * surface the failure (log + fail closed for storage; reject the\n * request for hot-path use).\n */\nexport async function decryptAesGcm(blob: string, keyHex: string): Promise<string> {\n if (!blob.startsWith(VERSION_PREFIX)) {\n throw new Error('decryptAesGcm: unsupported version prefix in blob')\n }\n const parts = blob.slice(VERSION_PREFIX.length).split(':')\n if (parts.length !== 2) {\n throw new Error('decryptAesGcm: malformed blob; expected nonce:ciphertext')\n }\n const [nonceHex, ciphertextHex] = parts as [string, string]\n const nonce = fromHex(nonceHex)\n const ciphertextWithTag = fromHex(ciphertextHex)\n if (nonce.length !== NONCE_LENGTH_BYTES) {\n throw new Error(`decryptAesGcm: nonce must be ${NONCE_LENGTH_BYTES} bytes`)\n }\n const key = await importKey(keyHex)\n const plaintextBytes = new Uint8Array(\n await crypto.subtle.decrypt(\n { name: ALGORITHM, iv: nonce as BufferSource, tagLength: TAG_LENGTH_BITS },\n key,\n ciphertextWithTag as BufferSource,\n ),\n )\n return new TextDecoder().decode(plaintextBytes)\n}\n\n/**\n * Convenience helper for generating a fresh 32-byte hex key. Used\n * by ops scripts that seed the env var. Returned as a hex string so\n * it can be pasted into a secrets vault without binary handling.\n */\nexport function generateAesGcmKey(): string {\n const bytes = crypto.getRandomValues(new Uint8Array(KEY_LENGTH_BYTES))\n return toHex(bytes)\n}\n\nasync function importKey(keyHex: string): Promise<CryptoKey> {\n if (!/^[0-9a-fA-F]+$/u.test(keyHex)) {\n throw new Error('encryption key must be a hex-encoded string')\n }\n const bytes = fromHex(keyHex)\n if (bytes.length !== KEY_LENGTH_BYTES) {\n throw new Error(`encryption key must be ${KEY_LENGTH_BYTES} bytes (got ${bytes.length})`)\n }\n return crypto.subtle.importKey('raw', bytes as BufferSource, { name: ALGORITHM }, false, [\n 'encrypt',\n 'decrypt',\n ])\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/encoding.ts","../src/hash.ts","../src/random.ts","../src/api-key.ts"],"names":["API_KEY_PREFIXES","API_KEY_RANDOM_BYTES","API_KEY_P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M,CAAA,CAAA;AACpC,EAAA,MAAM,IAAA,GAAO,MAAM,SAAA,CAAU,MAAM,CAAA;AACnC,EAAA,OAAO;AAAA,IACL,MAAA;AAAA,IACA,IAAA;AAAA,IACA,MAAA,EAAQ,MAAA,CAAO,KAAA,CAAM,CAAA,EAAGC,qCAA6B,CAAA;AAAA,IACrD,QAAA,EAAU,MAAA,CAAO,KAAA,CAAM,EAAE,CAAA;AAAA,IACzB,IAAA;AAAA,IACA;AAAA,GACF;AACF;AAEA,eAAsB,WAAW,MAAA,EAAiC;AAChE,EAAA,OAAO,UAAU,MAAM,CAAA;AACzB;AAEO,SAAS,aAAa,MAAA,EAAwB;AACnD,EAAA,IAAI,MAAA,CAAO,MAAA,IAAUA,qCAAA,GAAgC,CAAA,EAAG;AACtD,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,OAAO,CAAA,EAAG,MAAA,CAAO,KAAA,CAAM,CAAA,EAAGA,qCAA6B,CAAC,CAAA,GAAA,EAAM,MAAA,CAAO,KAAA,CAAM,EAAE,CAAC,CAAA,CAAA;AAChF","file":"api-key.cjs","sourcesContent":["/**\n * Byte encoders that work in every JavaScript runtime we target.\n * No Node `Buffer`, no dependency on the DOM — just Uint8Array, strings, and\n * the two TextEncoder/TextDecoder classes available everywhere.\n */\n\nconst HEX_CHARS = '0123456789abcdef'\n\nexport function toHex(bytes: Uint8Array): string {\n let out = ''\n for (let i = 0; i < bytes.length; i++) {\n const b = bytes[i] as number\n out += HEX_CHARS[b >>> 4]\n out += HEX_CHARS[b & 0x0f]\n }\n return out\n}\n\nexport function fromHex(hex: string): Uint8Array {\n const clean = hex.startsWith('0x') ? hex.slice(2) : hex\n if (clean.length % 2 !== 0) {\n throw new Error('fromHex: odd-length hex input')\n }\n const out = new Uint8Array(clean.length / 2)\n for (let i = 0; i < out.length; i++) {\n const byte = Number.parseInt(clean.substr(i * 2, 2), 16)\n if (Number.isNaN(byte)) {\n throw new Error('fromHex: non-hex character')\n }\n out[i] = byte\n }\n return out\n}\n\nexport function toBase64Url(bytes: Uint8Array): string {\n // Build a binary string without spreading (spread can overflow on large arrays).\n // `btoa` is global in Node 16+, all Edge runtimes, and browsers.\n let binary = ''\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i] as number)\n }\n return btoa(binary).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nexport function utf8ToBytes(str: string): Uint8Array {\n return new TextEncoder().encode(str)\n}\n\nexport function bytesToUtf8(bytes: Uint8Array): string {\n return new TextDecoder().decode(bytes)\n}\n","/**\n * SHA-256 via Web Crypto (available in Node 16+, Deno, Bun, Workers, Edge).\n */\n\nimport { toHex, utf8ToBytes } from './encoding.js'\n\nexport async function sha256(data: string | Uint8Array): Promise<Uint8Array> {\n const bytes = typeof data === 'string' ? utf8ToBytes(data) : data\n const digest = await subtle().digest('SHA-256', bytes as BufferSource)\n return new Uint8Array(digest)\n}\n\nexport async function sha256Hex(data: string | Uint8Array): Promise<string> {\n return toHex(await sha256(data))\n}\n\nfunction subtle(): SubtleCrypto {\n const c = globalThis.crypto\n if (!c || !c.subtle) {\n throw new Error('Web Crypto is not available in this runtime')\n }\n return c.subtle\n}\n","/**\n * Random-bytes and id generation using the Web Crypto RNG\n * (cryptographically secure in every runtime we target).\n */\n\nimport { toBase64Url, toHex } from './encoding.js'\n\nexport function randomBytes(n: number): Uint8Array {\n if (!Number.isInteger(n) || n <= 0) {\n throw new Error('randomBytes: n must be a positive integer')\n }\n const buf = new Uint8Array(n)\n getRandomValues(buf)\n return buf\n}\n\nexport function randomHex(nBytes: number): string {\n return toHex(randomBytes(nBytes))\n}\n\nexport function randomBase64Url(nBytes: number): string {\n return toBase64Url(randomBytes(nBytes))\n}\n\nexport function uuid(): string {\n const c = globalThis.crypto\n if (c && typeof c.randomUUID === 'function') {\n return c.randomUUID()\n }\n // Fallback — RFC 4122 v4 from getRandomValues\n const bytes = randomBytes(16)\n bytes[6] = ((bytes[6] as number) & 0x0f) | 0x40\n bytes[8] = ((bytes[8] as number) & 0x3f) | 0x80\n const hex = toHex(bytes)\n return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`\n}\n\nfunction getRandomValues(buf: Uint8Array): void {\n const c = globalThis.crypto\n if (!c || typeof c.getRandomValues !== 'function') {\n throw new Error('crypto.getRandomValues is not available in this runtime')\n }\n c.getRandomValues(buf)\n}\n","/**\n * API key generation and indexing.\n *\n * A key is `<prefix><random>` where random is base64url-encoded with 32 bytes\n * of entropy (256 bits). The backend stores:\n * - prefix (for display)\n * - last 4 chars (for display)\n * - sha256(key) in lowercase hex (for O(1) lookup on every request)\n *\n * No salt is needed: the random portion has 256 bits of entropy and brute-\n * forcing SHA-256 preimages of uniformly random 256-bit values is infeasible.\n */\n\nimport {\n API_KEY_PREFIXES,\n API_KEY_RANDOM_BYTES,\n API_KEY_PREFIX_DISPLAY_LENGTH,\n type ApiKeyKind,\n type ApiKeyMode,\n} from '@strimz/shared-config/api-keys'\nimport { sha256Hex } from './hash.js'\nimport { randomBase64Url } from './random.js'\n\nexport interface GeneratedApiKey {\n /** The full plaintext key — display once then discard. */\n secret: string\n /** Stored as the index. */\n hash: string\n /** Stored for display / log redaction. */\n prefix: string\n /** Stored for display — \"my key ending in ...abcd\". */\n lastFour: string\n kind: ApiKeyKind\n mode: ApiKeyMode\n}\n\nexport async function generateApiKey(kind: ApiKeyKind, mode: ApiKeyMode): Promise<GeneratedApiKey> {\n const prefixStr = API_KEY_PREFIXES[kind][mode]\n const random = randomBase64Url(API_KEY_RANDOM_BYTES)\n const secret = `${prefixStr}${random}`\n const hash = await sha256Hex(secret)\n return {\n secret,\n hash,\n prefix: secret.slice(0, API_KEY_PREFIX_DISPLAY_LENGTH),\n lastFour: secret.slice(-4),\n kind,\n mode,\n }\n}\n\nexport async function hashApiKey(secret: string): Promise<string> {\n return sha256Hex(secret)\n}\n\nexport function redactApiKey(secret: string): string {\n if (secret.length <= API_KEY_PREFIX_DISPLAY_LENGTH + 4) {\n return '***'\n }\n return `${secret.slice(0, API_KEY_PREFIX_DISPLAY_LENGTH)}...${secret.slice(-4)}`\n}\n"]}
1
+ {"version":3,"sources":["../src/encoding.ts","../src/hash.ts","../src/random.ts","../src/api-key.ts"],"names":["API_KEY_PREFIXES","API_KEY_RANDOM_BYTES","API_KEY_PREFIX_DISPLAY_LENGTH"],"mappings":";;;;;;;AAMA,IAAM,SAAA,GAAY,kBAAA;AAEX,SAAS,MAAM,KAAA,EAA2B;AAC/C,EAAA,IAAI,GAAA,GAAM,EAAA;AACV,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAM,CAAA,GAAI,MAAM,CAAC,CAAA;AACjB,IAAA,GAAA,IAAO,SAAA,CAAU,MAAM,CAAC,CAAA;AACxB,IAAA,GAAA,IAAO,SAAA,CAAU,IAAI,EAAI,CAAA;AAAA,EAC3B;AACA,EAAA,OAAO,GAAA;AACT;AAkBO,SAAS,YAAY,KAAA,EAA2B;AAGrD,EAAA,IAAI,MAAA,GAAS,EAAA;AACb,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAA,IAAU,MAAA,CAAO,YAAA,CAAa,KAAA,CAAM,CAAC,CAAW,CAAA;AAAA,EAClD;AACA,EAAA,OAAO,IAAA,CAAK,MAAM,CAAA,CAAE,OAAA,CAAQ,KAAA,EAAO,GAAG,CAAA,CAAE,OAAA,CAAQ,KAAA,EAAO,GAAG,CAAA,CAAE,OAAA,CAAQ,OAAO,EAAE,CAAA;AAC/E;AAEO,SAAS,YAAY,GAAA,EAAyB;AACnD,EAAA,OAAO,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,GAAG,CAAA;AACrC;;;ACxCA,eAAsB,OAAO,IA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;;;ACPA,eAAsB,cAAA,CAAe,MAAkB,IAAA,EAA4C;AACjG,EAAA,MAAM,SAAA,GAAYA,wBAAA,CAAiB,IAAI,CAAA,CAAE,IAAI,CAAA;AAC7C,EAAA,MAAM,MAAA,GAAS,gBAAgBC,4BAAoB,CAAA;AACnD,EAAA,MAAM,MAAA,GAAS,CAAA,EAAG,SAAS,CAAA,EAAG,MAAM,CAAA,CAAA;AACpC,EAAA,MAAM,IAAA,GAAO,MAAM,SAAA,CAAU,MAAM,CAAA;AACnC,EAAA,OAAO;AAAA,IACL,MAAA;AAAA,IACA,IAAA;AAAA,IACA,MAAA,EAAQ,MAAA,CAAO,KAAA,CAAM,CAAA,EAAGC,qCAA6B,CAAA;AAAA,IACrD,QAAA,EAAU,MAAA,CAAO,KAAA,CAAM,EAAE,CAAA;AAAA,IACzB,IAAA;AAAA,IACA;AAAA,GACF;AACF;AAEA,eAAsB,WAAW,MAAA,EAAiC;AAChE,EAAA,OAAO,UAAU,MAAM,CAAA;AACzB;AAEO,SAAS,aAAa,MAAA,EAAwB;AACnD,EAAA,IAAI,MAAA,CAAO,MAAA,IAAUA,qCAAA,GAAgC,CAAA,EAAG;AACtD,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,OAAO,CAAA,EAAG,MAAA,CAAO,KAAA,CAAM,CAAA,EAAGA,qCAA6B,CAAC,CAAA,GAAA,EAAM,MAAA,CAAO,KAAA,CAAM,EAAE,CAAC,CAAA,CAAA;AAChF","file":"api-key.cjs","sourcesContent":["/**\n * Byte encoders that work in every JavaScript runtime we target.\n * No Node `Buffer`, no dependency on the DOM. Only Uint8Array, strings, and\n * the two TextEncoder/TextDecoder classes available everywhere.\n */\n\nconst HEX_CHARS = '0123456789abcdef'\n\nexport function toHex(bytes: Uint8Array): string {\n let out = ''\n for (let i = 0; i < bytes.length; i++) {\n const b = bytes[i] as number\n out += HEX_CHARS[b >>> 4]\n out += HEX_CHARS[b & 0x0f]\n }\n return out\n}\n\nexport function fromHex(hex: string): Uint8Array {\n const clean = hex.startsWith('0x') ? hex.slice(2) : hex\n if (clean.length % 2 !== 0) {\n throw new Error('fromHex: odd-length hex input')\n }\n const out = new Uint8Array(clean.length / 2)\n for (let i = 0; i < out.length; i++) {\n const byte = Number.parseInt(clean.substr(i * 2, 2), 16)\n if (Number.isNaN(byte)) {\n throw new Error('fromHex: non-hex character')\n }\n out[i] = byte\n }\n return out\n}\n\nexport function toBase64Url(bytes: Uint8Array): string {\n // Build a binary string without spreading (spread can overflow on large arrays).\n // `btoa` is global in Node 16+, all Edge runtimes, and browsers.\n let binary = ''\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i] as number)\n }\n return btoa(binary).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nexport function utf8ToBytes(str: string): Uint8Array {\n return new TextEncoder().encode(str)\n}\n\nexport function bytesToUtf8(bytes: Uint8Array): string {\n return new TextDecoder().decode(bytes)\n}\n","/**\n * SHA-256 via Web Crypto (available in Node 16+, Deno, Bun, Workers, Edge).\n */\n\nimport { toHex, utf8ToBytes } from './encoding.js'\n\nexport async function sha256(data: string | Uint8Array): Promise<Uint8Array> {\n const bytes = typeof data === 'string' ? utf8ToBytes(data) : data\n const digest = await subtle().digest('SHA-256', bytes as BufferSource)\n return new Uint8Array(digest)\n}\n\nexport async function sha256Hex(data: string | Uint8Array): Promise<string> {\n return toHex(await sha256(data))\n}\n\nfunction subtle(): SubtleCrypto {\n const c = globalThis.crypto\n if (!c || !c.subtle) {\n throw new Error('Web Crypto is not available in this runtime')\n }\n return c.subtle\n}\n","/**\n * Random-bytes and id generation using the Web Crypto RNG\n * (cryptographically secure in every runtime we target).\n */\n\nimport { toBase64Url, toHex } from './encoding.js'\n\nexport function randomBytes(n: number): Uint8Array {\n if (!Number.isInteger(n) || n <= 0) {\n throw new Error('randomBytes: n must be a positive integer')\n }\n const buf = new Uint8Array(n)\n getRandomValues(buf)\n return buf\n}\n\nexport function randomHex(nBytes: number): string {\n return toHex(randomBytes(nBytes))\n}\n\nexport function randomBase64Url(nBytes: number): string {\n return toBase64Url(randomBytes(nBytes))\n}\n\nexport function uuid(): string {\n const c = globalThis.crypto\n if (c && typeof c.randomUUID === 'function') {\n return c.randomUUID()\n }\n // Fallback. RFC 4122 v4 from getRandomValues.\n const bytes = randomBytes(16)\n bytes[6] = ((bytes[6] as number) & 0x0f) | 0x40\n bytes[8] = ((bytes[8] as number) & 0x3f) | 0x80\n const hex = toHex(bytes)\n return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`\n}\n\nfunction getRandomValues(buf: Uint8Array): void {\n const c = globalThis.crypto\n if (!c || typeof c.getRandomValues !== 'function') {\n throw new Error('crypto.getRandomValues is not available in this runtime')\n }\n c.getRandomValues(buf)\n}\n","/**\n * API key generation and indexing.\n *\n * A key is `<prefix><random>` where random is base64url-encoded with 32 bytes\n * of entropy (256 bits). The backend stores:\n * - prefix (for display)\n * - last 4 chars (for display)\n * - sha256(key) in lowercase hex (for O(1) lookup on every request)\n *\n * No salt is needed: the random portion has 256 bits of entropy and brute-\n * forcing SHA-256 preimages of uniformly random 256-bit values is infeasible.\n */\n\nimport {\n API_KEY_PREFIXES,\n API_KEY_RANDOM_BYTES,\n API_KEY_PREFIX_DISPLAY_LENGTH,\n type ApiKeyKind,\n type ApiKeyMode,\n} from '@strimz/shared-config/api-keys'\nimport { sha256Hex } from './hash.js'\nimport { randomBase64Url } from './random.js'\n\nexport interface GeneratedApiKey {\n /** The full plaintext key. Display once then discard. */\n secret: string\n /** Stored as the index. */\n hash: string\n /** Stored for display / log redaction. */\n prefix: string\n /** Stored for display, e.g. \"my key ending in ...abcd\". */\n lastFour: string\n kind: ApiKeyKind\n mode: ApiKeyMode\n}\n\nexport async function generateApiKey(kind: ApiKeyKind, mode: ApiKeyMode): Promise<GeneratedApiKey> {\n const prefixStr = API_KEY_PREFIXES[kind][mode]\n const random = randomBase64Url(API_KEY_RANDOM_BYTES)\n const secret = `${prefixStr}${random}`\n const hash = await sha256Hex(secret)\n return {\n secret,\n hash,\n prefix: secret.slice(0, API_KEY_PREFIX_DISPLAY_LENGTH),\n lastFour: secret.slice(-4),\n kind,\n mode,\n }\n}\n\nexport async function hashApiKey(secret: string): Promise<string> {\n return sha256Hex(secret)\n}\n\nexport function redactApiKey(secret: string): string {\n if (secret.length <= API_KEY_PREFIX_DISPLAY_LENGTH + 4) {\n return '***'\n }\n return `${secret.slice(0, API_KEY_PREFIX_DISPLAY_LENGTH)}...${secret.slice(-4)}`\n}\n"]}
@@ -14,13 +14,13 @@ import { ApiKeyKind, ApiKeyMode } from '@strimz/shared-config/api-keys';
14
14
  */
15
15
 
16
16
  interface GeneratedApiKey {
17
- /** The full plaintext key — display once then discard. */
17
+ /** The full plaintext key. Display once then discard. */
18
18
  secret: string;
19
19
  /** Stored as the index. */
20
20
  hash: string;
21
21
  /** Stored for display / log redaction. */
22
22
  prefix: string;
23
- /** Stored for display — "my key ending in ...abcd". */
23
+ /** Stored for display, e.g. "my key ending in ...abcd". */
24
24
  lastFour: string;
25
25
  kind: ApiKeyKind;
26
26
  mode: ApiKeyMode;
package/dist/api-key.d.ts CHANGED
@@ -14,13 +14,13 @@ import { ApiKeyKind, ApiKeyMode } from '@strimz/shared-config/api-keys';
14
14
  */
15
15
 
16
16
  interface GeneratedApiKey {
17
- /** The full plaintext key — display once then discard. */
17
+ /** The full plaintext key. Display once then discard. */
18
18
  secret: string;
19
19
  /** Stored as the index. */
20
20
  hash: string;
21
21
  /** Stored for display / log redaction. */
22
22
  prefix: string;
23
- /** Stored for display — "my key ending in ...abcd". */
23
+ /** Stored for display, e.g. "my key ending in ...abcd". */
24
24
  lastFour: string;
25
25
  kind: ApiKeyKind;
26
26
  mode: ApiKeyMode;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/encoding.ts","../src/hash.ts","../src/random.ts","../src/api-key.ts"],"names":[],"mappings":";;;;;A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AO,MAAM,SAAA,CAAU,MAAM,CAAA;AACnC,EAAA,OAAO;AAAA,IACL,MAAA;AAAA,IACA,IAAA;AAAA,IACA,MAAA,EAAQ,MAAA,CAAO,KAAA,CAAM,CAAA,EAAG,6BAA6B,CAAA;AAAA,IACrD,QAAA,EAAU,MAAA,CAAO,KAAA,CAAM,EAAE,CAAA;AAAA,IACzB,IAAA;AAAA,IACA;AAAA,GACF;AACF;AAEA,eAAsB,WAAW,MAAA,EAAiC;AAChE,EAAA,OAAO,UAAU,MAAM,CAAA;AACzB;AAEO,SAAS,aAAa,MAAA,EAAwB;AACnD,EAAA,IAAI,MAAA,CAAO,MAAA,IAAU,6BAAA,GAAgC,CAAA,EAAG;AACtD,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,OAAO,CAAA,EAAG,MAAA,CAAO,KAAA,CAAM,CAAA,EAAG,6BAA6B,CAAC,CAAA,GAAA,EAAM,MAAA,CAAO,KAAA,CAAM,EAAE,CAAC,CAAA,CAAA;AAChF","file":"api-key.js","sourcesContent":["/**\n * Byte encoders that work in every JavaScript runtime we target.\n * No Node `Buffer`, no dependency on the DOM — just Uint8Array, strings, and\n * the two TextEncoder/TextDecoder classes available everywhere.\n */\n\nconst HEX_CHARS = '0123456789abcdef'\n\nexport function toHex(bytes: Uint8Array): string {\n let out = ''\n for (let i = 0; i < bytes.length; i++) {\n const b = bytes[i] as number\n out += HEX_CHARS[b >>> 4]\n out += HEX_CHARS[b & 0x0f]\n }\n return out\n}\n\nexport function fromHex(hex: string): Uint8Array {\n const clean = hex.startsWith('0x') ? hex.slice(2) : hex\n if (clean.length % 2 !== 0) {\n throw new Error('fromHex: odd-length hex input')\n }\n const out = new Uint8Array(clean.length / 2)\n for (let i = 0; i < out.length; i++) {\n const byte = Number.parseInt(clean.substr(i * 2, 2), 16)\n if (Number.isNaN(byte)) {\n throw new Error('fromHex: non-hex character')\n }\n out[i] = byte\n }\n return out\n}\n\nexport function toBase64Url(bytes: Uint8Array): string {\n // Build a binary string without spreading (spread can overflow on large arrays).\n // `btoa` is global in Node 16+, all Edge runtimes, and browsers.\n let binary = ''\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i] as number)\n }\n return btoa(binary).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nexport function utf8ToBytes(str: string): Uint8Array {\n return new TextEncoder().encode(str)\n}\n\nexport function bytesToUtf8(bytes: Uint8Array): string {\n return new TextDecoder().decode(bytes)\n}\n","/**\n * SHA-256 via Web Crypto (available in Node 16+, Deno, Bun, Workers, Edge).\n */\n\nimport { toHex, utf8ToBytes } from './encoding.js'\n\nexport async function sha256(data: string | Uint8Array): Promise<Uint8Array> {\n const bytes = typeof data === 'string' ? utf8ToBytes(data) : data\n const digest = await subtle().digest('SHA-256', bytes as BufferSource)\n return new Uint8Array(digest)\n}\n\nexport async function sha256Hex(data: string | Uint8Array): Promise<string> {\n return toHex(await sha256(data))\n}\n\nfunction subtle(): SubtleCrypto {\n const c = globalThis.crypto\n if (!c || !c.subtle) {\n throw new Error('Web Crypto is not available in this runtime')\n }\n return c.subtle\n}\n","/**\n * Random-bytes and id generation using the Web Crypto RNG\n * (cryptographically secure in every runtime we target).\n */\n\nimport { toBase64Url, toHex } from './encoding.js'\n\nexport function randomBytes(n: number): Uint8Array {\n if (!Number.isInteger(n) || n <= 0) {\n throw new Error('randomBytes: n must be a positive integer')\n }\n const buf = new Uint8Array(n)\n getRandomValues(buf)\n return buf\n}\n\nexport function randomHex(nBytes: number): string {\n return toHex(randomBytes(nBytes))\n}\n\nexport function randomBase64Url(nBytes: number): string {\n return toBase64Url(randomBytes(nBytes))\n}\n\nexport function uuid(): string {\n const c = globalThis.crypto\n if (c && typeof c.randomUUID === 'function') {\n return c.randomUUID()\n }\n // Fallback — RFC 4122 v4 from getRandomValues\n const bytes = randomBytes(16)\n bytes[6] = ((bytes[6] as number) & 0x0f) | 0x40\n bytes[8] = ((bytes[8] as number) & 0x3f) | 0x80\n const hex = toHex(bytes)\n return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`\n}\n\nfunction getRandomValues(buf: Uint8Array): void {\n const c = globalThis.crypto\n if (!c || typeof c.getRandomValues !== 'function') {\n throw new Error('crypto.getRandomValues is not available in this runtime')\n }\n c.getRandomValues(buf)\n}\n","/**\n * API key generation and indexing.\n *\n * A key is `<prefix><random>` where random is base64url-encoded with 32 bytes\n * of entropy (256 bits). The backend stores:\n * - prefix (for display)\n * - last 4 chars (for display)\n * - sha256(key) in lowercase hex (for O(1) lookup on every request)\n *\n * No salt is needed: the random portion has 256 bits of entropy and brute-\n * forcing SHA-256 preimages of uniformly random 256-bit values is infeasible.\n */\n\nimport {\n API_KEY_PREFIXES,\n API_KEY_RANDOM_BYTES,\n API_KEY_PREFIX_DISPLAY_LENGTH,\n type ApiKeyKind,\n type ApiKeyMode,\n} from '@strimz/shared-config/api-keys'\nimport { sha256Hex } from './hash.js'\nimport { randomBase64Url } from './random.js'\n\nexport interface GeneratedApiKey {\n /** The full plaintext key — display once then discard. */\n secret: string\n /** Stored as the index. */\n hash: string\n /** Stored for display / log redaction. */\n prefix: string\n /** Stored for display — \"my key ending in ...abcd\". */\n lastFour: string\n kind: ApiKeyKind\n mode: ApiKeyMode\n}\n\nexport async function generateApiKey(kind: ApiKeyKind, mode: ApiKeyMode): Promise<GeneratedApiKey> {\n const prefixStr = API_KEY_PREFIXES[kind][mode]\n const random = randomBase64Url(API_KEY_RANDOM_BYTES)\n const secret = `${prefixStr}${random}`\n const hash = await sha256Hex(secret)\n return {\n secret,\n hash,\n prefix: secret.slice(0, API_KEY_PREFIX_DISPLAY_LENGTH),\n lastFour: secret.slice(-4),\n kind,\n mode,\n }\n}\n\nexport async function hashApiKey(secret: string): Promise<string> {\n return sha256Hex(secret)\n}\n\nexport function redactApiKey(secret: string): string {\n if (secret.length <= API_KEY_PREFIX_DISPLAY_LENGTH + 4) {\n return '***'\n }\n return `${secret.slice(0, API_KEY_PREFIX_DISPLAY_LENGTH)}...${secret.slice(-4)}`\n}\n"]}
1
+ {"version":3,"sources":["../src/encoding.ts","../src/hash.ts","../src/random.ts","../src/api-key.ts"],"names":[],"mappings":";;;;;AAMA,IAAM,SAAA,GAAY,kBAAA;AAEX,SAAS,MAAM,KAAA,EAA2B;AAC/C,EAAA,IAAI,GAAA,GAAM,EAAA;AACV,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAM,CAAA,GAAI,MAAM,CAAC,CAAA;AACjB,IAAA,GAAA,IAAO,SAAA,CAAU,MAAM,CAAC,CAAA;AACxB,IAAA,GAAA,IAAO,SAAA,CAAU,IAAI,EAAI,CAAA;AAAA,EAC3B;AACA,EAAA,OAAO,GAAA;AACT;AAkBO,SAAS,YAAY,KAAA,EAA2B;AAGrD,EAAA,IAAI,MAAA,GAAS,EAAA;AACb,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAA,IAAU,MAAA,CAAO,YAAA,CAAa,KAAA,CAAM,CAAC,CAAW,CAAA;AAAA,EAClD;AACA,EAAA,OAAO,IAAA,CAAK,MAAM,CAAA,CAAE,OAAA,CAAQ,KAAA,EAAO,GAAG,CAAA,CAAE,OAAA,CAAQ,KAAA,EAAO,GAAG,CAAA,CAAE,OAAA,CAAQ,OAAO,EAAE,CAAA;AAC/E;AAEO,SAAS,YAAY,GAAA,EAAyB;AACnD,EAAA,OAAO,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,GAAG,CAAA;AACrC;;;ACxCA,eAAsB,OAAO,IAAA,EAAgD;AAC3E,EAAA,MAAM,QAAQ,OAAO,IAAA,KAAS,QAAA,GAAW,WAAA,CAAY,IAAI,CAAA,GAAI,IAAA;AAC7D,EAAA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I,CAAA;AAC7C,EAAA,MAAM,MAAA,GAAS,gBAAgB,oBAAoB,CAAA;AACnD,EAAA,MAAM,MAAA,GAAS,CAAA,EAAG,SAAS,CAAA,EAAG,MAAM,CAAA,CAAA;AACpC,EAAA,MAAM,IAAA,GAAO,MAAM,SAAA,CAAU,MAAM,CAAA;AACnC,EAAA,OAAO;AAAA,IACL,MAAA;AAAA,IACA,IAAA;AAAA,IACA,MAAA,EAAQ,MAAA,CAAO,KAAA,CAAM,CAAA,EAAG,6BAA6B,CAAA;AAAA,IACrD,QAAA,EAAU,MAAA,CAAO,KAAA,CAAM,EAAE,CAAA;AAAA,IACzB,IAAA;AAAA,IACA;AAAA,GACF;AACF;AAEA,eAAsB,WAAW,MAAA,EAAiC;AAChE,EAAA,OAAO,UAAU,MAAM,CAAA;AACzB;AAEO,SAAS,aAAa,MAAA,EAAwB;AACnD,EAAA,IAAI,MAAA,CAAO,MAAA,IAAU,6BAAA,GAAgC,CAAA,EAAG;AACtD,IAAA,OAAO,KAAA;AAAA,EACT;AACA,EAAA,OAAO,CAAA,EAAG,MAAA,CAAO,KAAA,CAAM,CAAA,EAAG,6BAA6B,CAAC,CAAA,GAAA,EAAM,MAAA,CAAO,KAAA,CAAM,EAAE,CAAC,CAAA,CAAA;AAChF","file":"api-key.js","sourcesContent":["/**\n * Byte encoders that work in every JavaScript runtime we target.\n * No Node `Buffer`, no dependency on the DOM. Only Uint8Array, strings, and\n * the two TextEncoder/TextDecoder classes available everywhere.\n */\n\nconst HEX_CHARS = '0123456789abcdef'\n\nexport function toHex(bytes: Uint8Array): string {\n let out = ''\n for (let i = 0; i < bytes.length; i++) {\n const b = bytes[i] as number\n out += HEX_CHARS[b >>> 4]\n out += HEX_CHARS[b & 0x0f]\n }\n return out\n}\n\nexport function fromHex(hex: string): Uint8Array {\n const clean = hex.startsWith('0x') ? hex.slice(2) : hex\n if (clean.length % 2 !== 0) {\n throw new Error('fromHex: odd-length hex input')\n }\n const out = new Uint8Array(clean.length / 2)\n for (let i = 0; i < out.length; i++) {\n const byte = Number.parseInt(clean.substr(i * 2, 2), 16)\n if (Number.isNaN(byte)) {\n throw new Error('fromHex: non-hex character')\n }\n out[i] = byte\n }\n return out\n}\n\nexport function toBase64Url(bytes: Uint8Array): string {\n // Build a binary string without spreading (spread can overflow on large arrays).\n // `btoa` is global in Node 16+, all Edge runtimes, and browsers.\n let binary = ''\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i] as number)\n }\n return btoa(binary).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nexport function utf8ToBytes(str: string): Uint8Array {\n return new TextEncoder().encode(str)\n}\n\nexport function bytesToUtf8(bytes: Uint8Array): string {\n return new TextDecoder().decode(bytes)\n}\n","/**\n * SHA-256 via Web Crypto (available in Node 16+, Deno, Bun, Workers, Edge).\n */\n\nimport { toHex, utf8ToBytes } from './encoding.js'\n\nexport async function sha256(data: string | Uint8Array): Promise<Uint8Array> {\n const bytes = typeof data === 'string' ? utf8ToBytes(data) : data\n const digest = await subtle().digest('SHA-256', bytes as BufferSource)\n return new Uint8Array(digest)\n}\n\nexport async function sha256Hex(data: string | Uint8Array): Promise<string> {\n return toHex(await sha256(data))\n}\n\nfunction subtle(): SubtleCrypto {\n const c = globalThis.crypto\n if (!c || !c.subtle) {\n throw new Error('Web Crypto is not available in this runtime')\n }\n return c.subtle\n}\n","/**\n * Random-bytes and id generation using the Web Crypto RNG\n * (cryptographically secure in every runtime we target).\n */\n\nimport { toBase64Url, toHex } from './encoding.js'\n\nexport function randomBytes(n: number): Uint8Array {\n if (!Number.isInteger(n) || n <= 0) {\n throw new Error('randomBytes: n must be a positive integer')\n }\n const buf = new Uint8Array(n)\n getRandomValues(buf)\n return buf\n}\n\nexport function randomHex(nBytes: number): string {\n return toHex(randomBytes(nBytes))\n}\n\nexport function randomBase64Url(nBytes: number): string {\n return toBase64Url(randomBytes(nBytes))\n}\n\nexport function uuid(): string {\n const c = globalThis.crypto\n if (c && typeof c.randomUUID === 'function') {\n return c.randomUUID()\n }\n // Fallback. RFC 4122 v4 from getRandomValues.\n const bytes = randomBytes(16)\n bytes[6] = ((bytes[6] as number) & 0x0f) | 0x40\n bytes[8] = ((bytes[8] as number) & 0x3f) | 0x80\n const hex = toHex(bytes)\n return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`\n}\n\nfunction getRandomValues(buf: Uint8Array): void {\n const c = globalThis.crypto\n if (!c || typeof c.getRandomValues !== 'function') {\n throw new Error('crypto.getRandomValues is not available in this runtime')\n }\n c.getRandomValues(buf)\n}\n","/**\n * API key generation and indexing.\n *\n * A key is `<prefix><random>` where random is base64url-encoded with 32 bytes\n * of entropy (256 bits). The backend stores:\n * - prefix (for display)\n * - last 4 chars (for display)\n * - sha256(key) in lowercase hex (for O(1) lookup on every request)\n *\n * No salt is needed: the random portion has 256 bits of entropy and brute-\n * forcing SHA-256 preimages of uniformly random 256-bit values is infeasible.\n */\n\nimport {\n API_KEY_PREFIXES,\n API_KEY_RANDOM_BYTES,\n API_KEY_PREFIX_DISPLAY_LENGTH,\n type ApiKeyKind,\n type ApiKeyMode,\n} from '@strimz/shared-config/api-keys'\nimport { sha256Hex } from './hash.js'\nimport { randomBase64Url } from './random.js'\n\nexport interface GeneratedApiKey {\n /** The full plaintext key. Display once then discard. */\n secret: string\n /** Stored as the index. */\n hash: string\n /** Stored for display / log redaction. */\n prefix: string\n /** Stored for display, e.g. \"my key ending in ...abcd\". */\n lastFour: string\n kind: ApiKeyKind\n mode: ApiKeyMode\n}\n\nexport async function generateApiKey(kind: ApiKeyKind, mode: ApiKeyMode): Promise<GeneratedApiKey> {\n const prefixStr = API_KEY_PREFIXES[kind][mode]\n const random = randomBase64Url(API_KEY_RANDOM_BYTES)\n const secret = `${prefixStr}${random}`\n const hash = await sha256Hex(secret)\n return {\n secret,\n hash,\n prefix: secret.slice(0, API_KEY_PREFIX_DISPLAY_LENGTH),\n lastFour: secret.slice(-4),\n kind,\n mode,\n }\n}\n\nexport async function hashApiKey(secret: string): Promise<string> {\n return sha256Hex(secret)\n}\n\nexport function redactApiKey(secret: string): string {\n if (secret.length <= API_KEY_PREFIX_DISPLAY_LENGTH + 4) {\n return '***'\n }\n return `${secret.slice(0, API_KEY_PREFIX_DISPLAY_LENGTH)}...${secret.slice(-4)}`\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/encoding.ts"],"names":[],"mappings":";;;AAMA,IAAM,SAAA,GAAY,kBAAA;AAEX,SAAS,MAAM,KAAA,EAA2B;AAC/C,EAAA,IAAI,GAAA,GAAM,EAAA;AACV,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAM,CAAA,GAAI,MAAM,CAAC,CAAA;AACjB,IAAA,GAAA,IAAO,SAAA,CAAU,MAAM,CAAC,CAAA;AACxB,IAAA,GAAA,IAAO,SAAA,CAAU,IAAI,EAAI,CAAA;AAAA,EAC3B;AACA,EAAA,OAAO,GAAA;AACT;AAEO,SAAS,QAAQ,GAAA,EAAyB;AAC/C,EAAA,MAAM,KAAA,GAAQ,IAAI,UAAA,CAAW,IAAI,IAAI,GAAA,CAAI,KAAA,CAAM,CAAC,CAAA,GAAI,GAAA;AACpD,EAAA,IAAI,KAAA,CAAM,MAAA,GAAS,CAAA,KAAM,CAAA,EAAG;AAC1B,IAAA,MAAM,IAAI,MAAM,+BAA+B,CAAA;AAAA,EACjD;AACA,EAAA,MAAM,GAAA,GAAM,IAAI,UAAA,CAAW,KAAA,CAAM,SAAS,CAAC,CAAA;AAC3C,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,GAAA,CAAI,QAAQ,CAAA,EAAA,EAAK;AACnC,IAAA,MAAM,IAAA,GAAO,OAAO,QAAA,CAAS,KAAA,CAAM,OAAO,CAAA,GAAI,CAAA,EAAG,CAAC,CAAA,EAAG,EAAE,CAAA;AACvD,IAAA,IAAI,MAAA,CAAO,KAAA,CAAM,IAAI,CAAA,EAAG;AACtB,MAAA,MAAM,IAAI,MAAM,4BAA4B,CAAA;AAAA,IAC9C;AACA,IAAA,GAAA,CAAI,CAAC,CAAA,GAAI,IAAA;AAAA,EACX;AACA,EAAA,OAAO,GAAA;AACT;AAEO,SAAS,YAAY,KAAA,EAA2B;AAGrD,EAAA,IAAI,MAAA,GAAS,EAAA;AACb,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAA,IAAU,MAAA,CAAO,YAAA,CAAa,KAAA,CAAM,CAAC,CAAW,CAAA;AAAA,EAClD;AACA,EAAA,OAAO,IAAA,CAAK,MAAM,CAAA,CAAE,OAAA,CAAQ,KAAA,EAAO,GAAG,CAAA,CAAE,OAAA,CAAQ,KAAA,EAAO,GAAG,CAAA,CAAE,OAAA,CAAQ,OAAO,EAAE,CAAA;AAC/E;AAEO,SAAS,YAAY,GAAA,EAAyB;AACnD,EAAA,OAAO,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,GAAG,CAAA;AACrC;AAEO,SAAS,YAAY,KAAA,EAA2B;AACrD,EAAA,OAAO,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,KAAK,CAAA;AACvC","file":"encoding.cjs","sourcesContent":["/**\n * Byte encoders that work in every JavaScript runtime we target.\n * No Node `Buffer`, no dependency on the DOM — just Uint8Array, strings, and\n * the two TextEncoder/TextDecoder classes available everywhere.\n */\n\nconst HEX_CHARS = '0123456789abcdef'\n\nexport function toHex(bytes: Uint8Array): string {\n let out = ''\n for (let i = 0; i < bytes.length; i++) {\n const b = bytes[i] as number\n out += HEX_CHARS[b >>> 4]\n out += HEX_CHARS[b & 0x0f]\n }\n return out\n}\n\nexport function fromHex(hex: string): Uint8Array {\n const clean = hex.startsWith('0x') ? hex.slice(2) : hex\n if (clean.length % 2 !== 0) {\n throw new Error('fromHex: odd-length hex input')\n }\n const out = new Uint8Array(clean.length / 2)\n for (let i = 0; i < out.length; i++) {\n const byte = Number.parseInt(clean.substr(i * 2, 2), 16)\n if (Number.isNaN(byte)) {\n throw new Error('fromHex: non-hex character')\n }\n out[i] = byte\n }\n return out\n}\n\nexport function toBase64Url(bytes: Uint8Array): string {\n // Build a binary string without spreading (spread can overflow on large arrays).\n // `btoa` is global in Node 16+, all Edge runtimes, and browsers.\n let binary = ''\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i] as number)\n }\n return btoa(binary).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nexport function utf8ToBytes(str: string): Uint8Array {\n return new TextEncoder().encode(str)\n}\n\nexport function bytesToUtf8(bytes: Uint8Array): string {\n return new TextDecoder().decode(bytes)\n}\n"]}
1
+ {"version":3,"sources":["../src/encoding.ts"],"names":[],"mappings":";;;AAMA,IAAM,SAAA,GAAY,kBAAA;AAEX,SAAS,MAAM,KAAA,EAA2B;AAC/C,EAAA,IAAI,GAAA,GAAM,EAAA;AACV,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAM,CAAA,GAAI,MAAM,CAAC,CAAA;AACjB,IAAA,GAAA,IAAO,SAAA,CAAU,MAAM,CAAC,CAAA;AACxB,IAAA,GAAA,IAAO,SAAA,CAAU,IAAI,EAAI,CAAA;AAAA,EAC3B;AACA,EAAA,OAAO,GAAA;AACT;AAEO,SAAS,QAAQ,GAAA,EAAyB;AAC/C,EAAA,MAAM,KAAA,GAAQ,IAAI,UAAA,CAAW,IAAI,IAAI,GAAA,CAAI,KAAA,CAAM,CAAC,CAAA,GAAI,GAAA;AACpD,EAAA,IAAI,KAAA,CAAM,MAAA,GAAS,CAAA,KAAM,CAAA,EAAG;AAC1B,I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ime we target.\n * No Node `Buffer`, no dependency on the DOM. Only Uint8Array, strings, and\n * the two TextEncoder/TextDecoder classes available everywhere.\n */\n\nconst HEX_CHARS = '0123456789abcdef'\n\nexport function toHex(bytes: Uint8Array): string {\n let out = ''\n for (let i = 0; i < bytes.length; i++) {\n const b = bytes[i] as number\n out += HEX_CHARS[b >>> 4]\n out += HEX_CHARS[b & 0x0f]\n }\n return out\n}\n\nexport function fromHex(hex: string): Uint8Array {\n const clean = hex.startsWith('0x') ? hex.slice(2) : hex\n if (clean.length % 2 !== 0) {\n throw new Error('fromHex: odd-length hex input')\n }\n const out = new Uint8Array(clean.length / 2)\n for (let i = 0; i < out.length; i++) {\n const byte = Number.parseInt(clean.substr(i * 2, 2), 16)\n if (Number.isNaN(byte)) {\n throw new Error('fromHex: non-hex character')\n }\n out[i] = byte\n }\n return out\n}\n\nexport function toBase64Url(bytes: Uint8Array): string {\n // Build a binary string without spreading (spread can overflow on large arrays).\n // `btoa` is global in Node 16+, all Edge runtimes, and browsers.\n let binary = ''\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i] as number)\n }\n return btoa(binary).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nexport function utf8ToBytes(str: string): Uint8Array {\n return new TextEncoder().encode(str)\n}\n\nexport function bytesToUtf8(bytes: Uint8Array): string {\n return new TextDecoder().decode(bytes)\n}\n"]}
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Byte encoders that work in every JavaScript runtime we target.
3
- * No Node `Buffer`, no dependency on the DOM — just Uint8Array, strings, and
3
+ * No Node `Buffer`, no dependency on the DOM. Only Uint8Array, strings, and
4
4
  * the two TextEncoder/TextDecoder classes available everywhere.
5
5
  */
6
6
  declare function toHex(bytes: Uint8Array): string;
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * Byte encoders that work in every JavaScript runtime we target.
3
- * No Node `Buffer`, no dependency on the DOM — just Uint8Array, strings, and
3
+ * No Node `Buffer`, no dependency on the DOM. Only Uint8Array, strings, and
4
4
  * the two TextEncoder/TextDecoder classes available everywhere.
5
5
  */
6
6
  declare function toHex(bytes: Uint8Array): string;
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/encoding.ts"],"names":[],"mappings":";AAMA,IAAM,SAAA,GAAY,kBAAA;AAEX,SAAS,MAAM,KAAA,EAA2B;AAC/C,EAAA,IAAI,GAAA,GAAM,EAAA;AACV,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAM,CAAA,GAAI,MAAM,CAAC,CAAA;AACjB,IAAA,GAAA,IAAO,SAAA,CAAU,MAAM,CAAC,CAAA;AACxB,IAAA,GAAA,IAAO,SAAA,CAAU,IAAI,EAAI,CAAA;AAAA,EAC3B;AACA,EAAA,OAAO,GAAA;AACT;AAEO,SAAS,QAAQ,GAAA,EAAyB;AAC/C,EAAA,MAAM,KAAA,GAAQ,IAAI,UAAA,CAAW,IAAI,IAAI,GAAA,CAAI,KAAA,CAAM,CAAC,CAAA,GAAI,GAAA;AACpD,EAAA,IAAI,KAAA,CAAM,MAAA,GAAS,CAAA,KAAM,CAAA,EAAG;AAC1B,IAAA,MAAM,IAAI,MAAM,+BAA+B,CAAA;AAAA,EACjD;AACA,EAAA,MAAM,GAAA,GAAM,IAAI,UAAA,CAAW,KAAA,CAAM,SAAS,CAAC,CAAA;AAC3C,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,GAAA,CAAI,QAAQ,CAAA,EAAA,EAAK;AACnC,IAAA,MAAM,IAAA,GAAO,OAAO,QAAA,CAAS,KAAA,CAAM,OAAO,CAAA,GAAI,CAAA,EAAG,CAAC,CAAA,EAAG,EAAE,CAAA;AACvD,IAAA,IAAI,MAAA,CAAO,KAAA,CAAM,IAAI,CAAA,EAAG;AACtB,MAAA,MAAM,IAAI,MAAM,4BAA4B,CAAA;AAAA,IAC9C;AACA,IAAA,GAAA,CAAI,CAAC,CAAA,GAAI,IAAA;AAAA,EACX;AACA,EAAA,OAAO,GAAA;AACT;AAEO,SAAS,YAAY,KAAA,EAA2B;AAGrD,EAAA,IAAI,MAAA,GAAS,EAAA;AACb,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAA,IAAU,MAAA,CAAO,YAAA,CAAa,KAAA,CAAM,CAAC,CAAW,CAAA;AAAA,EAClD;AACA,EAAA,OAAO,IAAA,CAAK,MAAM,CAAA,CAAE,OAAA,CAAQ,KAAA,EAAO,GAAG,CAAA,CAAE,OAAA,CAAQ,KAAA,EAAO,GAAG,CAAA,CAAE,OAAA,CAAQ,OAAO,EAAE,CAAA;AAC/E;AAEO,SAAS,YAAY,GAAA,EAAyB;AACnD,EAAA,OAAO,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,GAAG,CAAA;AACrC;AAEO,SAAS,YAAY,KAAA,EAA2B;AACrD,EAAA,OAAO,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,KAAK,CAAA;AACvC","file":"encoding.js","sourcesContent":["/**\n * Byte encoders that work in every JavaScript runtime we target.\n * No Node `Buffer`, no dependency on the DOM — just Uint8Array, strings, and\n * the two TextEncoder/TextDecoder classes available everywhere.\n */\n\nconst HEX_CHARS = '0123456789abcdef'\n\nexport function toHex(bytes: Uint8Array): string {\n let out = ''\n for (let i = 0; i < bytes.length; i++) {\n const b = bytes[i] as number\n out += HEX_CHARS[b >>> 4]\n out += HEX_CHARS[b & 0x0f]\n }\n return out\n}\n\nexport function fromHex(hex: string): Uint8Array {\n const clean = hex.startsWith('0x') ? hex.slice(2) : hex\n if (clean.length % 2 !== 0) {\n throw new Error('fromHex: odd-length hex input')\n }\n const out = new Uint8Array(clean.length / 2)\n for (let i = 0; i < out.length; i++) {\n const byte = Number.parseInt(clean.substr(i * 2, 2), 16)\n if (Number.isNaN(byte)) {\n throw new Error('fromHex: non-hex character')\n }\n out[i] = byte\n }\n return out\n}\n\nexport function toBase64Url(bytes: Uint8Array): string {\n // Build a binary string without spreading (spread can overflow on large arrays).\n // `btoa` is global in Node 16+, all Edge runtimes, and browsers.\n let binary = ''\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i] as number)\n }\n return btoa(binary).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nexport function utf8ToBytes(str: string): Uint8Array {\n return new TextEncoder().encode(str)\n}\n\nexport function bytesToUtf8(bytes: Uint8Array): string {\n return new TextDecoder().decode(bytes)\n}\n"]}
1
+ {"version":3,"sources":["../src/encoding.ts"],"names":[],"mappings":";AAMA,IAAM,SAAA,GAAY,kBAAA;AAEX,SAAS,MAAM,KAAA,EAA2B;AAC/C,EAAA,IAAI,GAAA,GAAM,EAAA;AACV,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAM,CAAA,GAAI,MAAM,CAAC,CAAA;AACjB,IAAA,GAAA,IAAO,SAAA,CAAU,MAAM,CAAC,CAAA;AACxB,IAAA,GAAA,IAAO,SAAA,CAAU,IAAI,EAAI,CAAA;AAAA,EAC3B;AACA,EAAA,OAAO,GAAA;AACT;AAEO,SAAS,QAAQ,GAAA,EAAyB;AAC/C,EAAA,MAAM,KAAA,GAAQ,IAAI,UAAA,CAAW,IAAI,IAAI,GAAA,CAAI,KAAA,CAAM,CAAC,CAAA,GAAI,GAAA;AACpD,EAAA,IAAI,KAAA,CAAM,MAAA,GAAS,CAAA,KAAM,CAAA,EAAG;AAC1B,IAAA,MAAM,IAAI,MAAM,+BAA+B,CAAA;AAAA,EACjD;AACA,EAAA,MAAM,GAAA,GAAM,IAAI,UAAA,CAAW,KAAA,CAAM,SAAS,CAAC,CAAA;AAC3C,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,GAAA,CAAI,QAAQ,CAAA,EAAA,EAAK;AACnC,IAAA,MAAM,IAAA,GAAO,OAAO,QAAA,CAAS,KAAA,CAAM,OAAO,CAAA,GAAI,CAAA,EAAG,CAAC,CAAA,EAAG,EAAE,CAAA;AACvD,IAAA,IAAI,MAAA,CAAO,KAAA,CAAM,IAAI,CAAA,EAAG;AACtB,MAAA,MAAM,IAAI,MAAM,4BAA4B,CAAA;AAAA,IAC9C;AACA,IAAA,GAAA,CAAI,CAAC,CAAA,GAAI,IAAA;AAAA,EACX;AACA,EAAA,OAAO,GAAA;AACT;AAEO,SAAS,YAAY,KAAA,EAA2B;AAGrD,EAAA,IAAI,MAAA,GAAS,EAAA;AACb,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAA,IAAU,MAAA,CAAO,YAAA,CAAa,KAAA,CAAM,CAAC,CAAW,CAAA;AAAA,EAClD;AACA,EAAA,OAAO,IAAA,CAAK,MAAM,CAAA,CAAE,OAAA,CAAQ,KAAA,EAAO,GAAG,CAAA,CAAE,OAAA,CAAQ,KAAA,EAAO,GAAG,CAAA,CAAE,OAAA,CAAQ,OAAO,EAAE,CAAA;AAC/E;AAEO,SAAS,YAAY,GAAA,EAAyB;AACnD,EAAA,OAAO,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,GAAG,CAAA;AACrC;AAEO,SAAS,YAAY,KAAA,EAA2B;AACrD,EAAA,OAAO,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,KAAK,CAAA;AACvC","file":"encoding.js","sourcesContent":["/**\n * Byte encoders that work in every JavaScript runtime we target.\n * No Node `Buffer`, no dependency on the DOM. Only Uint8Array, strings, and\n * the two TextEncoder/TextDecoder classes available everywhere.\n */\n\nconst HEX_CHARS = '0123456789abcdef'\n\nexport function toHex(bytes: Uint8Array): string {\n let out = ''\n for (let i = 0; i < bytes.length; i++) {\n const b = bytes[i] as number\n out += HEX_CHARS[b >>> 4]\n out += HEX_CHARS[b & 0x0f]\n }\n return out\n}\n\nexport function fromHex(hex: string): Uint8Array {\n const clean = hex.startsWith('0x') ? hex.slice(2) : hex\n if (clean.length % 2 !== 0) {\n throw new Error('fromHex: odd-length hex input')\n }\n const out = new Uint8Array(clean.length / 2)\n for (let i = 0; i < out.length; i++) {\n const byte = Number.parseInt(clean.substr(i * 2, 2), 16)\n if (Number.isNaN(byte)) {\n throw new Error('fromHex: non-hex character')\n }\n out[i] = byte\n }\n return out\n}\n\nexport function toBase64Url(bytes: Uint8Array): string {\n // Build a binary string without spreading (spread can overflow on large arrays).\n // `btoa` is global in Node 16+, all Edge runtimes, and browsers.\n let binary = ''\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i] as number)\n }\n return btoa(binary).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nexport function utf8ToBytes(str: string): Uint8Array {\n return new TextEncoder().encode(str)\n}\n\nexport function bytesToUtf8(bytes: Uint8Array): string {\n return new TextDecoder().decode(bytes)\n}\n"]}
package/dist/hash.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/encoding.ts","../src/hash.ts"],"names":[],"mappings":";;;AAMA,IAAM,SAAA,GAAY,kBAAA;AAEX,SAAS,MAAM,KAAA,EAA2B;AAC/C,EAAA,IAAI,GAAA,GAAM,EAAA;AACV,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAM,CAAA,GAAI,MAAM,CAAC,CAAA;AACjB,IAAA,GAAA,IAAO,SAAA,CAAU,MAAM,CAAC,CAAA;AACxB,IAAA,GAAA,IAAO,SAAA,CAAU,IAAI,EAAI,CAAA;AAAA,EAC3B;AACA,EAAA,OAAO,GAAA;AACT;AA4BO,SAAS,YAAY,GAAA,EAAyB;AACnD,EAAA,OAAO,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,GAAG,CAAA;AACrC;;;ACxCA,eAAsB,OAAO,IAAA,EAAgD;AAC3E,EAAA,MAAM,QAAQ,OAAO,IAAA,KAAS,QAAA,GAAW,WAAA,CAAY,IAAI,CAAA,GAAI,IAAA;AAC7D,EAAA,MAAM,SAAS,MAAM,MAAA,EAAO,CAAE,MAAA,CAAO,WAAW,KAAqB,CAAA;AACrE,EAAA,OAAO,IAAI,WAAW,MAAM,CAAA;AAC9B;AAEA,eAAsB,UAAU,IAAA,EAA4C;AAC1E,EAAA,OAAO,KAAA,CAAM,MAAM,MAAA,CAAO,IAAI,CAAC,CAAA;AACjC;AAEA,SAAS,MAAA,GAAuB;AAC9B,EAAA,MAAM,IAAI,UAAA,CAAW,MAAA;AACrB,EAAA,IAAI,CAAC,CAAA,IAAK,CAAC,CAAA,CAAE,MAAA,EAAQ;AACnB,IAAA,MAAM,IAAI,MAAM,6CAA6C,CAAA;AAAA,EAC/D;AACA,EAAA,OAAO,CAAA,CAAE,MAAA;AACX","file":"hash.cjs","sourcesContent":["/**\n * Byte encoders that work in every JavaScript runtime we target.\n * No Node `Buffer`, no dependency on the DOM — just Uint8Array, strings, and\n * the two TextEncoder/TextDecoder classes available everywhere.\n */\n\nconst HEX_CHARS = '0123456789abcdef'\n\nexport function toHex(bytes: Uint8Array): string {\n let out = ''\n for (let i = 0; i < bytes.length; i++) {\n const b = bytes[i] as number\n out += HEX_CHARS[b >>> 4]\n out += HEX_CHARS[b & 0x0f]\n }\n return out\n}\n\nexport function fromHex(hex: string): Uint8Array {\n const clean = hex.startsWith('0x') ? hex.slice(2) : hex\n if (clean.length % 2 !== 0) {\n throw new Error('fromHex: odd-length hex input')\n }\n const out = new Uint8Array(clean.length / 2)\n for (let i = 0; i < out.length; i++) {\n const byte = Number.parseInt(clean.substr(i * 2, 2), 16)\n if (Number.isNaN(byte)) {\n throw new Error('fromHex: non-hex character')\n }\n out[i] = byte\n }\n return out\n}\n\nexport function toBase64Url(bytes: Uint8Array): string {\n // Build a binary string without spreading (spread can overflow on large arrays).\n // `btoa` is global in Node 16+, all Edge runtimes, and browsers.\n let binary = ''\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i] as number)\n }\n return btoa(binary).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nexport function utf8ToBytes(str: string): Uint8Array {\n return new TextEncoder().encode(str)\n}\n\nexport function bytesToUtf8(bytes: Uint8Array): string {\n return new TextDecoder().decode(bytes)\n}\n","/**\n * SHA-256 via Web Crypto (available in Node 16+, Deno, Bun, Workers, Edge).\n */\n\nimport { toHex, utf8ToBytes } from './encoding.js'\n\nexport async function sha256(data: string | Uint8Array): Promise<Uint8Array> {\n const bytes = typeof data === 'string' ? utf8ToBytes(data) : data\n const digest = await subtle().digest('SHA-256', bytes as BufferSource)\n return new Uint8Array(digest)\n}\n\nexport async function sha256Hex(data: string | Uint8Array): Promise<string> {\n return toHex(await sha256(data))\n}\n\nfunction subtle(): SubtleCrypto {\n const c = globalThis.crypto\n if (!c || !c.subtle) {\n throw new Error('Web Crypto is not available in this runtime')\n }\n return c.subtle\n}\n"]}
1
+ {"version":3,"sources":["../src/encoding.ts","../src/hash.ts"],"names":[],"mappings":";;;AAMA,IAAM,SAAA,GAAY,kBAAA;AAEX,SAAS,MAAM,KAAA,EAA2B;AAC/C,EAAA,IAAI,GAAA,GAAM,EAAA;AACV,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAM,CAAA,GAAI,MAAM,CAAC,CAAA;AACjB,IAAA,GAAA,IAAO,SAAA,CAAU,MAAM,CAAC,CAAA;AACxB,IAAA,GAAA,IAAO,SAAA,CAAU,IAAI,EAAI,CAAA;AAAA,EAC3B;AACA,EAAA,OAAO,GAAA;AACT;AA4BO,SAAS,YAAY,GAAA,EAAyB;AACnD,EAAA,OAAO,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,GAAG,CAAA;AACrC;;;ACxCA,eAAsB,OAAO,IAAA,EAAgD;AAC3E,EAAA,MAAM,QAAQ,OAAO,IAAA,KAAS,QAAA,GAAW,WAAA,CAAY,IAAI,CAAA,GAAI,IAAA;AAC7D,EAAA,MAAM,SAAS,MAAM,MAAA,EAAO,CAAE,MAAA,CAAO,WAAW,KAAqB,CAAA;AACrE,EAAA,OAAO,IAAI,WAAW,MAAM,CAAA;AAC9B;AAEA,eAAsB,UAAU,IAAA,EAA4C;AAC1E,EAAA,OAAO,KAAA,CAAM,MAAM,MAAA,CAAO,IAAI,CAAC,CAAA;AACjC;AAEA,SAAS,MAAA,GAAuB;AAC9B,EAAA,MAAM,IAAI,UAAA,CAAW,MAAA;AACrB,EAAA,IAAI,CAAC,CAAA,IAAK,CAAC,CAAA,CAAE,MAAA,EAAQ;AACnB,IAAA,MAAM,IAAI,MAAM,6CAA6C,CAAA;AAAA,EAC/D;AACA,EAAA,OAAO,CAAA,CAAE,MAAA;AACX","file":"hash.cjs","sourcesContent":["/**\n * Byte encoders that work in every JavaScript runtime we target.\n * No Node `Buffer`, no dependency on the DOM. Only Uint8Array, strings, and\n * the two TextEncoder/TextDecoder classes available everywhere.\n */\n\nconst HEX_CHARS = '0123456789abcdef'\n\nexport function toHex(bytes: Uint8Array): string {\n let out = ''\n for (let i = 0; i < bytes.length; i++) {\n const b = bytes[i] as number\n out += HEX_CHARS[b >>> 4]\n out += HEX_CHARS[b & 0x0f]\n }\n return out\n}\n\nexport function fromHex(hex: string): Uint8Array {\n const clean = hex.startsWith('0x') ? hex.slice(2) : hex\n if (clean.length % 2 !== 0) {\n throw new Error('fromHex: odd-length hex input')\n }\n const out = new Uint8Array(clean.length / 2)\n for (let i = 0; i < out.length; i++) {\n const byte = Number.parseInt(clean.substr(i * 2, 2), 16)\n if (Number.isNaN(byte)) {\n throw new Error('fromHex: non-hex character')\n }\n out[i] = byte\n }\n return out\n}\n\nexport function toBase64Url(bytes: Uint8Array): string {\n // Build a binary string without spreading (spread can overflow on large arrays).\n // `btoa` is global in Node 16+, all Edge runtimes, and browsers.\n let binary = ''\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i] as number)\n }\n return btoa(binary).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nexport function utf8ToBytes(str: string): Uint8Array {\n return new TextEncoder().encode(str)\n}\n\nexport function bytesToUtf8(bytes: Uint8Array): string {\n return new TextDecoder().decode(bytes)\n}\n","/**\n * SHA-256 via Web Crypto (available in Node 16+, Deno, Bun, Workers, Edge).\n */\n\nimport { toHex, utf8ToBytes } from './encoding.js'\n\nexport async function sha256(data: string | Uint8Array): Promise<Uint8Array> {\n const bytes = typeof data === 'string' ? utf8ToBytes(data) : data\n const digest = await subtle().digest('SHA-256', bytes as BufferSource)\n return new Uint8Array(digest)\n}\n\nexport async function sha256Hex(data: string | Uint8Array): Promise<string> {\n return toHex(await sha256(data))\n}\n\nfunction subtle(): SubtleCrypto {\n const c = globalThis.crypto\n if (!c || !c.subtle) {\n throw new Error('Web Crypto is not available in this runtime')\n }\n return c.subtle\n}\n"]}
package/dist/hash.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/encoding.ts","../src/hash.ts"],"names":[],"mappings":";AAMA,IAAM,SAAA,GAAY,kBAAA;AAEX,SAAS,MAAM,KAAA,EAA2B;AAC/C,EAAA,IAAI,GAAA,GAAM,EAAA;AACV,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAM,CAAA,GAAI,MAAM,CAAC,CAAA;AACjB,IAAA,GAAA,IAAO,SAAA,CAAU,MAAM,CAAC,CAAA;AACxB,IAAA,GAAA,IAAO,SAAA,CAAU,IAAI,EAAI,CAAA;AAAA,EAC3B;AACA,EAAA,OAAO,GAAA;AACT;AA4BO,SAAS,YAAY,GAAA,EAAyB;AACnD,EAAA,OAAO,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,GAAG,CAAA;AACrC;;;ACxCA,eAAsB,OAAO,IAAA,EAAgD;AAC3E,EAAA,MAAM,QAAQ,OAAO,IAAA,KAAS,QAAA,GAAW,WAAA,CAAY,IAAI,CAAA,GAAI,IAAA;AAC7D,EAAA,MAAM,SAAS,MAAM,MAAA,EAAO,CAAE,MAAA,CAAO,WAAW,KAAqB,CAAA;AACrE,EAAA,OAAO,IAAI,WAAW,MAAM,CAAA;AAC9B;AAEA,eAAsB,UAAU,IAAA,EAA4C;AAC1E,EAAA,OAAO,KAAA,CAAM,MAAM,MAAA,CAAO,IAAI,CAAC,CAAA;AACjC;AAEA,SAAS,MAAA,GAAuB;AAC9B,EAAA,MAAM,IAAI,UAAA,CAAW,MAAA;AACrB,EAAA,IAAI,CAAC,CAAA,IAAK,CAAC,CAAA,CAAE,MAAA,EAAQ;AACnB,IAAA,MAAM,IAAI,MAAM,6CAA6C,CAAA;AAAA,EAC/D;AACA,EAAA,OAAO,CAAA,CAAE,MAAA;AACX","file":"hash.js","sourcesContent":["/**\n * Byte encoders that work in every JavaScript runtime we target.\n * No Node `Buffer`, no dependency on the DOM — just Uint8Array, strings, and\n * the two TextEncoder/TextDecoder classes available everywhere.\n */\n\nconst HEX_CHARS = '0123456789abcdef'\n\nexport function toHex(bytes: Uint8Array): string {\n let out = ''\n for (let i = 0; i < bytes.length; i++) {\n const b = bytes[i] as number\n out += HEX_CHARS[b >>> 4]\n out += HEX_CHARS[b & 0x0f]\n }\n return out\n}\n\nexport function fromHex(hex: string): Uint8Array {\n const clean = hex.startsWith('0x') ? hex.slice(2) : hex\n if (clean.length % 2 !== 0) {\n throw new Error('fromHex: odd-length hex input')\n }\n const out = new Uint8Array(clean.length / 2)\n for (let i = 0; i < out.length; i++) {\n const byte = Number.parseInt(clean.substr(i * 2, 2), 16)\n if (Number.isNaN(byte)) {\n throw new Error('fromHex: non-hex character')\n }\n out[i] = byte\n }\n return out\n}\n\nexport function toBase64Url(bytes: Uint8Array): string {\n // Build a binary string without spreading (spread can overflow on large arrays).\n // `btoa` is global in Node 16+, all Edge runtimes, and browsers.\n let binary = ''\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i] as number)\n }\n return btoa(binary).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nexport function utf8ToBytes(str: string): Uint8Array {\n return new TextEncoder().encode(str)\n}\n\nexport function bytesToUtf8(bytes: Uint8Array): string {\n return new TextDecoder().decode(bytes)\n}\n","/**\n * SHA-256 via Web Crypto (available in Node 16+, Deno, Bun, Workers, Edge).\n */\n\nimport { toHex, utf8ToBytes } from './encoding.js'\n\nexport async function sha256(data: string | Uint8Array): Promise<Uint8Array> {\n const bytes = typeof data === 'string' ? utf8ToBytes(data) : data\n const digest = await subtle().digest('SHA-256', bytes as BufferSource)\n return new Uint8Array(digest)\n}\n\nexport async function sha256Hex(data: string | Uint8Array): Promise<string> {\n return toHex(await sha256(data))\n}\n\nfunction subtle(): SubtleCrypto {\n const c = globalThis.crypto\n if (!c || !c.subtle) {\n throw new Error('Web Crypto is not available in this runtime')\n }\n return c.subtle\n}\n"]}
1
+ {"version":3,"sources":["../src/encoding.ts","../src/hash.ts"],"names":[],"mappings":";AAMA,IAAM,SAAA,GAAY,kBAAA;AAEX,SAAS,MAAM,KAAA,EAA2B;AAC/C,EAAA,IAAI,GAAA,GAAM,EAAA;AACV,EAAA,KAAA,IAAS,CAAA,GAAI,CAAA,EAAG,CAAA,GAAI,KAAA,CAAM,QAAQ,CAAA,EAAA,EAAK;AACrC,IAAA,MAAM,CAAA,GAAI,MAAM,CAAC,CAAA;AACjB,IAAA,GAAA,IAAO,SAAA,CAAU,MAAM,CAAC,CAAA;AACxB,IAAA,GAAA,IAAO,SAAA,CAAU,IAAI,EAAI,CAAA;AAAA,EAC3B;AACA,EAAA,OAAO,GAAA;AACT;AA4BO,SAAS,YAAY,GAAA,EAAyB;AACnD,EAAA,OAAO,IAAI,WAAA,EAAY,CAAE,MAAA,CAAO,GAAG,CAAA;AACrC;;;ACxCA,eAAsB,OAAO,IAAA,EAAgD;AAC3E,EAAA,MAAM,QAAQ,OAAO,IAAA,KAAS,QAAA,GAAW,WAAA,CAAY,IAAI,CAAA,GAAI,IAAA;AAC7D,EAAA,MAAM,SAAS,MAAM,MAAA,EAAO,CAAE,MAAA,CAAO,WAAW,KAAqB,CAAA;AACrE,EAAA,OAAO,IAAI,WAAW,MAAM,CAAA;AAC9B;AAEA,eAAsB,UAAU,IAAA,EAA4C;AAC1E,EAAA,OAAO,KAAA,CAAM,MAAM,MAAA,CAAO,IAAI,CAAC,CAAA;AACjC;AAEA,SAAS,MAAA,GAAuB;AAC9B,EAAA,MAAM,IAAI,UAAA,CAAW,MAAA;AACrB,EAAA,IAAI,CAAC,CAAA,IAAK,CAAC,CAAA,CAAE,MAAA,EAAQ;AACnB,IAAA,MAAM,IAAI,MAAM,6CAA6C,CAAA;AAAA,EAC/D;AACA,EAAA,OAAO,CAAA,CAAE,MAAA;AACX","file":"hash.js","sourcesContent":["/**\n * Byte encoders that work in every JavaScript runtime we target.\n * No Node `Buffer`, no dependency on the DOM. Only Uint8Array, strings, and\n * the two TextEncoder/TextDecoder classes available everywhere.\n */\n\nconst HEX_CHARS = '0123456789abcdef'\n\nexport function toHex(bytes: Uint8Array): string {\n let out = ''\n for (let i = 0; i < bytes.length; i++) {\n const b = bytes[i] as number\n out += HEX_CHARS[b >>> 4]\n out += HEX_CHARS[b & 0x0f]\n }\n return out\n}\n\nexport function fromHex(hex: string): Uint8Array {\n const clean = hex.startsWith('0x') ? hex.slice(2) : hex\n if (clean.length % 2 !== 0) {\n throw new Error('fromHex: odd-length hex input')\n }\n const out = new Uint8Array(clean.length / 2)\n for (let i = 0; i < out.length; i++) {\n const byte = Number.parseInt(clean.substr(i * 2, 2), 16)\n if (Number.isNaN(byte)) {\n throw new Error('fromHex: non-hex character')\n }\n out[i] = byte\n }\n return out\n}\n\nexport function toBase64Url(bytes: Uint8Array): string {\n // Build a binary string without spreading (spread can overflow on large arrays).\n // `btoa` is global in Node 16+, all Edge runtimes, and browsers.\n let binary = ''\n for (let i = 0; i < bytes.length; i++) {\n binary += String.fromCharCode(bytes[i] as number)\n }\n return btoa(binary).replace(/\\+/g, '-').replace(/\\//g, '_').replace(/=+$/, '')\n}\n\nexport function utf8ToBytes(str: string): Uint8Array {\n return new TextEncoder().encode(str)\n}\n\nexport function bytesToUtf8(bytes: Uint8Array): string {\n return new TextDecoder().decode(bytes)\n}\n","/**\n * SHA-256 via Web Crypto (available in Node 16+, Deno, Bun, Workers, Edge).\n */\n\nimport { toHex, utf8ToBytes } from './encoding.js'\n\nexport async function sha256(data: string | Uint8Array): Promise<Uint8Array> {\n const bytes = typeof data === 'string' ? utf8ToBytes(data) : data\n const digest = await subtle().digest('SHA-256', bytes as BufferSource)\n return new Uint8Array(digest)\n}\n\nexport async function sha256Hex(data: string | Uint8Array): Promise<string> {\n return toHex(await sha256(data))\n}\n\nfunction subtle(): SubtleCrypto {\n const c = globalThis.crypto\n if (!c || !c.subtle) {\n throw new Error('Web Crypto is not available in this runtime')\n }\n return c.subtle\n}\n"]}