@crabcrop/mcp 0.1.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,248 @@
1
+ /**
2
+ * AES-256 (FIPS 197) in GCM mode (NIST SP 800-38D) with a
3
+ * 96-bit nonce and a 128-bit tag, in plain TypeScript, for
4
+ * runtimes without `node:crypto`. Like SHA-256 here, it backs
5
+ * synchronous functions (`seal`, `unseal`), which WebCrypto's
6
+ * asynchronous `subtle.encrypt` cannot.
7
+ *
8
+ * GHASH is computed with masks rather than branches. The AES
9
+ * rounds index the S-box by key-dependent bytes, so they are
10
+ * not hardened against cache-timing observers; Node keeps
11
+ * OpenSSL through the package's `node` condition, and this
12
+ * implementation serves the browser and edge builds.
13
+ */
14
+ const SBOX = new Uint8Array(256);
15
+ {
16
+ // The S-box is the multiplicative inverse in GF(2^8)
17
+ // followed by the FIPS 197 affine map. Walk p over the
18
+ // powers of the generator 3 while q walks the matching
19
+ // inverses (powers of 3^-1), so sbox[p] = affine(q).
20
+ let p = 1;
21
+ let q = 1;
22
+ do {
23
+ p = p ^ ((p << 1) & 0xff) ^ (p & 0x80 ? 0x1b : 0);
24
+ q ^= q << 1;
25
+ q ^= q << 2;
26
+ q ^= q << 4;
27
+ q &= 0xff;
28
+ if (q & 0x80) {
29
+ q ^= 0x09;
30
+ }
31
+ const rotl = (x, n) => ((x << n) | (x >>> (8 - n))) & 0xff;
32
+ SBOX[p] = q ^ rotl(q, 1) ^ rotl(q, 2) ^ rotl(q, 3) ^ rotl(q, 4) ^ 0x63;
33
+ } while (p !== 1);
34
+ SBOX[0] = 0x63;
35
+ }
36
+ /** Multiply by x in GF(2^8), without a branch on the high bit. */
37
+ const xtime = (x) => ((x << 1) ^ ((x >>> 7) * 0x1b)) & 0xff;
38
+ const ROUNDS = 14;
39
+ /** The AES-256 key schedule: 15 round keys, 240 bytes. */
40
+ function expandKey(key) {
41
+ if (key.length !== 32) {
42
+ throw new Error(`AES-256 needs a 32-byte key, got ${key.length}`);
43
+ }
44
+ const w = new Uint8Array(16 * (ROUNDS + 1));
45
+ w.set(key);
46
+ let rcon = 1;
47
+ for (let i = 32; i < w.length; i += 4) {
48
+ let t0 = w[i - 4];
49
+ let t1 = w[i - 3];
50
+ let t2 = w[i - 2];
51
+ let t3 = w[i - 1];
52
+ if (i % 32 === 0) {
53
+ // RotWord, SubWord, and the round constant.
54
+ const first = t0;
55
+ t0 = SBOX[t1] ^ rcon;
56
+ t1 = SBOX[t2];
57
+ t2 = SBOX[t3];
58
+ t3 = SBOX[first];
59
+ rcon = xtime(rcon);
60
+ }
61
+ else if (i % 32 === 16) {
62
+ t0 = SBOX[t0];
63
+ t1 = SBOX[t1];
64
+ t2 = SBOX[t2];
65
+ t3 = SBOX[t3];
66
+ }
67
+ w[i] = w[i - 32] ^ t0;
68
+ w[i + 1] = w[i - 31] ^ t1;
69
+ w[i + 2] = w[i - 30] ^ t2;
70
+ w[i + 3] = w[i - 29] ^ t3;
71
+ }
72
+ return w;
73
+ }
74
+ /** Encrypt one 16-byte block from `input` into `output`. */
75
+ function encryptBlock(roundKeys, input, output) {
76
+ // The state is column-major: byte r of column c is s[r + 4c].
77
+ const s = new Uint8Array(16);
78
+ const t = new Uint8Array(16);
79
+ for (let i = 0; i < 16; i++) {
80
+ s[i] = input[i] ^ roundKeys[i];
81
+ }
82
+ for (let round = 1; round <= ROUNDS; round++) {
83
+ // SubBytes and ShiftRows: row r rotates left by r columns.
84
+ for (let c = 0; c < 4; c++) {
85
+ for (let r = 0; r < 4; r++) {
86
+ t[r + 4 * c] = SBOX[s[r + 4 * ((c + r) & 3)]];
87
+ }
88
+ }
89
+ if (round < ROUNDS) {
90
+ for (let c = 0; c < 16; c += 4) {
91
+ const a0 = t[c];
92
+ const a1 = t[c + 1];
93
+ const a2 = t[c + 2];
94
+ const a3 = t[c + 3];
95
+ const all = a0 ^ a1 ^ a2 ^ a3;
96
+ // MixColumns: b_i = a_i ^ all ^ 2·(a_i ^ a_{i+1}).
97
+ t[c] = a0 ^ all ^ xtime(a0 ^ a1);
98
+ t[c + 1] = a1 ^ all ^ xtime(a1 ^ a2);
99
+ t[c + 2] = a2 ^ all ^ xtime(a2 ^ a3);
100
+ t[c + 3] = a3 ^ all ^ xtime(a3 ^ a0);
101
+ }
102
+ }
103
+ const k = round * 16;
104
+ for (let i = 0; i < 16; i++) {
105
+ s[i] = t[i] ^ roundKeys[k + i];
106
+ }
107
+ }
108
+ output.set(s);
109
+ }
110
+ /**
111
+ * Multiply two GF(2^128) elements in GCM's bit order, each as
112
+ * four big-endian 32-bit words (SP 800-38D, Algorithm 1). The
113
+ * loop runs all 128 steps with masks, never a branch on a
114
+ * secret bit.
115
+ */
116
+ function gmul(x, h) {
117
+ let z0 = 0;
118
+ let z1 = 0;
119
+ let z2 = 0;
120
+ let z3 = 0;
121
+ let v0 = h[0];
122
+ let v1 = h[1];
123
+ let v2 = h[2];
124
+ let v3 = h[3];
125
+ for (let i = 0; i < 128; i++) {
126
+ const bit = (x[i >>> 5] >>> (31 - (i & 31))) & 1;
127
+ const take = -bit;
128
+ z0 ^= v0 & take;
129
+ z1 ^= v1 & take;
130
+ z2 ^= v2 & take;
131
+ z3 ^= v3 & take;
132
+ const reduce = -(v3 & 1);
133
+ v3 = (v3 >>> 1) | (v2 << 31);
134
+ v2 = (v2 >>> 1) | (v1 << 31);
135
+ v1 = (v1 >>> 1) | (v0 << 31);
136
+ v0 = (v0 >>> 1) ^ (0xe1000000 & reduce);
137
+ }
138
+ x[0] = z0;
139
+ x[1] = z1;
140
+ x[2] = z2;
141
+ x[3] = z3;
142
+ }
143
+ function readWords(bytes) {
144
+ const view = new DataView(bytes.buffer, bytes.byteOffset, 16);
145
+ return Int32Array.of(view.getInt32(0), view.getInt32(4), view.getInt32(8), view.getInt32(12));
146
+ }
147
+ /** GHASH_H over the AAD and ciphertext, then their bit lengths. */
148
+ function ghash(h, aad, ciphertext) {
149
+ const hw = readWords(h);
150
+ const x = new Int32Array(4);
151
+ const block = new Uint8Array(16);
152
+ const absorb = (data) => {
153
+ for (let off = 0; off < data.length; off += 16) {
154
+ block.fill(0);
155
+ block.set(data.subarray(off, off + 16));
156
+ const bw = readWords(block);
157
+ for (let i = 0; i < 4; i++) {
158
+ x[i] ^= bw[i];
159
+ }
160
+ gmul(x, hw);
161
+ }
162
+ };
163
+ absorb(aad);
164
+ absorb(ciphertext);
165
+ const lengths = new DataView(block.buffer);
166
+ lengths.setUint32(0, Math.floor(aad.length / 0x20000000));
167
+ lengths.setUint32(4, (aad.length << 3) >>> 0);
168
+ lengths.setUint32(8, Math.floor(ciphertext.length / 0x20000000));
169
+ lengths.setUint32(12, (ciphertext.length << 3) >>> 0);
170
+ const lw = readWords(block);
171
+ for (let i = 0; i < 4; i++) {
172
+ x[i] ^= lw[i];
173
+ }
174
+ gmul(x, hw);
175
+ const out = new Uint8Array(16);
176
+ const outView = new DataView(out.buffer);
177
+ for (let i = 0; i < 4; i++) {
178
+ outView.setInt32(i * 4, x[i]);
179
+ }
180
+ return out;
181
+ }
182
+ /** CTR mode from inc32(J0), XORing `input` into `output`. */
183
+ function ctr(roundKeys, j0, input, output) {
184
+ const counter = j0.slice();
185
+ const view = new DataView(counter.buffer);
186
+ const stream = new Uint8Array(16);
187
+ for (let off = 0; off < input.length; off += 16) {
188
+ view.setUint32(12, (view.getUint32(12) + 1) >>> 0);
189
+ encryptBlock(roundKeys, counter, stream);
190
+ const end = Math.min(off + 16, input.length);
191
+ for (let i = off; i < end; i++) {
192
+ output[i] = input[i] ^ stream[i - off];
193
+ }
194
+ }
195
+ }
196
+ function setup(key, nonce) {
197
+ if (nonce.length !== 12) {
198
+ throw new Error(`AES-GCM here takes a 12-byte nonce, got ${nonce.length}`);
199
+ }
200
+ const roundKeys = expandKey(key);
201
+ const h = new Uint8Array(16);
202
+ encryptBlock(roundKeys, new Uint8Array(16), h);
203
+ const j0 = new Uint8Array(16);
204
+ j0.set(nonce);
205
+ j0[15] = 1;
206
+ return { roundKeys, h, j0 };
207
+ }
208
+ /** The tag: E(K, J0) XOR GHASH(A, C). */
209
+ function tagFor(g, aad, ciphertext) {
210
+ const tag = ghash(g.h, aad, ciphertext);
211
+ const mask = new Uint8Array(16);
212
+ encryptBlock(g.roundKeys, g.j0, mask);
213
+ for (let i = 0; i < 16; i++) {
214
+ tag[i] ^= mask[i];
215
+ }
216
+ return tag;
217
+ }
218
+ /** AES-256-GCM encryption: returns ciphertext || 16-byte tag. */
219
+ export function aes256GcmSeal(key, nonce, aad, plaintext) {
220
+ const g = setup(key, nonce);
221
+ const out = new Uint8Array(plaintext.length + 16);
222
+ ctr(g.roundKeys, g.j0, plaintext, out);
223
+ out.set(tagFor(g, aad, out.subarray(0, plaintext.length)), plaintext.length);
224
+ return out;
225
+ }
226
+ /**
227
+ * AES-256-GCM decryption of ciphertext || tag. Returns the
228
+ * plaintext, or null when the tag does not verify; nothing is
229
+ * decrypted before the tag is checked.
230
+ */
231
+ export function aes256GcmOpen(key, nonce, aad, sealed) {
232
+ if (sealed.length < 16) {
233
+ return null;
234
+ }
235
+ const g = setup(key, nonce);
236
+ const ciphertext = sealed.subarray(0, sealed.length - 16);
237
+ const expected = tagFor(g, aad, ciphertext);
238
+ let diff = 0;
239
+ for (let i = 0; i < 16; i++) {
240
+ diff |= expected[i] ^ sealed[ciphertext.length + i];
241
+ }
242
+ if (diff !== 0) {
243
+ return null;
244
+ }
245
+ const plaintext = new Uint8Array(ciphertext.length);
246
+ ctr(g.roundKeys, g.j0, ciphertext, plaintext);
247
+ return plaintext;
248
+ }
@@ -0,0 +1,11 @@
1
+ export declare const backend = "node";
2
+ /** HMAC-SHA-256 of the UTF-8 `message` under `key`. */
3
+ export declare function hmacSha256(key: string | Uint8Array, message: string): Uint8Array;
4
+ /** AES-256-GCM: ciphertext || 16-byte tag. */
5
+ export declare function aes256GcmSeal(key: Uint8Array, nonce: Uint8Array, aad: Uint8Array, plaintext: Uint8Array): Uint8Array;
6
+ /** AES-256-GCM: the plaintext, or null when the tag does not verify. */
7
+ export declare function aes256GcmOpen(key: Uint8Array, nonce: Uint8Array, aad: Uint8Array, sealed: Uint8Array): Uint8Array | null;
8
+ /** `n` bytes from OpenSSL's CSPRNG. */
9
+ export declare function randomBytes(n: number): Uint8Array;
10
+ /** Constant-time equality of two byte strings of equal length. */
11
+ export declare function timingSafeEqual(a: Uint8Array, b: Uint8Array): boolean;
@@ -0,0 +1,41 @@
1
+ /**
2
+ * The Node crypto backend: OpenSSL through `node:crypto`.
3
+ * Package resolution picks it through the `#crypto` import's
4
+ * `node` condition; bundles for browsers take `./portable.ts`,
5
+ * which exports the same functions.
6
+ */
7
+ import { createCipheriv, createDecipheriv, createHmac, randomBytes as nodeRandomBytes, timingSafeEqual as nodeTimingSafeEqual, } from "node:crypto";
8
+ export const backend = "node";
9
+ /** HMAC-SHA-256 of the UTF-8 `message` under `key`. */
10
+ export function hmacSha256(key, message) {
11
+ return createHmac("sha256", key).update(message, "utf8").digest();
12
+ }
13
+ /** AES-256-GCM: ciphertext || 16-byte tag. */
14
+ export function aes256GcmSeal(key, nonce, aad, plaintext) {
15
+ const cipher = createCipheriv("aes-256-gcm", key, nonce);
16
+ cipher.setAAD(aad);
17
+ return Buffer.concat([cipher.update(plaintext), cipher.final(), cipher.getAuthTag()]);
18
+ }
19
+ /** AES-256-GCM: the plaintext, or null when the tag does not verify. */
20
+ export function aes256GcmOpen(key, nonce, aad, sealed) {
21
+ if (sealed.length < 16) {
22
+ return null;
23
+ }
24
+ const decipher = createDecipheriv("aes-256-gcm", key, nonce);
25
+ decipher.setAAD(aad);
26
+ decipher.setAuthTag(sealed.subarray(sealed.length - 16));
27
+ try {
28
+ return Buffer.concat([decipher.update(sealed.subarray(0, sealed.length - 16)), decipher.final()]);
29
+ }
30
+ catch {
31
+ return null;
32
+ }
33
+ }
34
+ /** `n` bytes from OpenSSL's CSPRNG. */
35
+ export function randomBytes(n) {
36
+ return nodeRandomBytes(n);
37
+ }
38
+ /** Constant-time equality of two byte strings of equal length. */
39
+ export function timingSafeEqual(a, b) {
40
+ return a.length === b.length && nodeTimingSafeEqual(a, b);
41
+ }
@@ -0,0 +1,14 @@
1
+ export declare const backend = "portable";
2
+ /**
3
+ * HMAC-SHA-256 of the UTF-8 `message` under `key` (a string
4
+ * is UTF-8 encoded, as `node:crypto` does).
5
+ */
6
+ export declare function hmacSha256(key: string | Uint8Array, message: string): Uint8Array;
7
+ /** AES-256-GCM: ciphertext || 16-byte tag. */
8
+ export declare function aes256GcmSeal(key: Uint8Array, nonce: Uint8Array, aad: Uint8Array, plaintext: Uint8Array): Uint8Array;
9
+ /** AES-256-GCM: the plaintext, or null when the tag does not verify. */
10
+ export declare function aes256GcmOpen(key: Uint8Array, nonce: Uint8Array, aad: Uint8Array, sealed: Uint8Array): Uint8Array | null;
11
+ /** `n` bytes from the platform's CSPRNG (`crypto.getRandomValues`). */
12
+ export declare function randomBytes(n: number): Uint8Array;
13
+ /** Constant-time equality of two byte strings of equal length. */
14
+ export declare function timingSafeEqual(a: Uint8Array, b: Uint8Array): boolean;
@@ -0,0 +1,60 @@
1
+ /**
2
+ * The portable crypto backend: plain TypeScript, no Node
3
+ * built-ins. Package resolution picks it through the
4
+ * `#crypto` import's `browser` and `default` conditions, so
5
+ * browser bundles (Vite, webpack, esbuild) and edge runtimes
6
+ * get a working, synchronous SDK; Node takes `./node.ts`.
7
+ * Both modules export the same functions with the same
8
+ * results, which the test suite checks against `node:crypto`.
9
+ */
10
+ import { aes256GcmOpen as open, aes256GcmSeal as seal } from "./aes-gcm.js";
11
+ import { hmacSha256 as hmac } from "./sha256.js";
12
+ export const backend = "portable";
13
+ const encoder = new TextEncoder();
14
+ /**
15
+ * HMAC-SHA-256 of the UTF-8 `message` under `key` (a string
16
+ * is UTF-8 encoded, as `node:crypto` does).
17
+ */
18
+ export function hmacSha256(key, message) {
19
+ let keyBytes;
20
+ if (typeof key === "string") {
21
+ keyBytes = encoder.encode(key);
22
+ }
23
+ else if (key instanceof Uint8Array) {
24
+ keyBytes = key;
25
+ }
26
+ else {
27
+ // node:crypto refuses a missing or non-string key the
28
+ // same way, rather than signing with "undefined".
29
+ throw new TypeError(`the HMAC key must be a string or a Uint8Array, got ${typeof key}`);
30
+ }
31
+ return hmac(keyBytes, encoder.encode(message));
32
+ }
33
+ /** AES-256-GCM: ciphertext || 16-byte tag. */
34
+ export function aes256GcmSeal(key, nonce, aad, plaintext) {
35
+ return seal(key, nonce, aad, plaintext);
36
+ }
37
+ /** AES-256-GCM: the plaintext, or null when the tag does not verify. */
38
+ export function aes256GcmOpen(key, nonce, aad, sealed) {
39
+ return open(key, nonce, aad, sealed);
40
+ }
41
+ /** `n` bytes from the platform's CSPRNG (`crypto.getRandomValues`). */
42
+ export function randomBytes(n) {
43
+ const source = globalThis
44
+ .crypto;
45
+ if (typeof source?.getRandomValues !== "function") {
46
+ throw new Error("no secure random source: crypto.getRandomValues is unavailable here");
47
+ }
48
+ return source.getRandomValues(new Uint8Array(n));
49
+ }
50
+ /** Constant-time equality of two byte strings of equal length. */
51
+ export function timingSafeEqual(a, b) {
52
+ if (a.length !== b.length) {
53
+ return false;
54
+ }
55
+ let diff = 0;
56
+ for (let i = 0; i < a.length; i++) {
57
+ diff |= a[i] ^ b[i];
58
+ }
59
+ return diff === 0;
60
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * SHA-256 (FIPS 180-4) and HMAC-SHA-256 (RFC 2104) in plain
3
+ * TypeScript, for runtimes without `node:crypto`. WebCrypto
4
+ * cannot stand in: `crypto.subtle` is asynchronous, and the
5
+ * SDK's signing functions are synchronous.
6
+ *
7
+ * All arithmetic is on 32-bit words with no data-dependent
8
+ * branches or table indices, so the time taken depends only
9
+ * on the input lengths.
10
+ */
11
+ /** The SHA-256 digest of `data`. */
12
+ export declare function sha256(data: Uint8Array): Uint8Array;
13
+ /** HMAC-SHA-256 of `message` under `key` (RFC 2104). */
14
+ export declare function hmacSha256(key: Uint8Array, message: Uint8Array): Uint8Array;
@@ -0,0 +1,131 @@
1
+ /**
2
+ * SHA-256 (FIPS 180-4) and HMAC-SHA-256 (RFC 2104) in plain
3
+ * TypeScript, for runtimes without `node:crypto`. WebCrypto
4
+ * cannot stand in: `crypto.subtle` is asynchronous, and the
5
+ * SDK's signing functions are synchronous.
6
+ *
7
+ * All arithmetic is on 32-bit words with no data-dependent
8
+ * branches or table indices, so the time taken depends only
9
+ * on the input lengths.
10
+ */
11
+ // Int32Array keeps every word a small integer to V8 (a
12
+ // Uint32Array would hand back doubles above 2^31).
13
+ const K = new Int32Array([
14
+ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
15
+ 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
16
+ 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
17
+ 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
18
+ 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
19
+ 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
20
+ 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
21
+ 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2,
22
+ ]);
23
+ const BLOCK = 64;
24
+ // Scratch space, reused across calls: the functions here are
25
+ // synchronous and never re-entered, and reusing it keeps a
26
+ // signature from allocating a dozen typed arrays.
27
+ const W = new Int32Array(64);
28
+ const TAIL = new Uint8Array(2 * BLOCK);
29
+ const TAIL_VIEW = new DataView(TAIL.buffer);
30
+ const PAD = new Uint8Array(BLOCK);
31
+ /** One compression round over the 64-byte block at `offset`. */
32
+ function compress(state, view, offset) {
33
+ const w = W;
34
+ for (let i = 0; i < 16; i++) {
35
+ w[i] = view.getInt32(offset + i * 4);
36
+ }
37
+ for (let i = 16; i < 64; i++) {
38
+ const a = w[i - 15];
39
+ const b = w[i - 2];
40
+ const s0 = ((a >>> 7) | (a << 25)) ^ ((a >>> 18) | (a << 14)) ^ (a >>> 3);
41
+ const s1 = ((b >>> 17) | (b << 15)) ^ ((b >>> 19) | (b << 13)) ^ (b >>> 10);
42
+ w[i] = (w[i - 16] + s0 + w[i - 7] + s1) | 0;
43
+ }
44
+ let a = state[0];
45
+ let b = state[1];
46
+ let c = state[2];
47
+ let d = state[3];
48
+ let e = state[4];
49
+ let f = state[5];
50
+ let g = state[6];
51
+ let h = state[7];
52
+ for (let i = 0; i < 64; i++) {
53
+ const S1 = ((e >>> 6) | (e << 26)) ^ ((e >>> 11) | (e << 21)) ^ ((e >>> 25) | (e << 7));
54
+ const ch = (e & f) ^ (~e & g);
55
+ const t1 = (h + S1 + ch + K[i] + w[i]) | 0;
56
+ const S0 = ((a >>> 2) | (a << 30)) ^ ((a >>> 13) | (a << 19)) ^ ((a >>> 22) | (a << 10));
57
+ const maj = (a & b) ^ (a & c) ^ (b & c);
58
+ const t2 = (S0 + maj) | 0;
59
+ h = g;
60
+ g = f;
61
+ f = e;
62
+ e = (d + t1) | 0;
63
+ d = c;
64
+ c = b;
65
+ b = a;
66
+ a = (t1 + t2) | 0;
67
+ }
68
+ state[0] = (state[0] + a) | 0;
69
+ state[1] = (state[1] + b) | 0;
70
+ state[2] = (state[2] + c) | 0;
71
+ state[3] = (state[3] + d) | 0;
72
+ state[4] = (state[4] + e) | 0;
73
+ state[5] = (state[5] + f) | 0;
74
+ state[6] = (state[6] + g) | 0;
75
+ state[7] = (state[7] + h) | 0;
76
+ }
77
+ /**
78
+ * SHA-256 of `prefix || data`, where `prefix` is one whole
79
+ * block or absent: HMAC's padded keys go in without copying
80
+ * the message behind them.
81
+ */
82
+ function digest(prefix, data) {
83
+ const state = Int32Array.of(0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19);
84
+ if (prefix !== null) {
85
+ compress(state, new DataView(prefix.buffer, prefix.byteOffset, BLOCK), 0);
86
+ }
87
+ const view = new DataView(data.buffer, data.byteOffset, data.byteLength);
88
+ const whole = data.length - (data.length % BLOCK);
89
+ for (let offset = 0; offset < whole; offset += BLOCK) {
90
+ compress(state, view, offset);
91
+ }
92
+ // The tail: the leftover bytes, a 0x80 byte, zeros, and the
93
+ // message length in bits as a big-endian 64-bit integer,
94
+ // split into words without leaving the safe-integer range.
95
+ const rest = data.length - whole;
96
+ const tailLength = rest < BLOCK - 8 ? BLOCK : 2 * BLOCK;
97
+ const total = data.length + (prefix === null ? 0 : BLOCK);
98
+ TAIL.fill(0);
99
+ TAIL.set(data.subarray(whole));
100
+ TAIL[rest] = 0x80;
101
+ TAIL_VIEW.setUint32(tailLength - 8, Math.floor(total / 0x20000000));
102
+ TAIL_VIEW.setUint32(tailLength - 4, (total << 3) >>> 0);
103
+ compress(state, TAIL_VIEW, 0);
104
+ if (tailLength === 2 * BLOCK) {
105
+ compress(state, TAIL_VIEW, BLOCK);
106
+ }
107
+ const out = new Uint8Array(32);
108
+ const outView = new DataView(out.buffer);
109
+ for (let i = 0; i < 8; i++) {
110
+ outView.setInt32(i * 4, state[i]);
111
+ }
112
+ return out;
113
+ }
114
+ /** The SHA-256 digest of `data`. */
115
+ export function sha256(data) {
116
+ return digest(null, data);
117
+ }
118
+ /** HMAC-SHA-256 of `message` under `key` (RFC 2104). */
119
+ export function hmacSha256(key, message) {
120
+ const k = key.length > BLOCK ? sha256(key) : key;
121
+ PAD.fill(0x36);
122
+ for (let i = 0; i < k.length; i++) {
123
+ PAD[i] ^= k[i];
124
+ }
125
+ const inner = digest(PAD, message);
126
+ // opad = ipad ^ (0x36 ^ 0x5c), byte for byte.
127
+ for (let i = 0; i < BLOCK; i++) {
128
+ PAD[i] ^= 0x36 ^ 0x5c;
129
+ }
130
+ return digest(PAD, inner);
131
+ }
@@ -1,14 +1,7 @@
1
- /**
2
- * Parse a crabcrop URL (or bare path) back into its pieces and
3
- * explain each segment, mirroring the server's grammar. This is
4
- * the canonical implementation: the playground keeps a plain-JS
5
- * copy for its no-build static page, and both are pinned to the
6
- * same verdict corpus (audit PL-10, P-3).
7
- */
8
1
  /** The segment kinds an explanation is made of. */
9
2
  export type SegmentKind = "expiry" | "maxage" | "debug" | "preset" | "operation";
10
3
  /** The signature kinds an explanation can report. */
11
- export type SignatureKind = "hmac" | "unsafe" | "invalid";
4
+ export type SignatureKind = "hmac" | "unsafe" | "sealed" | "invalid";
12
5
  export interface ExplainedSegment {
13
6
  raw: string;
14
7
  kind: SegmentKind;
@@ -30,6 +23,61 @@ export interface Explanation {
30
23
  errors: string[];
31
24
  }
32
25
  export declare const FILTER_DESCRIPTIONS: Record<string, string>;
26
+ /** Crop alignments (url.rs parse_align); `center` is the bare `crop:WxH`. */
27
+ export declare const CROP_ALIGNS: readonly ["center", "smart", "face", "top", "bottom", "left", "right", "top-left", "top-right", "bottom-left", "bottom-right"];
28
+ /** Text overlay anchors (url.rs Anchor::parse). */
29
+ export declare const TEXT_ANCHORS: readonly ["top-left", "top-center", "top-right", "center-left", "center", "center-right", "bottom-left", "bottom-center", "bottom-right"];
30
+ /** Output formats `format(...)` names; `jpg` is an input alias of `jpeg`. */
31
+ export declare const IMAGE_FORMATS: readonly ["jpeg", "jpg", "png", "webp", "jxl", "avif", "gif", "mp4", "webm"];
32
+ /**
33
+ * Formats a per-format `quality(...)` entry names, in
34
+ * canonical (name) order: the still formats, since a video
35
+ * format's quality is not the server's to set. `jpg` is
36
+ * accepted as the input alias of `jpeg`.
37
+ */
38
+ export declare const QUALITY_FORMATS: readonly ["avif", "gif", "jpeg", "jxl", "png", "webp"];
39
+ /** JPEG chroma subsamplings `format(jpeg,subsampling:...)` takes. */
40
+ export declare const JPEG_SUBSAMPLINGS: readonly [444, 422, 420];
41
+ /** Overlay blend modes. */
42
+ export declare const BLEND_MODES: readonly ["normal", "multiply", "screen", "overlay"];
43
+ /** Channels `curve(...)` remaps. */
44
+ export declare const CURVE_CHANNELS: readonly ["rgb", "r", "g", "b"];
45
+ /** Modes `studio(...)` runs. */
46
+ export declare const STUDIO_MODES: readonly ["product", "scene"];
47
+ /** Named backdrops for `studio(product,bg:...)`; any RRGGBB color also works. */
48
+ export declare const STUDIO_BACKDROPS: readonly ["white", "brand", "none"];
49
+ /** Contact shadow strengths for `studio(product, ...)`. */
50
+ export declare const STUDIO_SHADOWS: readonly ["soft", "strong", "none"];
51
+ /** Factors `upscale(...)` takes. */
52
+ export declare const UPSCALE_FACTORS: readonly [2, 4];
53
+ /** Device pixel ratios `dpr(...)` takes. */
54
+ export declare const DPR_FACTORS: readonly [1, 1.5, 2, 3];
55
+ /** Shapes `mask(...)` cuts. */
56
+ export declare const MASK_SHAPES: readonly ["circle", "ellipse"];
57
+ /** Axes `flip(...)` mirrors across. */
58
+ export declare const FLIP_AXES: readonly ["h", "v"];
59
+ /** Whether `value` is a member of the closed set `list`. */
60
+ export declare function isOneOf<T extends string | number>(list: readonly T[], value: unknown): value is T;
61
+ /**
62
+ * The objects crop's cap on distinct class names (url.rs
63
+ * MAX_OBJECTS_CLASSES).
64
+ */
65
+ export declare const MAX_OBJECTS_CLASSES = 32;
66
+ /**
67
+ * A detection class name as the server's parse_class_name
68
+ * takes it, already trimmed: 1 to 64 UTF-8 bytes (bytes, not
69
+ * UTF-16 code units, as on the server) of letters, digits,
70
+ * space, `_`, `.`, `-`, `/`.
71
+ */
72
+ export declare function isClassName(name: string): boolean;
73
+ /**
74
+ * The key a class name folds to when the objects crop drops
75
+ * repeats. The server compares names with
76
+ * `eq_ignore_ascii_case` (url.rs parse_crop_align), so only
77
+ * A to Z fold: `E` and `e` are one class, `É` and `é` are two
78
+ * (issue #482).
79
+ */
80
+ export declare function classFoldKey(name: string): string;
33
81
  /**
34
82
  * Parse a crabcrop URL (or bare path) back into its pieces,
35
83
  * explaining each segment. Mirrors the server's dispatch: