@strimz/shared-crypto 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +57 -0
- package/dist/aes-gcm.cjs +94 -0
- package/dist/aes-gcm.cjs.map +1 -0
- package/dist/aes-gcm.d.cts +59 -0
- package/dist/aes-gcm.d.ts +59 -0
- package/dist/aes-gcm.js +90 -0
- package/dist/aes-gcm.js.map +1 -0
- package/dist/api-key.cjs +95 -0
- package/dist/api-key.cjs.map +1 -0
- package/dist/api-key.d.cts +32 -0
- package/dist/api-key.d.ts +32 -0
- package/dist/api-key.js +91 -0
- package/dist/api-key.js.map +1 -0
- package/dist/encoding.cjs +49 -0
- package/dist/encoding.cjs.map +1 -0
- package/dist/encoding.d.cts +12 -0
- package/dist/encoding.d.ts +12 -0
- package/dist/encoding.js +43 -0
- package/dist/encoding.js.map +1 -0
- package/dist/hash.cjs +38 -0
- package/dist/hash.cjs.map +1 -0
- package/dist/hash.d.cts +7 -0
- package/dist/hash.d.ts +7 -0
- package/dist/hash.js +35 -0
- package/dist/hash.js.map +1 -0
- package/dist/hmac.cjs +47 -0
- package/dist/hmac.cjs.map +1 -0
- package/dist/hmac.d.cts +8 -0
- package/dist/hmac.d.ts +8 -0
- package/dist/hmac.js +44 -0
- package/dist/hmac.js.map +1 -0
- package/dist/index.cjs +296 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +9 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +271 -0
- package/dist/index.js.map +1 -0
- package/dist/random.cjs +61 -0
- package/dist/random.cjs.map +1 -0
- package/dist/random.d.cts +10 -0
- package/dist/random.d.ts +10 -0
- package/dist/random.js +56 -0
- package/dist/random.js.map +1 -0
- package/dist/timing-safe.cjs +24 -0
- package/dist/timing-safe.cjs.map +1 -0
- package/dist/timing-safe.d.cts +14 -0
- package/dist/timing-safe.d.ts +14 -0
- package/dist/timing-safe.js +21 -0
- package/dist/timing-safe.js.map +1 -0
- package/dist/webhook.cjs +123 -0
- package/dist/webhook.cjs.map +1 -0
- package/dist/webhook.d.cts +31 -0
- package/dist/webhook.d.ts +31 -0
- package/dist/webhook.js +119 -0
- package/dist/webhook.js.map +1 -0
- package/package.json +105 -0
- package/src/aes-gcm.test.ts +72 -0
- package/src/aes-gcm.ts +119 -0
- package/src/api-key.test.ts +59 -0
- package/src/api-key.ts +61 -0
- package/src/encoding.test.ts +49 -0
- package/src/encoding.ts +51 -0
- package/src/hash.test.ts +29 -0
- package/src/hash.ts +23 -0
- package/src/hmac.test.ts +28 -0
- package/src/hmac.ts +40 -0
- package/src/index.ts +15 -0
- package/src/random.test.ts +45 -0
- package/src/random.ts +44 -0
- package/src/timing-safe.test.ts +38 -0
- package/src/timing-safe.ts +28 -0
- package/src/webhook.test.ts +78 -0
- package/src/webhook.ts +106 -0
package/dist/api-key.js
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { API_KEY_PREFIXES, API_KEY_PREFIX_DISPLAY_LENGTH, API_KEY_RANDOM_BYTES } from '@strimz/shared-config/api-keys';
|
|
2
|
+
|
|
3
|
+
// src/api-key.ts
|
|
4
|
+
|
|
5
|
+
// src/encoding.ts
|
|
6
|
+
var HEX_CHARS = "0123456789abcdef";
|
|
7
|
+
function toHex(bytes) {
|
|
8
|
+
let out = "";
|
|
9
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
10
|
+
const b = bytes[i];
|
|
11
|
+
out += HEX_CHARS[b >>> 4];
|
|
12
|
+
out += HEX_CHARS[b & 15];
|
|
13
|
+
}
|
|
14
|
+
return out;
|
|
15
|
+
}
|
|
16
|
+
function toBase64Url(bytes) {
|
|
17
|
+
let binary = "";
|
|
18
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
19
|
+
binary += String.fromCharCode(bytes[i]);
|
|
20
|
+
}
|
|
21
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
22
|
+
}
|
|
23
|
+
function utf8ToBytes(str) {
|
|
24
|
+
return new TextEncoder().encode(str);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// src/hash.ts
|
|
28
|
+
async function sha256(data) {
|
|
29
|
+
const bytes = typeof data === "string" ? utf8ToBytes(data) : data;
|
|
30
|
+
const digest = await subtle().digest("SHA-256", bytes);
|
|
31
|
+
return new Uint8Array(digest);
|
|
32
|
+
}
|
|
33
|
+
async function sha256Hex(data) {
|
|
34
|
+
return toHex(await sha256(data));
|
|
35
|
+
}
|
|
36
|
+
function subtle() {
|
|
37
|
+
const c = globalThis.crypto;
|
|
38
|
+
if (!c || !c.subtle) {
|
|
39
|
+
throw new Error("Web Crypto is not available in this runtime");
|
|
40
|
+
}
|
|
41
|
+
return c.subtle;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
// src/random.ts
|
|
45
|
+
function randomBytes(n) {
|
|
46
|
+
if (!Number.isInteger(n) || n <= 0) {
|
|
47
|
+
throw new Error("randomBytes: n must be a positive integer");
|
|
48
|
+
}
|
|
49
|
+
const buf = new Uint8Array(n);
|
|
50
|
+
getRandomValues(buf);
|
|
51
|
+
return buf;
|
|
52
|
+
}
|
|
53
|
+
function randomBase64Url(nBytes) {
|
|
54
|
+
return toBase64Url(randomBytes(nBytes));
|
|
55
|
+
}
|
|
56
|
+
function getRandomValues(buf) {
|
|
57
|
+
const c = globalThis.crypto;
|
|
58
|
+
if (!c || typeof c.getRandomValues !== "function") {
|
|
59
|
+
throw new Error("crypto.getRandomValues is not available in this runtime");
|
|
60
|
+
}
|
|
61
|
+
c.getRandomValues(buf);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// src/api-key.ts
|
|
65
|
+
async function generateApiKey(kind, mode) {
|
|
66
|
+
const prefixStr = API_KEY_PREFIXES[kind][mode];
|
|
67
|
+
const random = randomBase64Url(API_KEY_RANDOM_BYTES);
|
|
68
|
+
const secret = `${prefixStr}${random}`;
|
|
69
|
+
const hash = await sha256Hex(secret);
|
|
70
|
+
return {
|
|
71
|
+
secret,
|
|
72
|
+
hash,
|
|
73
|
+
prefix: secret.slice(0, API_KEY_PREFIX_DISPLAY_LENGTH),
|
|
74
|
+
lastFour: secret.slice(-4),
|
|
75
|
+
kind,
|
|
76
|
+
mode
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
async function hashApiKey(secret) {
|
|
80
|
+
return sha256Hex(secret);
|
|
81
|
+
}
|
|
82
|
+
function redactApiKey(secret) {
|
|
83
|
+
if (secret.length <= API_KEY_PREFIX_DISPLAY_LENGTH + 4) {
|
|
84
|
+
return "***";
|
|
85
|
+
}
|
|
86
|
+
return `${secret.slice(0, API_KEY_PREFIX_DISPLAY_LENGTH)}...${secret.slice(-4)}`;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export { generateApiKey, hashApiKey, redactApiKey };
|
|
90
|
+
//# sourceMappingURL=api-key.js.map
|
|
91
|
+
//# sourceMappingURL=api-key.js.map
|
|
@@ -0,0 +1 @@
|
|
|
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,CAA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O,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"]}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/encoding.ts
|
|
4
|
+
var HEX_CHARS = "0123456789abcdef";
|
|
5
|
+
function toHex(bytes) {
|
|
6
|
+
let out = "";
|
|
7
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
8
|
+
const b = bytes[i];
|
|
9
|
+
out += HEX_CHARS[b >>> 4];
|
|
10
|
+
out += HEX_CHARS[b & 15];
|
|
11
|
+
}
|
|
12
|
+
return out;
|
|
13
|
+
}
|
|
14
|
+
function fromHex(hex) {
|
|
15
|
+
const clean = hex.startsWith("0x") ? hex.slice(2) : hex;
|
|
16
|
+
if (clean.length % 2 !== 0) {
|
|
17
|
+
throw new Error("fromHex: odd-length hex input");
|
|
18
|
+
}
|
|
19
|
+
const out = new Uint8Array(clean.length / 2);
|
|
20
|
+
for (let i = 0; i < out.length; i++) {
|
|
21
|
+
const byte = Number.parseInt(clean.substr(i * 2, 2), 16);
|
|
22
|
+
if (Number.isNaN(byte)) {
|
|
23
|
+
throw new Error("fromHex: non-hex character");
|
|
24
|
+
}
|
|
25
|
+
out[i] = byte;
|
|
26
|
+
}
|
|
27
|
+
return out;
|
|
28
|
+
}
|
|
29
|
+
function toBase64Url(bytes) {
|
|
30
|
+
let binary = "";
|
|
31
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
32
|
+
binary += String.fromCharCode(bytes[i]);
|
|
33
|
+
}
|
|
34
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
35
|
+
}
|
|
36
|
+
function utf8ToBytes(str) {
|
|
37
|
+
return new TextEncoder().encode(str);
|
|
38
|
+
}
|
|
39
|
+
function bytesToUtf8(bytes) {
|
|
40
|
+
return new TextDecoder().decode(bytes);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
exports.bytesToUtf8 = bytesToUtf8;
|
|
44
|
+
exports.fromHex = fromHex;
|
|
45
|
+
exports.toBase64Url = toBase64Url;
|
|
46
|
+
exports.toHex = toHex;
|
|
47
|
+
exports.utf8ToBytes = utf8ToBytes;
|
|
48
|
+
//# sourceMappingURL=encoding.cjs.map
|
|
49
|
+
//# sourceMappingURL=encoding.cjs.map
|
|
@@ -0,0 +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;AACr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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"]}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Byte encoders that work in every JavaScript runtime we target.
|
|
3
|
+
* No Node `Buffer`, no dependency on the DOM — just Uint8Array, strings, and
|
|
4
|
+
* the two TextEncoder/TextDecoder classes available everywhere.
|
|
5
|
+
*/
|
|
6
|
+
declare function toHex(bytes: Uint8Array): string;
|
|
7
|
+
declare function fromHex(hex: string): Uint8Array;
|
|
8
|
+
declare function toBase64Url(bytes: Uint8Array): string;
|
|
9
|
+
declare function utf8ToBytes(str: string): Uint8Array;
|
|
10
|
+
declare function bytesToUtf8(bytes: Uint8Array): string;
|
|
11
|
+
|
|
12
|
+
export { bytesToUtf8, fromHex, toBase64Url, toHex, utf8ToBytes };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Byte encoders that work in every JavaScript runtime we target.
|
|
3
|
+
* No Node `Buffer`, no dependency on the DOM — just Uint8Array, strings, and
|
|
4
|
+
* the two TextEncoder/TextDecoder classes available everywhere.
|
|
5
|
+
*/
|
|
6
|
+
declare function toHex(bytes: Uint8Array): string;
|
|
7
|
+
declare function fromHex(hex: string): Uint8Array;
|
|
8
|
+
declare function toBase64Url(bytes: Uint8Array): string;
|
|
9
|
+
declare function utf8ToBytes(str: string): Uint8Array;
|
|
10
|
+
declare function bytesToUtf8(bytes: Uint8Array): string;
|
|
11
|
+
|
|
12
|
+
export { bytesToUtf8, fromHex, toBase64Url, toHex, utf8ToBytes };
|
package/dist/encoding.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// src/encoding.ts
|
|
2
|
+
var HEX_CHARS = "0123456789abcdef";
|
|
3
|
+
function toHex(bytes) {
|
|
4
|
+
let out = "";
|
|
5
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
6
|
+
const b = bytes[i];
|
|
7
|
+
out += HEX_CHARS[b >>> 4];
|
|
8
|
+
out += HEX_CHARS[b & 15];
|
|
9
|
+
}
|
|
10
|
+
return out;
|
|
11
|
+
}
|
|
12
|
+
function fromHex(hex) {
|
|
13
|
+
const clean = hex.startsWith("0x") ? hex.slice(2) : hex;
|
|
14
|
+
if (clean.length % 2 !== 0) {
|
|
15
|
+
throw new Error("fromHex: odd-length hex input");
|
|
16
|
+
}
|
|
17
|
+
const out = new Uint8Array(clean.length / 2);
|
|
18
|
+
for (let i = 0; i < out.length; i++) {
|
|
19
|
+
const byte = Number.parseInt(clean.substr(i * 2, 2), 16);
|
|
20
|
+
if (Number.isNaN(byte)) {
|
|
21
|
+
throw new Error("fromHex: non-hex character");
|
|
22
|
+
}
|
|
23
|
+
out[i] = byte;
|
|
24
|
+
}
|
|
25
|
+
return out;
|
|
26
|
+
}
|
|
27
|
+
function toBase64Url(bytes) {
|
|
28
|
+
let binary = "";
|
|
29
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
30
|
+
binary += String.fromCharCode(bytes[i]);
|
|
31
|
+
}
|
|
32
|
+
return btoa(binary).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
33
|
+
}
|
|
34
|
+
function utf8ToBytes(str) {
|
|
35
|
+
return new TextEncoder().encode(str);
|
|
36
|
+
}
|
|
37
|
+
function bytesToUtf8(bytes) {
|
|
38
|
+
return new TextDecoder().decode(bytes);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export { bytesToUtf8, fromHex, toBase64Url, toHex, utf8ToBytes };
|
|
42
|
+
//# sourceMappingURL=encoding.js.map
|
|
43
|
+
//# sourceMappingURL=encoding.js.map
|
|
@@ -0,0 +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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,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"]}
|
package/dist/hash.cjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/encoding.ts
|
|
4
|
+
var HEX_CHARS = "0123456789abcdef";
|
|
5
|
+
function toHex(bytes) {
|
|
6
|
+
let out = "";
|
|
7
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
8
|
+
const b = bytes[i];
|
|
9
|
+
out += HEX_CHARS[b >>> 4];
|
|
10
|
+
out += HEX_CHARS[b & 15];
|
|
11
|
+
}
|
|
12
|
+
return out;
|
|
13
|
+
}
|
|
14
|
+
function utf8ToBytes(str) {
|
|
15
|
+
return new TextEncoder().encode(str);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// src/hash.ts
|
|
19
|
+
async function sha256(data) {
|
|
20
|
+
const bytes = typeof data === "string" ? utf8ToBytes(data) : data;
|
|
21
|
+
const digest = await subtle().digest("SHA-256", bytes);
|
|
22
|
+
return new Uint8Array(digest);
|
|
23
|
+
}
|
|
24
|
+
async function sha256Hex(data) {
|
|
25
|
+
return toHex(await sha256(data));
|
|
26
|
+
}
|
|
27
|
+
function subtle() {
|
|
28
|
+
const c = globalThis.crypto;
|
|
29
|
+
if (!c || !c.subtle) {
|
|
30
|
+
throw new Error("Web Crypto is not available in this runtime");
|
|
31
|
+
}
|
|
32
|
+
return c.subtle;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
exports.sha256 = sha256;
|
|
36
|
+
exports.sha256Hex = sha256Hex;
|
|
37
|
+
//# sourceMappingURL=hash.cjs.map
|
|
38
|
+
//# sourceMappingURL=hash.cjs.map
|
|
@@ -0,0 +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"]}
|
package/dist/hash.d.cts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SHA-256 via Web Crypto (available in Node 16+, Deno, Bun, Workers, Edge).
|
|
3
|
+
*/
|
|
4
|
+
declare function sha256(data: string | Uint8Array): Promise<Uint8Array>;
|
|
5
|
+
declare function sha256Hex(data: string | Uint8Array): Promise<string>;
|
|
6
|
+
|
|
7
|
+
export { sha256, sha256Hex };
|
package/dist/hash.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SHA-256 via Web Crypto (available in Node 16+, Deno, Bun, Workers, Edge).
|
|
3
|
+
*/
|
|
4
|
+
declare function sha256(data: string | Uint8Array): Promise<Uint8Array>;
|
|
5
|
+
declare function sha256Hex(data: string | Uint8Array): Promise<string>;
|
|
6
|
+
|
|
7
|
+
export { sha256, sha256Hex };
|
package/dist/hash.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// src/encoding.ts
|
|
2
|
+
var HEX_CHARS = "0123456789abcdef";
|
|
3
|
+
function toHex(bytes) {
|
|
4
|
+
let out = "";
|
|
5
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
6
|
+
const b = bytes[i];
|
|
7
|
+
out += HEX_CHARS[b >>> 4];
|
|
8
|
+
out += HEX_CHARS[b & 15];
|
|
9
|
+
}
|
|
10
|
+
return out;
|
|
11
|
+
}
|
|
12
|
+
function utf8ToBytes(str) {
|
|
13
|
+
return new TextEncoder().encode(str);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// src/hash.ts
|
|
17
|
+
async function sha256(data) {
|
|
18
|
+
const bytes = typeof data === "string" ? utf8ToBytes(data) : data;
|
|
19
|
+
const digest = await subtle().digest("SHA-256", bytes);
|
|
20
|
+
return new Uint8Array(digest);
|
|
21
|
+
}
|
|
22
|
+
async function sha256Hex(data) {
|
|
23
|
+
return toHex(await sha256(data));
|
|
24
|
+
}
|
|
25
|
+
function subtle() {
|
|
26
|
+
const c = globalThis.crypto;
|
|
27
|
+
if (!c || !c.subtle) {
|
|
28
|
+
throw new Error("Web Crypto is not available in this runtime");
|
|
29
|
+
}
|
|
30
|
+
return c.subtle;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export { sha256, sha256Hex };
|
|
34
|
+
//# sourceMappingURL=hash.js.map
|
|
35
|
+
//# sourceMappingURL=hash.js.map
|
package/dist/hash.js.map
ADDED
|
@@ -0,0 +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"]}
|
package/dist/hmac.cjs
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/encoding.ts
|
|
4
|
+
var HEX_CHARS = "0123456789abcdef";
|
|
5
|
+
function toHex(bytes) {
|
|
6
|
+
let out = "";
|
|
7
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
8
|
+
const b = bytes[i];
|
|
9
|
+
out += HEX_CHARS[b >>> 4];
|
|
10
|
+
out += HEX_CHARS[b & 15];
|
|
11
|
+
}
|
|
12
|
+
return out;
|
|
13
|
+
}
|
|
14
|
+
function utf8ToBytes(str) {
|
|
15
|
+
return new TextEncoder().encode(str);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// src/hmac.ts
|
|
19
|
+
async function hmacSha256(key, message) {
|
|
20
|
+
const keyBytes = typeof key === "string" ? utf8ToBytes(key) : key;
|
|
21
|
+
const msgBytes = typeof message === "string" ? utf8ToBytes(message) : message;
|
|
22
|
+
const subtle = requireSubtle();
|
|
23
|
+
const cryptoKey = await subtle.importKey(
|
|
24
|
+
"raw",
|
|
25
|
+
keyBytes,
|
|
26
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
27
|
+
false,
|
|
28
|
+
["sign"]
|
|
29
|
+
);
|
|
30
|
+
const signature = await subtle.sign("HMAC", cryptoKey, msgBytes);
|
|
31
|
+
return new Uint8Array(signature);
|
|
32
|
+
}
|
|
33
|
+
async function hmacSha256Hex(key, message) {
|
|
34
|
+
return toHex(await hmacSha256(key, message));
|
|
35
|
+
}
|
|
36
|
+
function requireSubtle() {
|
|
37
|
+
const c = globalThis.crypto;
|
|
38
|
+
if (!c || !c.subtle) {
|
|
39
|
+
throw new Error("Web Crypto is not available in this runtime");
|
|
40
|
+
}
|
|
41
|
+
return c.subtle;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
exports.hmacSha256 = hmacSha256;
|
|
45
|
+
exports.hmacSha256Hex = hmacSha256Hex;
|
|
46
|
+
//# sourceMappingURL=hmac.cjs.map
|
|
47
|
+
//# sourceMappingURL=hmac.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/encoding.ts","../src/hmac.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;;;ACvCA,eAAsB,UAAA,CACpB,KACA,OAAA,EACqB;AACrB,EAAA,MAAM,WAAW,OAAO,GAAA,KAAQ,QAAA,GAAW,WAAA,CAAY,GAAG,CAAA,GAAI,GAAA;AAC9D,EAAA,MAAM,WAAW,OAAO,OAAA,KAAY,QAAA,GAAW,WAAA,CAAY,OAAO,CAAA,GAAI,OAAA;AACtE,EAAA,MAAM,SAAS,aAAA,EAAc;AAE7B,EAAA,MAAM,SAAA,GAAY,MAAM,MAAA,CAAO,SAAA;AAAA,IAC7B,KAAA;AAAA,IACA,QAAA;AAAA,IACA,EAAE,IAAA,EAAM,MAAA,EAAQ,IAAA,EAAM,SAAA,EAAU;AAAA,IAChC,KAAA;AAAA,IACA,CAAC,MAAM;AAAA,GACT;AACA,EAAA,MAAM,YAAY,MAAM,MAAA,CAAO,IAAA,CAAK,MAAA,EAAQ,WAAW,QAAwB,CAAA;AAC/E,EAAA,OAAO,IAAI,WAAW,SAAS,CAAA;AACjC;AAEA,eAAsB,aAAA,CACpB,KACA,OAAA,EACiB;AACjB,EAAA,OAAO,KAAA,CAAM,MAAM,UAAA,CAAW,GAAA,EAAK,OAAO,CAAC,CAAA;AAC7C;AAEA,SAAS,aAAA,GAA8B;AACrC,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":"hmac.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 * HMAC-SHA256 via Web Crypto.\n * Keys accept either a string (UTF-8) or raw bytes.\n */\n\nimport { toHex, utf8ToBytes } from './encoding.js'\n\nexport async function hmacSha256(\n key: string | Uint8Array,\n message: string | Uint8Array,\n): Promise<Uint8Array> {\n const keyBytes = typeof key === 'string' ? utf8ToBytes(key) : key\n const msgBytes = typeof message === 'string' ? utf8ToBytes(message) : message\n const subtle = requireSubtle()\n\n const cryptoKey = await subtle.importKey(\n 'raw',\n keyBytes as BufferSource,\n { name: 'HMAC', hash: 'SHA-256' },\n false,\n ['sign'],\n )\n const signature = await subtle.sign('HMAC', cryptoKey, msgBytes as BufferSource)\n return new Uint8Array(signature)\n}\n\nexport async function hmacSha256Hex(\n key: string | Uint8Array,\n message: string | Uint8Array,\n): Promise<string> {\n return toHex(await hmacSha256(key, message))\n}\n\nfunction requireSubtle(): 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/hmac.d.cts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HMAC-SHA256 via Web Crypto.
|
|
3
|
+
* Keys accept either a string (UTF-8) or raw bytes.
|
|
4
|
+
*/
|
|
5
|
+
declare function hmacSha256(key: string | Uint8Array, message: string | Uint8Array): Promise<Uint8Array>;
|
|
6
|
+
declare function hmacSha256Hex(key: string | Uint8Array, message: string | Uint8Array): Promise<string>;
|
|
7
|
+
|
|
8
|
+
export { hmacSha256, hmacSha256Hex };
|
package/dist/hmac.d.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* HMAC-SHA256 via Web Crypto.
|
|
3
|
+
* Keys accept either a string (UTF-8) or raw bytes.
|
|
4
|
+
*/
|
|
5
|
+
declare function hmacSha256(key: string | Uint8Array, message: string | Uint8Array): Promise<Uint8Array>;
|
|
6
|
+
declare function hmacSha256Hex(key: string | Uint8Array, message: string | Uint8Array): Promise<string>;
|
|
7
|
+
|
|
8
|
+
export { hmacSha256, hmacSha256Hex };
|
package/dist/hmac.js
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// src/encoding.ts
|
|
2
|
+
var HEX_CHARS = "0123456789abcdef";
|
|
3
|
+
function toHex(bytes) {
|
|
4
|
+
let out = "";
|
|
5
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
6
|
+
const b = bytes[i];
|
|
7
|
+
out += HEX_CHARS[b >>> 4];
|
|
8
|
+
out += HEX_CHARS[b & 15];
|
|
9
|
+
}
|
|
10
|
+
return out;
|
|
11
|
+
}
|
|
12
|
+
function utf8ToBytes(str) {
|
|
13
|
+
return new TextEncoder().encode(str);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// src/hmac.ts
|
|
17
|
+
async function hmacSha256(key, message) {
|
|
18
|
+
const keyBytes = typeof key === "string" ? utf8ToBytes(key) : key;
|
|
19
|
+
const msgBytes = typeof message === "string" ? utf8ToBytes(message) : message;
|
|
20
|
+
const subtle = requireSubtle();
|
|
21
|
+
const cryptoKey = await subtle.importKey(
|
|
22
|
+
"raw",
|
|
23
|
+
keyBytes,
|
|
24
|
+
{ name: "HMAC", hash: "SHA-256" },
|
|
25
|
+
false,
|
|
26
|
+
["sign"]
|
|
27
|
+
);
|
|
28
|
+
const signature = await subtle.sign("HMAC", cryptoKey, msgBytes);
|
|
29
|
+
return new Uint8Array(signature);
|
|
30
|
+
}
|
|
31
|
+
async function hmacSha256Hex(key, message) {
|
|
32
|
+
return toHex(await hmacSha256(key, message));
|
|
33
|
+
}
|
|
34
|
+
function requireSubtle() {
|
|
35
|
+
const c = globalThis.crypto;
|
|
36
|
+
if (!c || !c.subtle) {
|
|
37
|
+
throw new Error("Web Crypto is not available in this runtime");
|
|
38
|
+
}
|
|
39
|
+
return c.subtle;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export { hmacSha256, hmacSha256Hex };
|
|
43
|
+
//# sourceMappingURL=hmac.js.map
|
|
44
|
+
//# sourceMappingURL=hmac.js.map
|
package/dist/hmac.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/encoding.ts","../src/hmac.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;;;ACvCA,eAAsB,UAAA,CACpB,KACA,OAAA,EACqB;AACrB,EAAA,MAAM,WAAW,OAAO,GAAA,KAAQ,QAAA,GAAW,WAAA,CAAY,GAAG,CAAA,GAAI,GAAA;AAC9D,EAAA,MAAM,WAAW,OAAO,OAAA,KAAY,QAAA,GAAW,WAAA,CAAY,OAAO,CAAA,GAAI,OAAA;AACtE,EAAA,MAAM,SAAS,aAAA,EAAc;AAE7B,EAAA,MAAM,SAAA,GAAY,MAAM,MAAA,CAAO,SAAA;AAAA,IAC7B,KAAA;AAAA,IACA,QAAA;AAAA,IACA,EAAE,IAAA,EAAM,MAAA,EAAQ,IAAA,EAAM,SAAA,EAAU;AAAA,IAChC,KAAA;AAAA,IACA,CAAC,MAAM;AAAA,GACT;AACA,EAAA,MAAM,YAAY,MAAM,MAAA,CAAO,IAAA,CAAK,MAAA,EAAQ,WAAW,QAAwB,CAAA;AAC/E,EAAA,OAAO,IAAI,WAAW,SAAS,CAAA;AACjC;AAEA,eAAsB,aAAA,CACpB,KACA,OAAA,EACiB;AACjB,EAAA,OAAO,KAAA,CAAM,MAAM,UAAA,CAAW,GAAA,EAAK,OAAO,CAAC,CAAA;AAC7C;AAEA,SAAS,aAAA,GAA8B;AACrC,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":"hmac.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 * HMAC-SHA256 via Web Crypto.\n * Keys accept either a string (UTF-8) or raw bytes.\n */\n\nimport { toHex, utf8ToBytes } from './encoding.js'\n\nexport async function hmacSha256(\n key: string | Uint8Array,\n message: string | Uint8Array,\n): Promise<Uint8Array> {\n const keyBytes = typeof key === 'string' ? utf8ToBytes(key) : key\n const msgBytes = typeof message === 'string' ? utf8ToBytes(message) : message\n const subtle = requireSubtle()\n\n const cryptoKey = await subtle.importKey(\n 'raw',\n keyBytes as BufferSource,\n { name: 'HMAC', hash: 'SHA-256' },\n false,\n ['sign'],\n )\n const signature = await subtle.sign('HMAC', cryptoKey, msgBytes as BufferSource)\n return new Uint8Array(signature)\n}\n\nexport async function hmacSha256Hex(\n key: string | Uint8Array,\n message: string | Uint8Array,\n): Promise<string> {\n return toHex(await hmacSha256(key, message))\n}\n\nfunction requireSubtle(): 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"]}
|