@fangorn-network/sdk 2026.7.14 → 2026.7.27

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.
@@ -1,218 +0,0 @@
1
- import { abytes, aexists, aoutput, clean, copyBytes, createView, toBytes, u32 } from "./utils.js";
2
-
3
- //#region node_modules/@noble/ciphers/esm/_polyval.js
4
- /**
5
- * GHash from AES-GCM and its little-endian "mirror image" Polyval from AES-SIV.
6
- *
7
- * Implemented in terms of GHash with conversion function for keys
8
- * GCM GHASH from
9
- * [NIST SP800-38d](https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf),
10
- * SIV from
11
- * [RFC 8452](https://datatracker.ietf.org/doc/html/rfc8452).
12
- *
13
- * GHASH modulo: x^128 + x^7 + x^2 + x + 1
14
- * POLYVAL modulo: x^128 + x^127 + x^126 + x^121 + 1
15
- *
16
- * @module
17
- */
18
- const BLOCK_SIZE = 16;
19
- const ZEROS16 = /* @__PURE__ */ new Uint8Array(16);
20
- const ZEROS32 = u32(ZEROS16);
21
- const POLY = 225;
22
- const mul2 = (s0, s1, s2, s3) => {
23
- const hiBit = s3 & 1;
24
- return {
25
- s3: s2 << 31 | s3 >>> 1,
26
- s2: s1 << 31 | s2 >>> 1,
27
- s1: s0 << 31 | s1 >>> 1,
28
- s0: s0 >>> 1 ^ POLY << 24 & -(hiBit & 1)
29
- };
30
- };
31
- const swapLE = (n) => (n >>> 0 & 255) << 24 | (n >>> 8 & 255) << 16 | (n >>> 16 & 255) << 8 | n >>> 24 & 255 | 0;
32
- /**
33
- * `mulX_POLYVAL(ByteReverse(H))` from spec
34
- * @param k mutated in place
35
- */
36
- function _toGHASHKey(k) {
37
- k.reverse();
38
- const hiBit = k[15] & 1;
39
- let carry = 0;
40
- for (let i = 0; i < k.length; i++) {
41
- const t = k[i];
42
- k[i] = t >>> 1 | carry;
43
- carry = (t & 1) << 7;
44
- }
45
- k[0] ^= -hiBit & 225;
46
- return k;
47
- }
48
- const estimateWindow = (bytes) => {
49
- if (bytes > 64 * 1024) return 8;
50
- if (bytes > 1024) return 4;
51
- return 2;
52
- };
53
- var GHASH = class {
54
- constructor(key, expectedLength) {
55
- this.blockLen = BLOCK_SIZE;
56
- this.outputLen = BLOCK_SIZE;
57
- this.s0 = 0;
58
- this.s1 = 0;
59
- this.s2 = 0;
60
- this.s3 = 0;
61
- this.finished = false;
62
- key = toBytes(key);
63
- abytes(key, 16);
64
- const kView = createView(key);
65
- let k0 = kView.getUint32(0, false);
66
- let k1 = kView.getUint32(4, false);
67
- let k2 = kView.getUint32(8, false);
68
- let k3 = kView.getUint32(12, false);
69
- const doubles = [];
70
- for (let i = 0; i < 128; i++) {
71
- doubles.push({
72
- s0: swapLE(k0),
73
- s1: swapLE(k1),
74
- s2: swapLE(k2),
75
- s3: swapLE(k3)
76
- });
77
- ({s0: k0, s1: k1, s2: k2, s3: k3} = mul2(k0, k1, k2, k3));
78
- }
79
- const W = estimateWindow(expectedLength || 1024);
80
- if (![
81
- 1,
82
- 2,
83
- 4,
84
- 8
85
- ].includes(W)) throw new Error("ghash: invalid window size, expected 2, 4 or 8");
86
- this.W = W;
87
- const windows = 128 / W;
88
- const windowSize = this.windowSize = 2 ** W;
89
- const items = [];
90
- for (let w = 0; w < windows; w++) for (let byte = 0; byte < windowSize; byte++) {
91
- let s0 = 0, s1 = 0, s2 = 0, s3 = 0;
92
- for (let j = 0; j < W; j++) {
93
- if (!(byte >>> W - j - 1 & 1)) continue;
94
- const { s0: d0, s1: d1, s2: d2, s3: d3 } = doubles[W * w + j];
95
- s0 ^= d0, s1 ^= d1, s2 ^= d2, s3 ^= d3;
96
- }
97
- items.push({
98
- s0,
99
- s1,
100
- s2,
101
- s3
102
- });
103
- }
104
- this.t = items;
105
- }
106
- _updateBlock(s0, s1, s2, s3) {
107
- s0 ^= this.s0, s1 ^= this.s1, s2 ^= this.s2, s3 ^= this.s3;
108
- const { W, t, windowSize } = this;
109
- let o0 = 0, o1 = 0, o2 = 0, o3 = 0;
110
- const mask = (1 << W) - 1;
111
- let w = 0;
112
- for (const num of [
113
- s0,
114
- s1,
115
- s2,
116
- s3
117
- ]) for (let bytePos = 0; bytePos < 4; bytePos++) {
118
- const byte = num >>> 8 * bytePos & 255;
119
- for (let bitPos = 8 / W - 1; bitPos >= 0; bitPos--) {
120
- const bit = byte >>> W * bitPos & mask;
121
- const { s0: e0, s1: e1, s2: e2, s3: e3 } = t[w * windowSize + bit];
122
- o0 ^= e0, o1 ^= e1, o2 ^= e2, o3 ^= e3;
123
- w += 1;
124
- }
125
- }
126
- this.s0 = o0;
127
- this.s1 = o1;
128
- this.s2 = o2;
129
- this.s3 = o3;
130
- }
131
- update(data) {
132
- aexists(this);
133
- data = toBytes(data);
134
- abytes(data);
135
- const b32 = u32(data);
136
- const blocks = Math.floor(data.length / BLOCK_SIZE);
137
- const left = data.length % BLOCK_SIZE;
138
- for (let i = 0; i < blocks; i++) this._updateBlock(b32[i * 4 + 0], b32[i * 4 + 1], b32[i * 4 + 2], b32[i * 4 + 3]);
139
- if (left) {
140
- ZEROS16.set(data.subarray(blocks * BLOCK_SIZE));
141
- this._updateBlock(ZEROS32[0], ZEROS32[1], ZEROS32[2], ZEROS32[3]);
142
- clean(ZEROS32);
143
- }
144
- return this;
145
- }
146
- destroy() {
147
- const { t } = this;
148
- for (const elm of t) elm.s0 = 0, elm.s1 = 0, elm.s2 = 0, elm.s3 = 0;
149
- }
150
- digestInto(out) {
151
- aexists(this);
152
- aoutput(out, this);
153
- this.finished = true;
154
- const { s0, s1, s2, s3 } = this;
155
- const o32 = u32(out);
156
- o32[0] = s0;
157
- o32[1] = s1;
158
- o32[2] = s2;
159
- o32[3] = s3;
160
- return out;
161
- }
162
- digest() {
163
- const res = new Uint8Array(BLOCK_SIZE);
164
- this.digestInto(res);
165
- this.destroy();
166
- return res;
167
- }
168
- };
169
- var Polyval = class extends GHASH {
170
- constructor(key, expectedLength) {
171
- key = toBytes(key);
172
- abytes(key);
173
- const ghKey = _toGHASHKey(copyBytes(key));
174
- super(ghKey, expectedLength);
175
- clean(ghKey);
176
- }
177
- update(data) {
178
- data = toBytes(data);
179
- aexists(this);
180
- const b32 = u32(data);
181
- const left = data.length % BLOCK_SIZE;
182
- const blocks = Math.floor(data.length / BLOCK_SIZE);
183
- for (let i = 0; i < blocks; i++) this._updateBlock(swapLE(b32[i * 4 + 3]), swapLE(b32[i * 4 + 2]), swapLE(b32[i * 4 + 1]), swapLE(b32[i * 4 + 0]));
184
- if (left) {
185
- ZEROS16.set(data.subarray(blocks * BLOCK_SIZE));
186
- this._updateBlock(swapLE(ZEROS32[3]), swapLE(ZEROS32[2]), swapLE(ZEROS32[1]), swapLE(ZEROS32[0]));
187
- clean(ZEROS32);
188
- }
189
- return this;
190
- }
191
- digestInto(out) {
192
- aexists(this);
193
- aoutput(out, this);
194
- this.finished = true;
195
- const { s0, s1, s2, s3 } = this;
196
- const o32 = u32(out);
197
- o32[0] = s0;
198
- o32[1] = s1;
199
- o32[2] = s2;
200
- o32[3] = s3;
201
- return out.reverse();
202
- }
203
- };
204
- function wrapConstructorWithKey(hashCons) {
205
- const hashC = (msg, key) => hashCons(key, msg.length).update(toBytes(msg)).digest();
206
- const tmp = hashCons(new Uint8Array(16), 0);
207
- hashC.outputLen = tmp.outputLen;
208
- hashC.blockLen = tmp.blockLen;
209
- hashC.create = (key, expectedLength) => hashCons(key, expectedLength);
210
- return hashC;
211
- }
212
- /** GHash MAC for AES-GCM. */
213
- const ghash = wrapConstructorWithKey((key, expectedLength) => new GHASH(key, expectedLength));
214
- /** Polyval MAC for AES-SIV. */
215
- const polyval = wrapConstructorWithKey((key, expectedLength) => new Polyval(key, expectedLength));
216
-
217
- //#endregion
218
- export { ghash };
@@ -1,261 +0,0 @@
1
- import { abytes, clean, copyBytes, createView, equalBytes, getOutput, isAligned32, setBigUint64, u32, u64Lengths, u8, wrapCipher } from "./utils.js";
2
- import { ghash } from "./_polyval.js";
3
-
4
- //#region node_modules/@noble/ciphers/esm/aes.js
5
- /**
6
- * [AES](https://en.wikipedia.org/wiki/Advanced_Encryption_Standard)
7
- * a.k.a. Advanced Encryption Standard
8
- * is a variant of Rijndael block cipher, standardized by NIST in 2001.
9
- * We provide the fastest available pure JS implementation.
10
- *
11
- * Data is split into 128-bit blocks. Encrypted in 10/12/14 rounds (128/192/256 bits). In every round:
12
- * 1. **S-box**, table substitution
13
- * 2. **Shift rows**, cyclic shift left of all rows of data array
14
- * 3. **Mix columns**, multiplying every column by fixed polynomial
15
- * 4. **Add round key**, round_key xor i-th column of array
16
- *
17
- * Check out [FIPS-197](https://csrc.nist.gov/files/pubs/fips/197/final/docs/fips-197.pdf)
18
- * and [original proposal](https://csrc.nist.gov/csrc/media/projects/cryptographic-standards-and-guidelines/documents/aes-development/rijndael-ammended.pdf)
19
- * @module
20
- */
21
- const BLOCK_SIZE = 16;
22
- const BLOCK_SIZE32 = 4;
23
- const EMPTY_BLOCK = /* @__PURE__ */ new Uint8Array(BLOCK_SIZE);
24
- const POLY = 283;
25
- function mul2(n) {
26
- return n << 1 ^ POLY & -(n >> 7);
27
- }
28
- function mul(a, b) {
29
- let res = 0;
30
- for (; b > 0; b >>= 1) {
31
- res ^= a & -(b & 1);
32
- a = mul2(a);
33
- }
34
- return res;
35
- }
36
- const sbox = /* @__PURE__ */ (() => {
37
- const t = new Uint8Array(256);
38
- for (let i = 0, x = 1; i < 256; i++, x ^= mul2(x)) t[i] = x;
39
- const box = new Uint8Array(256);
40
- box[0] = 99;
41
- for (let i = 0; i < 255; i++) {
42
- let x = t[255 - i];
43
- x |= x << 8;
44
- box[t[i]] = (x ^ x >> 4 ^ x >> 5 ^ x >> 6 ^ x >> 7 ^ 99) & 255;
45
- }
46
- clean(t);
47
- return box;
48
- })();
49
- const rotr32_8 = (n) => n << 24 | n >>> 8;
50
- const rotl32_8 = (n) => n << 8 | n >>> 24;
51
- function genTtable(sbox$1, fn) {
52
- if (sbox$1.length !== 256) throw new Error("Wrong sbox length");
53
- const T0 = new Uint32Array(256).map((_, j) => fn(sbox$1[j]));
54
- const T1 = T0.map(rotl32_8);
55
- const T2 = T1.map(rotl32_8);
56
- const T3 = T2.map(rotl32_8);
57
- const T01 = new Uint32Array(256 * 256);
58
- const T23 = new Uint32Array(256 * 256);
59
- const sbox2 = new Uint16Array(256 * 256);
60
- for (let i = 0; i < 256; i++) for (let j = 0; j < 256; j++) {
61
- const idx = i * 256 + j;
62
- T01[idx] = T0[i] ^ T1[j];
63
- T23[idx] = T2[i] ^ T3[j];
64
- sbox2[idx] = sbox$1[i] << 8 | sbox$1[j];
65
- }
66
- return {
67
- sbox: sbox$1,
68
- sbox2,
69
- T0,
70
- T1,
71
- T2,
72
- T3,
73
- T01,
74
- T23
75
- };
76
- }
77
- const tableEncoding = /* @__PURE__ */ genTtable(sbox, (s) => mul(s, 3) << 24 | s << 16 | s << 8 | mul(s, 2));
78
- const xPowers = /* @__PURE__ */ (() => {
79
- const p = new Uint8Array(16);
80
- for (let i = 0, x = 1; i < 16; i++, x = mul2(x)) p[i] = x;
81
- return p;
82
- })();
83
- /** Key expansion used in CTR. */
84
- function expandKeyLE(key) {
85
- abytes(key);
86
- const len = key.length;
87
- if (![
88
- 16,
89
- 24,
90
- 32
91
- ].includes(len)) throw new Error("aes: invalid key size, should be 16, 24 or 32, got " + len);
92
- const { sbox2 } = tableEncoding;
93
- const toClean = [];
94
- if (!isAligned32(key)) toClean.push(key = copyBytes(key));
95
- const k32 = u32(key);
96
- const Nk = k32.length;
97
- const subByte = (n) => applySbox(sbox2, n, n, n, n);
98
- const xk = new Uint32Array(len + 28);
99
- xk.set(k32);
100
- for (let i = Nk; i < xk.length; i++) {
101
- let t = xk[i - 1];
102
- if (i % Nk === 0) t = subByte(rotr32_8(t)) ^ xPowers[i / Nk - 1];
103
- else if (Nk > 6 && i % Nk === 4) t = subByte(t);
104
- xk[i] = xk[i - Nk] ^ t;
105
- }
106
- clean(...toClean);
107
- return xk;
108
- }
109
- function apply0123(T01, T23, s0, s1, s2, s3) {
110
- return T01[s0 << 8 & 65280 | s1 >>> 8 & 255] ^ T23[s2 >>> 8 & 65280 | s3 >>> 24 & 255];
111
- }
112
- function applySbox(sbox2, s0, s1, s2, s3) {
113
- return sbox2[s0 & 255 | s1 & 65280] | sbox2[s2 >>> 16 & 255 | s3 >>> 16 & 65280] << 16;
114
- }
115
- function encrypt(xk, s0, s1, s2, s3) {
116
- const { sbox2, T01, T23 } = tableEncoding;
117
- let k = 0;
118
- s0 ^= xk[k++], s1 ^= xk[k++], s2 ^= xk[k++], s3 ^= xk[k++];
119
- const rounds = xk.length / 4 - 2;
120
- for (let i = 0; i < rounds; i++) {
121
- const t0 = xk[k++] ^ apply0123(T01, T23, s0, s1, s2, s3);
122
- const t1 = xk[k++] ^ apply0123(T01, T23, s1, s2, s3, s0);
123
- const t2 = xk[k++] ^ apply0123(T01, T23, s2, s3, s0, s1);
124
- const t3 = xk[k++] ^ apply0123(T01, T23, s3, s0, s1, s2);
125
- s0 = t0, s1 = t1, s2 = t2, s3 = t3;
126
- }
127
- return {
128
- s0: xk[k++] ^ applySbox(sbox2, s0, s1, s2, s3),
129
- s1: xk[k++] ^ applySbox(sbox2, s1, s2, s3, s0),
130
- s2: xk[k++] ^ applySbox(sbox2, s2, s3, s0, s1),
131
- s3: xk[k++] ^ applySbox(sbox2, s3, s0, s1, s2)
132
- };
133
- }
134
- function ctr32(xk, isLE, nonce, src, dst) {
135
- abytes(nonce, BLOCK_SIZE);
136
- abytes(src);
137
- dst = getOutput(src.length, dst);
138
- const ctr = nonce;
139
- const c32 = u32(ctr);
140
- const view = createView(ctr);
141
- const src32 = u32(src);
142
- const dst32 = u32(dst);
143
- const ctrPos = isLE ? 0 : 12;
144
- const srcLen = src.length;
145
- let ctrNum = view.getUint32(ctrPos, isLE);
146
- let { s0, s1, s2, s3 } = encrypt(xk, c32[0], c32[1], c32[2], c32[3]);
147
- for (let i = 0; i + 4 <= src32.length; i += 4) {
148
- dst32[i + 0] = src32[i + 0] ^ s0;
149
- dst32[i + 1] = src32[i + 1] ^ s1;
150
- dst32[i + 2] = src32[i + 2] ^ s2;
151
- dst32[i + 3] = src32[i + 3] ^ s3;
152
- ctrNum = ctrNum + 1 >>> 0;
153
- view.setUint32(ctrPos, ctrNum, isLE);
154
- ({s0, s1, s2, s3} = encrypt(xk, c32[0], c32[1], c32[2], c32[3]));
155
- }
156
- const start = BLOCK_SIZE * Math.floor(src32.length / BLOCK_SIZE32);
157
- if (start < srcLen) {
158
- const b32 = new Uint32Array([
159
- s0,
160
- s1,
161
- s2,
162
- s3
163
- ]);
164
- const buf = u8(b32);
165
- for (let i = start, pos = 0; i < srcLen; i++, pos++) dst[i] = src[i] ^ buf[pos];
166
- clean(b32);
167
- }
168
- return dst;
169
- }
170
- function computeTag(fn, isLE, key, data, AAD) {
171
- const aadLength = AAD ? AAD.length : 0;
172
- const h = fn.create(key, data.length + aadLength);
173
- if (AAD) h.update(AAD);
174
- const num = u64Lengths(8 * data.length, 8 * aadLength, isLE);
175
- h.update(data);
176
- h.update(num);
177
- const res = h.digest();
178
- clean(num);
179
- return res;
180
- }
181
- /**
182
- * GCM: Galois/Counter Mode.
183
- * Modern, parallel version of CTR, with MAC.
184
- * Be careful: MACs can be forged.
185
- * Unsafe to use random nonces under the same key, due to collision chance.
186
- * As for nonce size, prefer 12-byte, instead of 8-byte.
187
- */
188
- const gcm = /* @__PURE__ */ wrapCipher({
189
- blockSize: 16,
190
- nonceLength: 12,
191
- tagLength: 16,
192
- varSizeNonce: true
193
- }, function aesgcm(key, nonce, AAD) {
194
- if (nonce.length < 8) throw new Error("aes/gcm: invalid nonce length");
195
- const tagLength = 16;
196
- function _computeTag(authKey, tagMask, data) {
197
- const tag = computeTag(ghash, false, authKey, data, AAD);
198
- for (let i = 0; i < tagMask.length; i++) tag[i] ^= tagMask[i];
199
- return tag;
200
- }
201
- function deriveKeys() {
202
- const xk = expandKeyLE(key);
203
- const authKey = EMPTY_BLOCK.slice();
204
- const counter = EMPTY_BLOCK.slice();
205
- ctr32(xk, false, counter, counter, authKey);
206
- if (nonce.length === 12) counter.set(nonce);
207
- else {
208
- const nonceLen = EMPTY_BLOCK.slice();
209
- setBigUint64(createView(nonceLen), 8, BigInt(nonce.length * 8), false);
210
- const g = ghash.create(authKey).update(nonce).update(nonceLen);
211
- g.digestInto(counter);
212
- g.destroy();
213
- }
214
- return {
215
- xk,
216
- authKey,
217
- counter,
218
- tagMask: ctr32(xk, false, counter, EMPTY_BLOCK)
219
- };
220
- }
221
- return {
222
- encrypt(plaintext) {
223
- const { xk, authKey, counter, tagMask } = deriveKeys();
224
- const out = new Uint8Array(plaintext.length + tagLength);
225
- const toClean = [
226
- xk,
227
- authKey,
228
- counter,
229
- tagMask
230
- ];
231
- if (!isAligned32(plaintext)) toClean.push(plaintext = copyBytes(plaintext));
232
- ctr32(xk, false, counter, plaintext, out.subarray(0, plaintext.length));
233
- const tag = _computeTag(authKey, tagMask, out.subarray(0, out.length - tagLength));
234
- toClean.push(tag);
235
- out.set(tag, plaintext.length);
236
- clean(...toClean);
237
- return out;
238
- },
239
- decrypt(ciphertext) {
240
- const { xk, authKey, counter, tagMask } = deriveKeys();
241
- const toClean = [
242
- xk,
243
- authKey,
244
- tagMask,
245
- counter
246
- ];
247
- if (!isAligned32(ciphertext)) toClean.push(ciphertext = copyBytes(ciphertext));
248
- const data = ciphertext.subarray(0, -tagLength);
249
- const passedTag = ciphertext.subarray(-tagLength);
250
- const tag = _computeTag(authKey, tagMask, data);
251
- toClean.push(tag);
252
- if (!equalBytes(tag, passedTag)) throw new Error("aes/gcm: invalid ghash tag");
253
- const out = ctr32(xk, false, counter, data);
254
- clean(...toClean);
255
- return out;
256
- }
257
- };
258
- });
259
-
260
- //#endregion
261
- export { gcm };
@@ -1,156 +0,0 @@
1
- //#region node_modules/@noble/ciphers/esm/utils.js
2
- /**
3
- * Utilities for hex, bytes, CSPRNG.
4
- * @module
5
- */
6
- /*! noble-ciphers - MIT License (c) 2023 Paul Miller (paulmillr.com) */
7
- /** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
8
- function isBytes(a) {
9
- return a instanceof Uint8Array || ArrayBuffer.isView(a) && a.constructor.name === "Uint8Array";
10
- }
11
- /** Asserts something is boolean. */
12
- function abool(b) {
13
- if (typeof b !== "boolean") throw new Error(`boolean expected, not ${b}`);
14
- }
15
- /** Asserts something is Uint8Array. */
16
- function abytes(b, ...lengths) {
17
- if (!isBytes(b)) throw new Error("Uint8Array expected");
18
- if (lengths.length > 0 && !lengths.includes(b.length)) throw new Error("Uint8Array expected of length " + lengths + ", got length=" + b.length);
19
- }
20
- /** Asserts a hash instance has not been destroyed / finished */
21
- function aexists(instance, checkFinished = true) {
22
- if (instance.destroyed) throw new Error("Hash instance has been destroyed");
23
- if (checkFinished && instance.finished) throw new Error("Hash#digest() has already been called");
24
- }
25
- /** Asserts output is properly-sized byte array */
26
- function aoutput(out, instance) {
27
- abytes(out);
28
- const min = instance.outputLen;
29
- if (out.length < min) throw new Error("digestInto() expects output buffer of length at least " + min);
30
- }
31
- /** Cast u8 / u16 / u32 to u8. */
32
- function u8(arr) {
33
- return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
34
- }
35
- /** Cast u8 / u16 / u32 to u32. */
36
- function u32(arr) {
37
- return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
38
- }
39
- /** Zeroize a byte array. Warning: JS provides no guarantees. */
40
- function clean(...arrays) {
41
- for (let i = 0; i < arrays.length; i++) arrays[i].fill(0);
42
- }
43
- /** Create DataView of an array for easy byte-level manipulation. */
44
- function createView(arr) {
45
- return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
46
- }
47
- /** Is current platform little-endian? Most are. Big-Endian platform: IBM */
48
- const isLE = /* @__PURE__ */ (() => new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68)();
49
- /**
50
- * Converts string to bytes using UTF8 encoding.
51
- * @example utf8ToBytes('abc') // new Uint8Array([97, 98, 99])
52
- */
53
- function utf8ToBytes(str) {
54
- if (typeof str !== "string") throw new Error("string expected");
55
- return new Uint8Array(new TextEncoder().encode(str));
56
- }
57
- /**
58
- * Normalizes (non-hex) string or Uint8Array to Uint8Array.
59
- * Warning: when Uint8Array is passed, it would NOT get copied.
60
- * Keep in mind for future mutable operations.
61
- */
62
- function toBytes(data) {
63
- if (typeof data === "string") data = utf8ToBytes(data);
64
- else if (isBytes(data)) data = copyBytes(data);
65
- else throw new Error("Uint8Array expected, got " + typeof data);
66
- return data;
67
- }
68
- /** Compares 2 uint8array-s in kinda constant time. */
69
- function equalBytes(a, b) {
70
- if (a.length !== b.length) return false;
71
- let diff = 0;
72
- for (let i = 0; i < a.length; i++) diff |= a[i] ^ b[i];
73
- return diff === 0;
74
- }
75
- /**
76
- * Wraps a cipher: validates args, ensures encrypt() can only be called once.
77
- * @__NO_SIDE_EFFECTS__
78
- */
79
- const wrapCipher = (params, constructor) => {
80
- function wrappedCipher(key, ...args) {
81
- abytes(key);
82
- if (!isLE) throw new Error("Non little-endian hardware is not yet supported");
83
- if (params.nonceLength !== void 0) {
84
- const nonce = args[0];
85
- if (!nonce) throw new Error("nonce / iv required");
86
- if (params.varSizeNonce) abytes(nonce);
87
- else abytes(nonce, params.nonceLength);
88
- }
89
- const tagl = params.tagLength;
90
- if (tagl && args[1] !== void 0) abytes(args[1]);
91
- const cipher = constructor(key, ...args);
92
- const checkOutput = (fnLength, output) => {
93
- if (output !== void 0) {
94
- if (fnLength !== 2) throw new Error("cipher output not supported");
95
- abytes(output);
96
- }
97
- };
98
- let called = false;
99
- return {
100
- encrypt(data, output) {
101
- if (called) throw new Error("cannot encrypt() twice with same key + nonce");
102
- called = true;
103
- abytes(data);
104
- checkOutput(cipher.encrypt.length, output);
105
- return cipher.encrypt(data, output);
106
- },
107
- decrypt(data, output) {
108
- abytes(data);
109
- if (tagl && data.length < tagl) throw new Error("invalid ciphertext length: smaller than tagLength=" + tagl);
110
- checkOutput(cipher.decrypt.length, output);
111
- return cipher.decrypt(data, output);
112
- }
113
- };
114
- }
115
- Object.assign(wrappedCipher, params);
116
- return wrappedCipher;
117
- };
118
- /**
119
- * By default, returns u8a of length.
120
- * When out is available, it checks it for validity and uses it.
121
- */
122
- function getOutput(expectedLength, out, onlyAligned = true) {
123
- if (out === void 0) return new Uint8Array(expectedLength);
124
- if (out.length !== expectedLength) throw new Error("invalid output length, expected " + expectedLength + ", got: " + out.length);
125
- if (onlyAligned && !isAligned32(out)) throw new Error("invalid output, must be aligned");
126
- return out;
127
- }
128
- /** Polyfill for Safari 14. */
129
- function setBigUint64(view, byteOffset, value, isLE$1) {
130
- if (typeof view.setBigUint64 === "function") return view.setBigUint64(byteOffset, value, isLE$1);
131
- const _32n = BigInt(32);
132
- const _u32_max = BigInt(4294967295);
133
- const wh = Number(value >> _32n & _u32_max);
134
- const wl = Number(value & _u32_max);
135
- const h = isLE$1 ? 4 : 0;
136
- const l = isLE$1 ? 0 : 4;
137
- view.setUint32(byteOffset + h, wh, isLE$1);
138
- view.setUint32(byteOffset + l, wl, isLE$1);
139
- }
140
- function u64Lengths(dataLength, aadLength, isLE$1) {
141
- abool(isLE$1);
142
- const num = new Uint8Array(16);
143
- const view = createView(num);
144
- setBigUint64(view, 0, BigInt(aadLength), isLE$1);
145
- setBigUint64(view, 8, BigInt(dataLength), isLE$1);
146
- return num;
147
- }
148
- function isAligned32(bytes) {
149
- return bytes.byteOffset % 4 === 0;
150
- }
151
- function copyBytes(bytes) {
152
- return Uint8Array.from(bytes);
153
- }
154
-
155
- //#endregion
156
- export { abytes, aexists, aoutput, clean, copyBytes, createView, equalBytes, getOutput, isAligned32, setBigUint64, toBytes, u32, u64Lengths, u8, wrapCipher };