Spaces:
Paused
Paused
Upload folder using huggingface_hub (part 22)
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
- .gitattributes +4 -0
- node_modules/@noble/hashes/src/_blake.ts +50 -0
- node_modules/@noble/hashes/src/_md.ts +176 -0
- node_modules/@noble/hashes/src/_u64.ts +91 -0
- node_modules/@noble/hashes/src/argon2.ts +497 -0
- node_modules/@noble/hashes/src/blake1.ts +534 -0
- node_modules/@noble/hashes/src/blake2.ts +486 -0
- node_modules/@noble/hashes/src/blake2b.ts +10 -0
- node_modules/@noble/hashes/src/blake2s.ts +20 -0
- node_modules/@noble/hashes/src/blake3.ts +272 -0
- node_modules/@noble/hashes/src/crypto.ts +9 -0
- node_modules/@noble/hashes/src/cryptoNode.ts +15 -0
- node_modules/@noble/hashes/src/eskdf.ts +187 -0
- node_modules/@noble/hashes/src/hkdf.ts +88 -0
- node_modules/@noble/hashes/src/hmac.ts +94 -0
- node_modules/@noble/hashes/src/index.ts +31 -0
- node_modules/@noble/hashes/src/legacy.ts +293 -0
- node_modules/@noble/hashes/src/pbkdf2.ts +122 -0
- node_modules/@noble/hashes/src/ripemd160.ts +12 -0
- node_modules/@noble/hashes/src/scrypt.ts +257 -0
- node_modules/@noble/hashes/src/sha1.ts +10 -0
- node_modules/@noble/hashes/src/sha2.ts +402 -0
- node_modules/@noble/hashes/src/sha256.ts +24 -0
- node_modules/@noble/hashes/src/sha3-addons.ts +499 -0
- node_modules/@noble/hashes/src/sha3.ts +258 -0
- node_modules/@noble/hashes/src/sha512.ts +34 -0
- node_modules/@noble/hashes/src/utils.ts +395 -0
- node_modules/@noble/hashes/utils.d.ts +161 -0
- node_modules/@noble/hashes/utils.d.ts.map +1 -0
- node_modules/@noble/hashes/utils.js +313 -0
- node_modules/@noble/hashes/utils.js.map +1 -0
- node_modules/@nodable/entities/README.md +41 -0
- node_modules/@nodable/entities/package.json +57 -0
- node_modules/@nodable/entities/src/EntityDecoder.js +644 -0
- node_modules/@nodable/entities/src/EntityEncoder.js +194 -0
- node_modules/@nodable/entities/src/entities.js +1159 -0
- node_modules/@nodable/entities/src/entityTries.js +49 -0
- node_modules/@nodable/entities/src/index.d.ts +341 -0
- node_modules/@nodable/entities/src/index.js +29 -0
- node_modules/@open-draft/deferred-promise/LICENSE.md +21 -0
- node_modules/@open-draft/deferred-promise/README.md +191 -0
- node_modules/@open-draft/deferred-promise/build/index.d.mts +30 -0
- node_modules/@open-draft/deferred-promise/build/index.mjs +71 -0
- node_modules/@open-draft/deferred-promise/build/index.mjs.map +1 -0
- node_modules/@open-draft/deferred-promise/package.json +41 -0
- node_modules/@open-draft/logger/LICENSE +9 -0
- node_modules/@open-draft/logger/README.md +225 -0
- node_modules/@open-draft/logger/lib/index.d.ts +83 -0
- node_modules/@open-draft/logger/lib/index.js +295 -0
- node_modules/@open-draft/logger/lib/index.mjs +281 -0
.gitattributes
CHANGED
|
@@ -54,3 +54,7 @@ node_modules/@jimp/plugin-print/dist/fonts/open-sans/open-sans-128-white/open-sa
|
|
| 54 |
node_modules/@jimp/plugin-print/fonts/open-sans/open-sans-128-black/open-sans-128-black.png filter=lfs diff=lfs merge=lfs -text
|
| 55 |
node_modules/@jimp/plugin-print/fonts/open-sans/open-sans-128-white/open-sans-128-white.png filter=lfs diff=lfs merge=lfs -text
|
| 56 |
node_modules/@jimp/plugin-threshold/src/__image_snapshots__/index-test-ts-src-index-test-ts-threshold-defines-default-threshold-for-lighter-backgrounds-1-snap.png filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
node_modules/@jimp/plugin-print/fonts/open-sans/open-sans-128-black/open-sans-128-black.png filter=lfs diff=lfs merge=lfs -text
|
| 55 |
node_modules/@jimp/plugin-print/fonts/open-sans/open-sans-128-white/open-sans-128-white.png filter=lfs diff=lfs merge=lfs -text
|
| 56 |
node_modules/@jimp/plugin-threshold/src/__image_snapshots__/index-test-ts-src-index-test-ts-threshold-defines-default-threshold-for-lighter-backgrounds-1-snap.png filter=lfs diff=lfs merge=lfs -text
|
| 57 |
+
node_modules/@rolldown/binding-linux-x64-gnu/rolldown-binding.linux-x64-gnu.node filter=lfs diff=lfs merge=lfs -text
|
| 58 |
+
node_modules/@rolldown/binding-linux-x64-musl/rolldown-binding.linux-x64-musl.node filter=lfs diff=lfs merge=lfs -text
|
| 59 |
+
node_modules/@rollup/rollup-linux-x64-gnu/rollup.linux-x64-gnu.node filter=lfs diff=lfs merge=lfs -text
|
| 60 |
+
node_modules/@rollup/rollup-linux-x64-musl/rollup.linux-x64-musl.node filter=lfs diff=lfs merge=lfs -text
|
node_modules/@noble/hashes/src/_blake.ts
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Internal helpers for blake hash.
|
| 3 |
+
* @module
|
| 4 |
+
*/
|
| 5 |
+
import { rotr } from './utils.ts';
|
| 6 |
+
|
| 7 |
+
/**
|
| 8 |
+
* Internal blake variable.
|
| 9 |
+
* For BLAKE2b, the two extra permutations for rounds 10 and 11 are SIGMA[10..11] = SIGMA[0..1].
|
| 10 |
+
*/
|
| 11 |
+
// prettier-ignore
|
| 12 |
+
export const BSIGMA: Uint8Array = /* @__PURE__ */ Uint8Array.from([
|
| 13 |
+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
| 14 |
+
14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3,
|
| 15 |
+
11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4,
|
| 16 |
+
7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8,
|
| 17 |
+
9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13,
|
| 18 |
+
2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9,
|
| 19 |
+
12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11,
|
| 20 |
+
13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10,
|
| 21 |
+
6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5,
|
| 22 |
+
10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0,
|
| 23 |
+
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
|
| 24 |
+
14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3,
|
| 25 |
+
// Blake1, unused in others
|
| 26 |
+
11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4,
|
| 27 |
+
7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8,
|
| 28 |
+
9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13,
|
| 29 |
+
2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9,
|
| 30 |
+
]);
|
| 31 |
+
|
| 32 |
+
// prettier-ignore
|
| 33 |
+
export type Num4 = { a: number; b: number; c: number; d: number; };
|
| 34 |
+
|
| 35 |
+
// Mixing function G splitted in two halfs
|
| 36 |
+
export function G1s(a: number, b: number, c: number, d: number, x: number): Num4 {
|
| 37 |
+
a = (a + b + x) | 0;
|
| 38 |
+
d = rotr(d ^ a, 16);
|
| 39 |
+
c = (c + d) | 0;
|
| 40 |
+
b = rotr(b ^ c, 12);
|
| 41 |
+
return { a, b, c, d };
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
export function G2s(a: number, b: number, c: number, d: number, x: number): Num4 {
|
| 45 |
+
a = (a + b + x) | 0;
|
| 46 |
+
d = rotr(d ^ a, 8);
|
| 47 |
+
c = (c + d) | 0;
|
| 48 |
+
b = rotr(b ^ c, 7);
|
| 49 |
+
return { a, b, c, d };
|
| 50 |
+
}
|
node_modules/@noble/hashes/src/_md.ts
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Internal Merkle-Damgard hash utils.
|
| 3 |
+
* @module
|
| 4 |
+
*/
|
| 5 |
+
import { type Input, Hash, abytes, aexists, aoutput, clean, createView, toBytes } from './utils.ts';
|
| 6 |
+
|
| 7 |
+
/** Polyfill for Safari 14. https://caniuse.com/mdn-javascript_builtins_dataview_setbiguint64 */
|
| 8 |
+
export function setBigUint64(
|
| 9 |
+
view: DataView,
|
| 10 |
+
byteOffset: number,
|
| 11 |
+
value: bigint,
|
| 12 |
+
isLE: boolean
|
| 13 |
+
): void {
|
| 14 |
+
if (typeof view.setBigUint64 === 'function') return view.setBigUint64(byteOffset, value, isLE);
|
| 15 |
+
const _32n = BigInt(32);
|
| 16 |
+
const _u32_max = BigInt(0xffffffff);
|
| 17 |
+
const wh = Number((value >> _32n) & _u32_max);
|
| 18 |
+
const wl = Number(value & _u32_max);
|
| 19 |
+
const h = isLE ? 4 : 0;
|
| 20 |
+
const l = isLE ? 0 : 4;
|
| 21 |
+
view.setUint32(byteOffset + h, wh, isLE);
|
| 22 |
+
view.setUint32(byteOffset + l, wl, isLE);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
/** Choice: a ? b : c */
|
| 26 |
+
export function Chi(a: number, b: number, c: number): number {
|
| 27 |
+
return (a & b) ^ (~a & c);
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
/** Majority function, true if any two inputs is true. */
|
| 31 |
+
export function Maj(a: number, b: number, c: number): number {
|
| 32 |
+
return (a & b) ^ (a & c) ^ (b & c);
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
/**
|
| 36 |
+
* Merkle-Damgard hash construction base class.
|
| 37 |
+
* Could be used to create MD5, RIPEMD, SHA1, SHA2.
|
| 38 |
+
*/
|
| 39 |
+
export abstract class HashMD<T extends HashMD<T>> extends Hash<T> {
|
| 40 |
+
protected abstract process(buf: DataView, offset: number): void;
|
| 41 |
+
protected abstract get(): number[];
|
| 42 |
+
protected abstract set(...args: number[]): void;
|
| 43 |
+
abstract destroy(): void;
|
| 44 |
+
protected abstract roundClean(): void;
|
| 45 |
+
|
| 46 |
+
readonly blockLen: number;
|
| 47 |
+
readonly outputLen: number;
|
| 48 |
+
readonly padOffset: number;
|
| 49 |
+
readonly isLE: boolean;
|
| 50 |
+
|
| 51 |
+
// For partial updates less than block size
|
| 52 |
+
protected buffer: Uint8Array;
|
| 53 |
+
protected view: DataView;
|
| 54 |
+
protected finished = false;
|
| 55 |
+
protected length = 0;
|
| 56 |
+
protected pos = 0;
|
| 57 |
+
protected destroyed = false;
|
| 58 |
+
|
| 59 |
+
constructor(blockLen: number, outputLen: number, padOffset: number, isLE: boolean) {
|
| 60 |
+
super();
|
| 61 |
+
this.blockLen = blockLen;
|
| 62 |
+
this.outputLen = outputLen;
|
| 63 |
+
this.padOffset = padOffset;
|
| 64 |
+
this.isLE = isLE;
|
| 65 |
+
this.buffer = new Uint8Array(blockLen);
|
| 66 |
+
this.view = createView(this.buffer);
|
| 67 |
+
}
|
| 68 |
+
update(data: Input): this {
|
| 69 |
+
aexists(this);
|
| 70 |
+
data = toBytes(data);
|
| 71 |
+
abytes(data);
|
| 72 |
+
const { view, buffer, blockLen } = this;
|
| 73 |
+
const len = data.length;
|
| 74 |
+
for (let pos = 0; pos < len; ) {
|
| 75 |
+
const take = Math.min(blockLen - this.pos, len - pos);
|
| 76 |
+
// Fast path: we have at least one block in input, cast it to view and process
|
| 77 |
+
if (take === blockLen) {
|
| 78 |
+
const dataView = createView(data);
|
| 79 |
+
for (; blockLen <= len - pos; pos += blockLen) this.process(dataView, pos);
|
| 80 |
+
continue;
|
| 81 |
+
}
|
| 82 |
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
| 83 |
+
this.pos += take;
|
| 84 |
+
pos += take;
|
| 85 |
+
if (this.pos === blockLen) {
|
| 86 |
+
this.process(view, 0);
|
| 87 |
+
this.pos = 0;
|
| 88 |
+
}
|
| 89 |
+
}
|
| 90 |
+
this.length += data.length;
|
| 91 |
+
this.roundClean();
|
| 92 |
+
return this;
|
| 93 |
+
}
|
| 94 |
+
digestInto(out: Uint8Array): void {
|
| 95 |
+
aexists(this);
|
| 96 |
+
aoutput(out, this);
|
| 97 |
+
this.finished = true;
|
| 98 |
+
// Padding
|
| 99 |
+
// We can avoid allocation of buffer for padding completely if it
|
| 100 |
+
// was previously not allocated here. But it won't change performance.
|
| 101 |
+
const { buffer, view, blockLen, isLE } = this;
|
| 102 |
+
let { pos } = this;
|
| 103 |
+
// append the bit '1' to the message
|
| 104 |
+
buffer[pos++] = 0b10000000;
|
| 105 |
+
clean(this.buffer.subarray(pos));
|
| 106 |
+
// we have less than padOffset left in buffer, so we cannot put length in
|
| 107 |
+
// current block, need process it and pad again
|
| 108 |
+
if (this.padOffset > blockLen - pos) {
|
| 109 |
+
this.process(view, 0);
|
| 110 |
+
pos = 0;
|
| 111 |
+
}
|
| 112 |
+
// Pad until full block byte with zeros
|
| 113 |
+
for (let i = pos; i < blockLen; i++) buffer[i] = 0;
|
| 114 |
+
// Note: sha512 requires length to be 128bit integer, but length in JS will overflow before that
|
| 115 |
+
// You need to write around 2 exabytes (u64_max / 8 / (1024**6)) for this to happen.
|
| 116 |
+
// So we just write lowest 64 bits of that value.
|
| 117 |
+
setBigUint64(view, blockLen - 8, BigInt(this.length * 8), isLE);
|
| 118 |
+
this.process(view, 0);
|
| 119 |
+
const oview = createView(out);
|
| 120 |
+
const len = this.outputLen;
|
| 121 |
+
// NOTE: we do division by 4 later, which should be fused in single op with modulo by JIT
|
| 122 |
+
if (len % 4) throw new Error('_sha2: outputLen should be aligned to 32bit');
|
| 123 |
+
const outLen = len / 4;
|
| 124 |
+
const state = this.get();
|
| 125 |
+
if (outLen > state.length) throw new Error('_sha2: outputLen bigger than state');
|
| 126 |
+
for (let i = 0; i < outLen; i++) oview.setUint32(4 * i, state[i], isLE);
|
| 127 |
+
}
|
| 128 |
+
digest(): Uint8Array {
|
| 129 |
+
const { buffer, outputLen } = this;
|
| 130 |
+
this.digestInto(buffer);
|
| 131 |
+
const res = buffer.slice(0, outputLen);
|
| 132 |
+
this.destroy();
|
| 133 |
+
return res;
|
| 134 |
+
}
|
| 135 |
+
_cloneInto(to?: T): T {
|
| 136 |
+
to ||= new (this.constructor as any)() as T;
|
| 137 |
+
to.set(...this.get());
|
| 138 |
+
const { blockLen, buffer, length, finished, destroyed, pos } = this;
|
| 139 |
+
to.destroyed = destroyed;
|
| 140 |
+
to.finished = finished;
|
| 141 |
+
to.length = length;
|
| 142 |
+
to.pos = pos;
|
| 143 |
+
if (length % blockLen) to.buffer.set(buffer);
|
| 144 |
+
return to;
|
| 145 |
+
}
|
| 146 |
+
clone(): T {
|
| 147 |
+
return this._cloneInto();
|
| 148 |
+
}
|
| 149 |
+
}
|
| 150 |
+
|
| 151 |
+
/**
|
| 152 |
+
* Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
|
| 153 |
+
* Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
|
| 154 |
+
*/
|
| 155 |
+
|
| 156 |
+
/** Initial SHA256 state. Bits 0..32 of frac part of sqrt of primes 2..19 */
|
| 157 |
+
export const SHA256_IV: Uint32Array = /* @__PURE__ */ Uint32Array.from([
|
| 158 |
+
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
|
| 159 |
+
]);
|
| 160 |
+
|
| 161 |
+
/** Initial SHA224 state. Bits 32..64 of frac part of sqrt of primes 23..53 */
|
| 162 |
+
export const SHA224_IV: Uint32Array = /* @__PURE__ */ Uint32Array.from([
|
| 163 |
+
0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4,
|
| 164 |
+
]);
|
| 165 |
+
|
| 166 |
+
/** Initial SHA384 state. Bits 0..64 of frac part of sqrt of primes 23..53 */
|
| 167 |
+
export const SHA384_IV: Uint32Array = /* @__PURE__ */ Uint32Array.from([
|
| 168 |
+
0xcbbb9d5d, 0xc1059ed8, 0x629a292a, 0x367cd507, 0x9159015a, 0x3070dd17, 0x152fecd8, 0xf70e5939,
|
| 169 |
+
0x67332667, 0xffc00b31, 0x8eb44a87, 0x68581511, 0xdb0c2e0d, 0x64f98fa7, 0x47b5481d, 0xbefa4fa4,
|
| 170 |
+
]);
|
| 171 |
+
|
| 172 |
+
/** Initial SHA512 state. Bits 0..64 of frac part of sqrt of primes 2..19 */
|
| 173 |
+
export const SHA512_IV: Uint32Array = /* @__PURE__ */ Uint32Array.from([
|
| 174 |
+
0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa73b, 0x3c6ef372, 0xfe94f82b, 0xa54ff53a, 0x5f1d36f1,
|
| 175 |
+
0x510e527f, 0xade682d1, 0x9b05688c, 0x2b3e6c1f, 0x1f83d9ab, 0xfb41bd6b, 0x5be0cd19, 0x137e2179,
|
| 176 |
+
]);
|
node_modules/@noble/hashes/src/_u64.ts
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Internal helpers for u64. BigUint64Array is too slow as per 2025, so we implement it using Uint32Array.
|
| 3 |
+
* @todo re-check https://issues.chromium.org/issues/42212588
|
| 4 |
+
* @module
|
| 5 |
+
*/
|
| 6 |
+
const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
|
| 7 |
+
const _32n = /* @__PURE__ */ BigInt(32);
|
| 8 |
+
|
| 9 |
+
function fromBig(
|
| 10 |
+
n: bigint,
|
| 11 |
+
le = false
|
| 12 |
+
): {
|
| 13 |
+
h: number;
|
| 14 |
+
l: number;
|
| 15 |
+
} {
|
| 16 |
+
if (le) return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
|
| 17 |
+
return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
function split(lst: bigint[], le = false): Uint32Array[] {
|
| 21 |
+
const len = lst.length;
|
| 22 |
+
let Ah = new Uint32Array(len);
|
| 23 |
+
let Al = new Uint32Array(len);
|
| 24 |
+
for (let i = 0; i < len; i++) {
|
| 25 |
+
const { h, l } = fromBig(lst[i], le);
|
| 26 |
+
[Ah[i], Al[i]] = [h, l];
|
| 27 |
+
}
|
| 28 |
+
return [Ah, Al];
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
const toBig = (h: number, l: number): bigint => (BigInt(h >>> 0) << _32n) | BigInt(l >>> 0);
|
| 32 |
+
// for Shift in [0, 32)
|
| 33 |
+
const shrSH = (h: number, _l: number, s: number): number => h >>> s;
|
| 34 |
+
const shrSL = (h: number, l: number, s: number): number => (h << (32 - s)) | (l >>> s);
|
| 35 |
+
// Right rotate for Shift in [1, 32)
|
| 36 |
+
const rotrSH = (h: number, l: number, s: number): number => (h >>> s) | (l << (32 - s));
|
| 37 |
+
const rotrSL = (h: number, l: number, s: number): number => (h << (32 - s)) | (l >>> s);
|
| 38 |
+
// Right rotate for Shift in (32, 64), NOTE: 32 is special case.
|
| 39 |
+
const rotrBH = (h: number, l: number, s: number): number => (h << (64 - s)) | (l >>> (s - 32));
|
| 40 |
+
const rotrBL = (h: number, l: number, s: number): number => (h >>> (s - 32)) | (l << (64 - s));
|
| 41 |
+
// Right rotate for shift===32 (just swaps l&h)
|
| 42 |
+
const rotr32H = (_h: number, l: number): number => l;
|
| 43 |
+
const rotr32L = (h: number, _l: number): number => h;
|
| 44 |
+
// Left rotate for Shift in [1, 32)
|
| 45 |
+
const rotlSH = (h: number, l: number, s: number): number => (h << s) | (l >>> (32 - s));
|
| 46 |
+
const rotlSL = (h: number, l: number, s: number): number => (l << s) | (h >>> (32 - s));
|
| 47 |
+
// Left rotate for Shift in (32, 64), NOTE: 32 is special case.
|
| 48 |
+
const rotlBH = (h: number, l: number, s: number): number => (l << (s - 32)) | (h >>> (64 - s));
|
| 49 |
+
const rotlBL = (h: number, l: number, s: number): number => (h << (s - 32)) | (l >>> (64 - s));
|
| 50 |
+
|
| 51 |
+
// JS uses 32-bit signed integers for bitwise operations which means we cannot
|
| 52 |
+
// simple take carry out of low bit sum by shift, we need to use division.
|
| 53 |
+
function add(
|
| 54 |
+
Ah: number,
|
| 55 |
+
Al: number,
|
| 56 |
+
Bh: number,
|
| 57 |
+
Bl: number
|
| 58 |
+
): {
|
| 59 |
+
h: number;
|
| 60 |
+
l: number;
|
| 61 |
+
} {
|
| 62 |
+
const l = (Al >>> 0) + (Bl >>> 0);
|
| 63 |
+
return { h: (Ah + Bh + ((l / 2 ** 32) | 0)) | 0, l: l | 0 };
|
| 64 |
+
}
|
| 65 |
+
// Addition with more than 2 elements
|
| 66 |
+
const add3L = (Al: number, Bl: number, Cl: number): number => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
|
| 67 |
+
const add3H = (low: number, Ah: number, Bh: number, Ch: number): number =>
|
| 68 |
+
(Ah + Bh + Ch + ((low / 2 ** 32) | 0)) | 0;
|
| 69 |
+
const add4L = (Al: number, Bl: number, Cl: number, Dl: number): number =>
|
| 70 |
+
(Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
|
| 71 |
+
const add4H = (low: number, Ah: number, Bh: number, Ch: number, Dh: number): number =>
|
| 72 |
+
(Ah + Bh + Ch + Dh + ((low / 2 ** 32) | 0)) | 0;
|
| 73 |
+
const add5L = (Al: number, Bl: number, Cl: number, Dl: number, El: number): number =>
|
| 74 |
+
(Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
|
| 75 |
+
const add5H = (low: number, Ah: number, Bh: number, Ch: number, Dh: number, Eh: number): number =>
|
| 76 |
+
(Ah + Bh + Ch + Dh + Eh + ((low / 2 ** 32) | 0)) | 0;
|
| 77 |
+
|
| 78 |
+
// prettier-ignore
|
| 79 |
+
export {
|
| 80 |
+
add, add3H, add3L, add4H, add4L, add5H, add5L, fromBig, rotlBH, rotlBL, rotlSH, rotlSL, rotr32H, rotr32L, rotrBH, rotrBL, rotrSH, rotrSL, shrSH, shrSL, split, toBig
|
| 81 |
+
};
|
| 82 |
+
// prettier-ignore
|
| 83 |
+
const u64: { fromBig: typeof fromBig; split: typeof split; toBig: (h: number, l: number) => bigint; shrSH: (h: number, _l: number, s: number) => number; shrSL: (h: number, l: number, s: number) => number; rotrSH: (h: number, l: number, s: number) => number; rotrSL: (h: number, l: number, s: number) => number; rotrBH: (h: number, l: number, s: number) => number; rotrBL: (h: number, l: number, s: number) => number; rotr32H: (_h: number, l: number) => number; rotr32L: (h: number, _l: number) => number; rotlSH: (h: number, l: number, s: number) => number; rotlSL: (h: number, l: number, s: number) => number; rotlBH: (h: number, l: number, s: number) => number; rotlBL: (h: number, l: number, s: number) => number; add: typeof add; add3L: (Al: number, Bl: number, Cl: number) => number; add3H: (low: number, Ah: number, Bh: number, Ch: number) => number; add4L: (Al: number, Bl: number, Cl: number, Dl: number) => number; add4H: (low: number, Ah: number, Bh: number, Ch: number, Dh: number) => number; add5H: (low: number, Ah: number, Bh: number, Ch: number, Dh: number, Eh: number) => number; add5L: (Al: number, Bl: number, Cl: number, Dl: number, El: number) => number; } = {
|
| 84 |
+
fromBig, split, toBig,
|
| 85 |
+
shrSH, shrSL,
|
| 86 |
+
rotrSH, rotrSL, rotrBH, rotrBL,
|
| 87 |
+
rotr32H, rotr32L,
|
| 88 |
+
rotlSH, rotlSL, rotlBH, rotlBL,
|
| 89 |
+
add, add3L, add3H, add4L, add4H, add5H, add5L,
|
| 90 |
+
};
|
| 91 |
+
export default u64;
|
node_modules/@noble/hashes/src/argon2.ts
ADDED
|
@@ -0,0 +1,497 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Argon2 KDF from RFC 9106. Can be used to create a key from password and salt.
|
| 3 |
+
* We suggest to use Scrypt. JS Argon is 2-10x slower than native code because of 64-bitness:
|
| 4 |
+
* * argon uses uint64, but JS doesn't have fast uint64array
|
| 5 |
+
* * uint64 multiplication is 1/3 of time
|
| 6 |
+
* * `P` function would be very nice with u64, because most of value will be in registers,
|
| 7 |
+
* hovewer with u32 it will require 32 registers, which is too much.
|
| 8 |
+
* * JS arrays do slow bound checks, so reading from `A2_BUF` slows it down
|
| 9 |
+
* @module
|
| 10 |
+
*/
|
| 11 |
+
import { add3H, add3L, rotr32H, rotr32L, rotrBH, rotrBL, rotrSH, rotrSL } from './_u64.ts';
|
| 12 |
+
import { blake2b } from './blake2.ts';
|
| 13 |
+
import { abytes, clean, kdfInputToBytes, nextTick, u32, u8, type KDFInput } from './utils.ts';
|
| 14 |
+
|
| 15 |
+
const AT = { Argond2d: 0, Argon2i: 1, Argon2id: 2 } as const;
|
| 16 |
+
type Types = (typeof AT)[keyof typeof AT];
|
| 17 |
+
|
| 18 |
+
const ARGON2_SYNC_POINTS = 4;
|
| 19 |
+
const abytesOrZero = (buf?: KDFInput) => {
|
| 20 |
+
if (buf === undefined) return Uint8Array.of();
|
| 21 |
+
return kdfInputToBytes(buf);
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
// u32 * u32 = u64
|
| 25 |
+
function mul(a: number, b: number) {
|
| 26 |
+
const aL = a & 0xffff;
|
| 27 |
+
const aH = a >>> 16;
|
| 28 |
+
const bL = b & 0xffff;
|
| 29 |
+
const bH = b >>> 16;
|
| 30 |
+
const ll = Math.imul(aL, bL);
|
| 31 |
+
const hl = Math.imul(aH, bL);
|
| 32 |
+
const lh = Math.imul(aL, bH);
|
| 33 |
+
const hh = Math.imul(aH, bH);
|
| 34 |
+
const carry = (ll >>> 16) + (hl & 0xffff) + lh;
|
| 35 |
+
const high = (hh + (hl >>> 16) + (carry >>> 16)) | 0;
|
| 36 |
+
const low = (carry << 16) | (ll & 0xffff);
|
| 37 |
+
return { h: high, l: low };
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
function mul2(a: number, b: number) {
|
| 41 |
+
// 2 * a * b (via shifts)
|
| 42 |
+
const { h, l } = mul(a, b);
|
| 43 |
+
return { h: ((h << 1) | (l >>> 31)) & 0xffff_ffff, l: (l << 1) & 0xffff_ffff };
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
// BlaMka permutation for Argon2
|
| 47 |
+
// A + B + (2 * u32(A) * u32(B))
|
| 48 |
+
function blamka(Ah: number, Al: number, Bh: number, Bl: number) {
|
| 49 |
+
const { h: Ch, l: Cl } = mul2(Al, Bl);
|
| 50 |
+
// A + B + (2 * A * B)
|
| 51 |
+
const Rll = add3L(Al, Bl, Cl);
|
| 52 |
+
return { h: add3H(Rll, Ah, Bh, Ch), l: Rll | 0 };
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
// Temporary block buffer
|
| 56 |
+
const A2_BUF = new Uint32Array(256); // 1024 bytes (matrix 16x16)
|
| 57 |
+
|
| 58 |
+
function G(a: number, b: number, c: number, d: number) {
|
| 59 |
+
let Al = A2_BUF[2*a], Ah = A2_BUF[2*a + 1]; // prettier-ignore
|
| 60 |
+
let Bl = A2_BUF[2*b], Bh = A2_BUF[2*b + 1]; // prettier-ignore
|
| 61 |
+
let Cl = A2_BUF[2*c], Ch = A2_BUF[2*c + 1]; // prettier-ignore
|
| 62 |
+
let Dl = A2_BUF[2*d], Dh = A2_BUF[2*d + 1]; // prettier-ignore
|
| 63 |
+
|
| 64 |
+
({ h: Ah, l: Al } = blamka(Ah, Al, Bh, Bl));
|
| 65 |
+
({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
|
| 66 |
+
({ Dh, Dl } = { Dh: rotr32H(Dh, Dl), Dl: rotr32L(Dh, Dl) });
|
| 67 |
+
|
| 68 |
+
({ h: Ch, l: Cl } = blamka(Ch, Cl, Dh, Dl));
|
| 69 |
+
({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
|
| 70 |
+
({ Bh, Bl } = { Bh: rotrSH(Bh, Bl, 24), Bl: rotrSL(Bh, Bl, 24) });
|
| 71 |
+
|
| 72 |
+
({ h: Ah, l: Al } = blamka(Ah, Al, Bh, Bl));
|
| 73 |
+
({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
|
| 74 |
+
({ Dh, Dl } = { Dh: rotrSH(Dh, Dl, 16), Dl: rotrSL(Dh, Dl, 16) });
|
| 75 |
+
|
| 76 |
+
({ h: Ch, l: Cl } = blamka(Ch, Cl, Dh, Dl));
|
| 77 |
+
({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
|
| 78 |
+
({ Bh, Bl } = { Bh: rotrBH(Bh, Bl, 63), Bl: rotrBL(Bh, Bl, 63) });
|
| 79 |
+
|
| 80 |
+
(A2_BUF[2 * a] = Al), (A2_BUF[2 * a + 1] = Ah);
|
| 81 |
+
(A2_BUF[2 * b] = Bl), (A2_BUF[2 * b + 1] = Bh);
|
| 82 |
+
(A2_BUF[2 * c] = Cl), (A2_BUF[2 * c + 1] = Ch);
|
| 83 |
+
(A2_BUF[2 * d] = Dl), (A2_BUF[2 * d + 1] = Dh);
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
// prettier-ignore
|
| 87 |
+
function P(
|
| 88 |
+
v00: number, v01: number, v02: number, v03: number, v04: number, v05: number, v06: number, v07: number,
|
| 89 |
+
v08: number, v09: number, v10: number, v11: number, v12: number, v13: number, v14: number, v15: number,
|
| 90 |
+
) {
|
| 91 |
+
G(v00, v04, v08, v12);
|
| 92 |
+
G(v01, v05, v09, v13);
|
| 93 |
+
G(v02, v06, v10, v14);
|
| 94 |
+
G(v03, v07, v11, v15);
|
| 95 |
+
G(v00, v05, v10, v15);
|
| 96 |
+
G(v01, v06, v11, v12);
|
| 97 |
+
G(v02, v07, v08, v13);
|
| 98 |
+
G(v03, v04, v09, v14);
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
function block(x: Uint32Array, xPos: number, yPos: number, outPos: number, needXor: boolean) {
|
| 102 |
+
for (let i = 0; i < 256; i++) A2_BUF[i] = x[xPos + i] ^ x[yPos + i];
|
| 103 |
+
// columns (8)
|
| 104 |
+
for (let i = 0; i < 128; i += 16) {
|
| 105 |
+
// prettier-ignore
|
| 106 |
+
P(
|
| 107 |
+
i, i + 1, i + 2, i + 3, i + 4, i + 5, i + 6, i + 7,
|
| 108 |
+
i + 8, i + 9, i + 10, i + 11, i + 12, i + 13, i + 14, i + 15
|
| 109 |
+
);
|
| 110 |
+
}
|
| 111 |
+
// rows (8)
|
| 112 |
+
for (let i = 0; i < 16; i += 2) {
|
| 113 |
+
// prettier-ignore
|
| 114 |
+
P(
|
| 115 |
+
i, i + 1, i + 16, i + 17, i + 32, i + 33, i + 48, i + 49,
|
| 116 |
+
i + 64, i + 65, i + 80, i + 81, i + 96, i + 97, i + 112, i + 113
|
| 117 |
+
);
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
if (needXor) for (let i = 0; i < 256; i++) x[outPos + i] ^= A2_BUF[i] ^ x[xPos + i] ^ x[yPos + i];
|
| 121 |
+
else for (let i = 0; i < 256; i++) x[outPos + i] = A2_BUF[i] ^ x[xPos + i] ^ x[yPos + i];
|
| 122 |
+
clean(A2_BUF);
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
// Variable-Length Hash Function H'
|
| 126 |
+
function Hp(A: Uint32Array, dkLen: number) {
|
| 127 |
+
const A8 = u8(A);
|
| 128 |
+
const T = new Uint32Array(1);
|
| 129 |
+
const T8 = u8(T);
|
| 130 |
+
T[0] = dkLen;
|
| 131 |
+
// Fast path
|
| 132 |
+
if (dkLen <= 64) return blake2b.create({ dkLen }).update(T8).update(A8).digest();
|
| 133 |
+
const out = new Uint8Array(dkLen);
|
| 134 |
+
let V = blake2b.create({}).update(T8).update(A8).digest();
|
| 135 |
+
let pos = 0;
|
| 136 |
+
// First block
|
| 137 |
+
out.set(V.subarray(0, 32));
|
| 138 |
+
pos += 32;
|
| 139 |
+
// Rest blocks
|
| 140 |
+
for (; dkLen - pos > 64; pos += 32) {
|
| 141 |
+
const Vh = blake2b.create({}).update(V);
|
| 142 |
+
Vh.digestInto(V);
|
| 143 |
+
Vh.destroy();
|
| 144 |
+
out.set(V.subarray(0, 32), pos);
|
| 145 |
+
}
|
| 146 |
+
// Last block
|
| 147 |
+
out.set(blake2b(V, { dkLen: dkLen - pos }), pos);
|
| 148 |
+
clean(V, T);
|
| 149 |
+
return u32(out);
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
// Used only inside process block!
|
| 153 |
+
function indexAlpha(
|
| 154 |
+
r: number,
|
| 155 |
+
s: number,
|
| 156 |
+
laneLen: number,
|
| 157 |
+
segmentLen: number,
|
| 158 |
+
index: number,
|
| 159 |
+
randL: number,
|
| 160 |
+
sameLane: boolean = false
|
| 161 |
+
) {
|
| 162 |
+
// This is ugly, but close enough to reference implementation.
|
| 163 |
+
let area: number;
|
| 164 |
+
if (r === 0) {
|
| 165 |
+
if (s === 0) area = index - 1;
|
| 166 |
+
else if (sameLane) area = s * segmentLen + index - 1;
|
| 167 |
+
else area = s * segmentLen + (index == 0 ? -1 : 0);
|
| 168 |
+
} else if (sameLane) area = laneLen - segmentLen + index - 1;
|
| 169 |
+
else area = laneLen - segmentLen + (index == 0 ? -1 : 0);
|
| 170 |
+
const startPos = r !== 0 && s !== ARGON2_SYNC_POINTS - 1 ? (s + 1) * segmentLen : 0;
|
| 171 |
+
const rel = area - 1 - mul(area, mul(randL, randL).h).h;
|
| 172 |
+
return (startPos + rel) % laneLen;
|
| 173 |
+
}
|
| 174 |
+
|
| 175 |
+
/**
|
| 176 |
+
* Argon2 options.
|
| 177 |
+
* * t: time cost, m: mem cost in kb, p: parallelization.
|
| 178 |
+
* * key: optional key. personalization: arbitrary extra data.
|
| 179 |
+
* * dkLen: desired number of output bytes.
|
| 180 |
+
*/
|
| 181 |
+
export type ArgonOpts = {
|
| 182 |
+
t: number; // Time cost, iterations count
|
| 183 |
+
m: number; // Memory cost (in KB)
|
| 184 |
+
p: number; // Parallelization parameter
|
| 185 |
+
version?: number; // Default: 0x13 (19)
|
| 186 |
+
key?: KDFInput; // Optional key
|
| 187 |
+
personalization?: KDFInput; // Optional arbitrary extra data
|
| 188 |
+
dkLen?: number; // Desired number of returned bytes
|
| 189 |
+
asyncTick?: number; // Maximum time in ms for which async function can block execution
|
| 190 |
+
maxmem?: number;
|
| 191 |
+
onProgress?: (progress: number) => void;
|
| 192 |
+
};
|
| 193 |
+
|
| 194 |
+
const maxUint32 = Math.pow(2, 32);
|
| 195 |
+
function isU32(num: number) {
|
| 196 |
+
return Number.isSafeInteger(num) && num >= 0 && num < maxUint32;
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
function argon2Opts(opts: ArgonOpts) {
|
| 200 |
+
const merged: any = {
|
| 201 |
+
version: 0x13,
|
| 202 |
+
dkLen: 32,
|
| 203 |
+
maxmem: maxUint32 - 1,
|
| 204 |
+
asyncTick: 10,
|
| 205 |
+
};
|
| 206 |
+
for (let [k, v] of Object.entries(opts)) if (v != null) merged[k] = v;
|
| 207 |
+
|
| 208 |
+
const { dkLen, p, m, t, version, onProgress } = merged;
|
| 209 |
+
if (!isU32(dkLen) || dkLen < 4) throw new Error('dkLen should be at least 4 bytes');
|
| 210 |
+
if (!isU32(p) || p < 1 || p >= Math.pow(2, 24)) throw new Error('p should be 1 <= p < 2^24');
|
| 211 |
+
if (!isU32(m)) throw new Error('m should be 0 <= m < 2^32');
|
| 212 |
+
if (!isU32(t) || t < 1) throw new Error('t (iterations) should be 1 <= t < 2^32');
|
| 213 |
+
if (onProgress !== undefined && typeof onProgress !== 'function')
|
| 214 |
+
throw new Error('progressCb should be function');
|
| 215 |
+
/*
|
| 216 |
+
Memory size m MUST be an integer number of kibibytes from 8*p to 2^(32)-1. The actual number of blocks is m', which is m rounded down to the nearest multiple of 4*p.
|
| 217 |
+
*/
|
| 218 |
+
if (!isU32(m) || m < 8 * p) throw new Error('memory should be at least 8*p bytes');
|
| 219 |
+
if (version !== 0x10 && version !== 0x13) throw new Error('unknown version=' + version);
|
| 220 |
+
return merged;
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
function argon2Init(password: KDFInput, salt: KDFInput, type: Types, opts: ArgonOpts) {
|
| 224 |
+
password = kdfInputToBytes(password);
|
| 225 |
+
salt = kdfInputToBytes(salt);
|
| 226 |
+
abytes(password);
|
| 227 |
+
abytes(salt);
|
| 228 |
+
if (!isU32(password.length)) throw new Error('password should be less than 4 GB');
|
| 229 |
+
if (!isU32(salt.length) || salt.length < 8)
|
| 230 |
+
throw new Error('salt should be at least 8 bytes and less than 4 GB');
|
| 231 |
+
if (!Object.values(AT).includes(type)) throw new Error('invalid type');
|
| 232 |
+
let { p, dkLen, m, t, version, key, personalization, maxmem, onProgress, asyncTick } =
|
| 233 |
+
argon2Opts(opts);
|
| 234 |
+
|
| 235 |
+
// Validation
|
| 236 |
+
key = abytesOrZero(key);
|
| 237 |
+
personalization = abytesOrZero(personalization);
|
| 238 |
+
// H_0 = H^(64)(LE32(p) || LE32(T) || LE32(m) || LE32(t) ||
|
| 239 |
+
// LE32(v) || LE32(y) || LE32(length(P)) || P ||
|
| 240 |
+
// LE32(length(S)) || S || LE32(length(K)) || K ||
|
| 241 |
+
// LE32(length(X)) || X)
|
| 242 |
+
const h = blake2b.create({});
|
| 243 |
+
const BUF = new Uint32Array(1);
|
| 244 |
+
const BUF8 = u8(BUF);
|
| 245 |
+
for (let item of [p, dkLen, m, t, version, type]) {
|
| 246 |
+
BUF[0] = item;
|
| 247 |
+
h.update(BUF8);
|
| 248 |
+
}
|
| 249 |
+
for (let i of [password, salt, key, personalization]) {
|
| 250 |
+
BUF[0] = i.length; // BUF is u32 array, this is valid
|
| 251 |
+
h.update(BUF8).update(i);
|
| 252 |
+
}
|
| 253 |
+
const H0 = new Uint32Array(18);
|
| 254 |
+
const H0_8 = u8(H0);
|
| 255 |
+
h.digestInto(H0_8);
|
| 256 |
+
// 256 u32 = 1024 (BLOCK_SIZE), fills A2_BUF on processing
|
| 257 |
+
|
| 258 |
+
// Params
|
| 259 |
+
const lanes = p;
|
| 260 |
+
// m' = 4 * p * floor (m / 4p)
|
| 261 |
+
const mP = 4 * p * Math.floor(m / (ARGON2_SYNC_POINTS * p));
|
| 262 |
+
//q = m' / p columns
|
| 263 |
+
const laneLen = Math.floor(mP / p);
|
| 264 |
+
const segmentLen = Math.floor(laneLen / ARGON2_SYNC_POINTS);
|
| 265 |
+
const memUsed = mP * 256;
|
| 266 |
+
if (!isU32(maxmem) || memUsed > maxmem)
|
| 267 |
+
throw new Error(
|
| 268 |
+
'mem should be less than 2**32, got: maxmem=' + maxmem + ', memused=' + memUsed
|
| 269 |
+
);
|
| 270 |
+
const B = new Uint32Array(memUsed);
|
| 271 |
+
// Fill first blocks
|
| 272 |
+
for (let l = 0; l < p; l++) {
|
| 273 |
+
const i = 256 * laneLen * l;
|
| 274 |
+
// B[i][0] = H'^(1024)(H_0 || LE32(0) || LE32(i))
|
| 275 |
+
H0[17] = l;
|
| 276 |
+
H0[16] = 0;
|
| 277 |
+
B.set(Hp(H0, 1024), i);
|
| 278 |
+
// B[i][1] = H'^(1024)(H_0 || LE32(1) || LE32(i))
|
| 279 |
+
H0[16] = 1;
|
| 280 |
+
B.set(Hp(H0, 1024), i + 256);
|
| 281 |
+
}
|
| 282 |
+
let perBlock = () => {};
|
| 283 |
+
if (onProgress) {
|
| 284 |
+
const totalBlock = t * ARGON2_SYNC_POINTS * p * segmentLen;
|
| 285 |
+
// Invoke callback if progress changes from 10.01 to 10.02
|
| 286 |
+
// Allows to draw smooth progress bar on up to 8K screen
|
| 287 |
+
const callbackPer = Math.max(Math.floor(totalBlock / 10000), 1);
|
| 288 |
+
let blockCnt = 0;
|
| 289 |
+
perBlock = () => {
|
| 290 |
+
blockCnt++;
|
| 291 |
+
if (onProgress && (!(blockCnt % callbackPer) || blockCnt === totalBlock))
|
| 292 |
+
onProgress(blockCnt / totalBlock);
|
| 293 |
+
};
|
| 294 |
+
}
|
| 295 |
+
clean(BUF, H0);
|
| 296 |
+
return { type, mP, p, t, version, B, laneLen, lanes, segmentLen, dkLen, perBlock, asyncTick };
|
| 297 |
+
}
|
| 298 |
+
|
| 299 |
+
function argon2Output(B: Uint32Array, p: number, laneLen: number, dkLen: number) {
|
| 300 |
+
const B_final = new Uint32Array(256);
|
| 301 |
+
for (let l = 0; l < p; l++)
|
| 302 |
+
for (let j = 0; j < 256; j++) B_final[j] ^= B[256 * (laneLen * l + laneLen - 1) + j];
|
| 303 |
+
const res = u8(Hp(B_final, dkLen));
|
| 304 |
+
clean(B_final);
|
| 305 |
+
return res;
|
| 306 |
+
}
|
| 307 |
+
|
| 308 |
+
function processBlock(
|
| 309 |
+
B: Uint32Array,
|
| 310 |
+
address: Uint32Array,
|
| 311 |
+
l: number,
|
| 312 |
+
r: number,
|
| 313 |
+
s: number,
|
| 314 |
+
index: number,
|
| 315 |
+
laneLen: number,
|
| 316 |
+
segmentLen: number,
|
| 317 |
+
lanes: number,
|
| 318 |
+
offset: number,
|
| 319 |
+
prev: number,
|
| 320 |
+
dataIndependent: boolean,
|
| 321 |
+
needXor: boolean
|
| 322 |
+
) {
|
| 323 |
+
if (offset % laneLen) prev = offset - 1;
|
| 324 |
+
let randL, randH;
|
| 325 |
+
if (dataIndependent) {
|
| 326 |
+
let i128 = index % 128;
|
| 327 |
+
if (i128 === 0) {
|
| 328 |
+
address[256 + 12]++;
|
| 329 |
+
block(address, 256, 2 * 256, 0, false);
|
| 330 |
+
block(address, 0, 2 * 256, 0, false);
|
| 331 |
+
}
|
| 332 |
+
randL = address[2 * i128];
|
| 333 |
+
randH = address[2 * i128 + 1];
|
| 334 |
+
} else {
|
| 335 |
+
const T = 256 * prev;
|
| 336 |
+
randL = B[T];
|
| 337 |
+
randH = B[T + 1];
|
| 338 |
+
}
|
| 339 |
+
// address block
|
| 340 |
+
const refLane = r === 0 && s === 0 ? l : randH % lanes;
|
| 341 |
+
const refPos = indexAlpha(r, s, laneLen, segmentLen, index, randL, refLane == l);
|
| 342 |
+
const refBlock = laneLen * refLane + refPos;
|
| 343 |
+
// B[i][j] = G(B[i][j-1], B[l][z])
|
| 344 |
+
block(B, 256 * prev, 256 * refBlock, offset * 256, needXor);
|
| 345 |
+
}
|
| 346 |
+
|
| 347 |
+
function argon2(type: Types, password: KDFInput, salt: KDFInput, opts: ArgonOpts) {
|
| 348 |
+
const { mP, p, t, version, B, laneLen, lanes, segmentLen, dkLen, perBlock } = argon2Init(
|
| 349 |
+
password,
|
| 350 |
+
salt,
|
| 351 |
+
type,
|
| 352 |
+
opts
|
| 353 |
+
);
|
| 354 |
+
// Pre-loop setup
|
| 355 |
+
// [address, input, zero_block] format so we can pass single U32 to block function
|
| 356 |
+
const address = new Uint32Array(3 * 256);
|
| 357 |
+
address[256 + 6] = mP;
|
| 358 |
+
address[256 + 8] = t;
|
| 359 |
+
address[256 + 10] = type;
|
| 360 |
+
for (let r = 0; r < t; r++) {
|
| 361 |
+
const needXor = r !== 0 && version === 0x13;
|
| 362 |
+
address[256 + 0] = r;
|
| 363 |
+
for (let s = 0; s < ARGON2_SYNC_POINTS; s++) {
|
| 364 |
+
address[256 + 4] = s;
|
| 365 |
+
const dataIndependent = type == AT.Argon2i || (type == AT.Argon2id && r === 0 && s < 2);
|
| 366 |
+
for (let l = 0; l < p; l++) {
|
| 367 |
+
address[256 + 2] = l;
|
| 368 |
+
address[256 + 12] = 0;
|
| 369 |
+
let startPos = 0;
|
| 370 |
+
if (r === 0 && s === 0) {
|
| 371 |
+
startPos = 2;
|
| 372 |
+
if (dataIndependent) {
|
| 373 |
+
address[256 + 12]++;
|
| 374 |
+
block(address, 256, 2 * 256, 0, false);
|
| 375 |
+
block(address, 0, 2 * 256, 0, false);
|
| 376 |
+
}
|
| 377 |
+
}
|
| 378 |
+
// current block postion
|
| 379 |
+
let offset = l * laneLen + s * segmentLen + startPos;
|
| 380 |
+
// previous block position
|
| 381 |
+
let prev = offset % laneLen ? offset - 1 : offset + laneLen - 1;
|
| 382 |
+
for (let index = startPos; index < segmentLen; index++, offset++, prev++) {
|
| 383 |
+
perBlock();
|
| 384 |
+
processBlock(
|
| 385 |
+
B,
|
| 386 |
+
address,
|
| 387 |
+
l,
|
| 388 |
+
r,
|
| 389 |
+
s,
|
| 390 |
+
index,
|
| 391 |
+
laneLen,
|
| 392 |
+
segmentLen,
|
| 393 |
+
lanes,
|
| 394 |
+
offset,
|
| 395 |
+
prev,
|
| 396 |
+
dataIndependent,
|
| 397 |
+
needXor
|
| 398 |
+
);
|
| 399 |
+
}
|
| 400 |
+
}
|
| 401 |
+
}
|
| 402 |
+
}
|
| 403 |
+
clean(address);
|
| 404 |
+
return argon2Output(B, p, laneLen, dkLen);
|
| 405 |
+
}
|
| 406 |
+
|
| 407 |
+
/** argon2d GPU-resistant version. */
|
| 408 |
+
export const argon2d = (password: KDFInput, salt: KDFInput, opts: ArgonOpts): Uint8Array =>
|
| 409 |
+
argon2(AT.Argond2d, password, salt, opts);
|
| 410 |
+
/** argon2i side-channel-resistant version. */
|
| 411 |
+
export const argon2i = (password: KDFInput, salt: KDFInput, opts: ArgonOpts): Uint8Array =>
|
| 412 |
+
argon2(AT.Argon2i, password, salt, opts);
|
| 413 |
+
/** argon2id, combining i+d, the most popular version from RFC 9106 */
|
| 414 |
+
export const argon2id = (password: KDFInput, salt: KDFInput, opts: ArgonOpts): Uint8Array =>
|
| 415 |
+
argon2(AT.Argon2id, password, salt, opts);
|
| 416 |
+
|
| 417 |
+
async function argon2Async(type: Types, password: KDFInput, salt: KDFInput, opts: ArgonOpts) {
|
| 418 |
+
const { mP, p, t, version, B, laneLen, lanes, segmentLen, dkLen, perBlock, asyncTick } =
|
| 419 |
+
argon2Init(password, salt, type, opts);
|
| 420 |
+
// Pre-loop setup
|
| 421 |
+
// [address, input, zero_block] format so we can pass single U32 to block function
|
| 422 |
+
const address = new Uint32Array(3 * 256);
|
| 423 |
+
address[256 + 6] = mP;
|
| 424 |
+
address[256 + 8] = t;
|
| 425 |
+
address[256 + 10] = type;
|
| 426 |
+
let ts = Date.now();
|
| 427 |
+
for (let r = 0; r < t; r++) {
|
| 428 |
+
const needXor = r !== 0 && version === 0x13;
|
| 429 |
+
address[256 + 0] = r;
|
| 430 |
+
for (let s = 0; s < ARGON2_SYNC_POINTS; s++) {
|
| 431 |
+
address[256 + 4] = s;
|
| 432 |
+
const dataIndependent = type == AT.Argon2i || (type == AT.Argon2id && r === 0 && s < 2);
|
| 433 |
+
for (let l = 0; l < p; l++) {
|
| 434 |
+
address[256 + 2] = l;
|
| 435 |
+
address[256 + 12] = 0;
|
| 436 |
+
let startPos = 0;
|
| 437 |
+
if (r === 0 && s === 0) {
|
| 438 |
+
startPos = 2;
|
| 439 |
+
if (dataIndependent) {
|
| 440 |
+
address[256 + 12]++;
|
| 441 |
+
block(address, 256, 2 * 256, 0, false);
|
| 442 |
+
block(address, 0, 2 * 256, 0, false);
|
| 443 |
+
}
|
| 444 |
+
}
|
| 445 |
+
// current block postion
|
| 446 |
+
let offset = l * laneLen + s * segmentLen + startPos;
|
| 447 |
+
// previous block position
|
| 448 |
+
let prev = offset % laneLen ? offset - 1 : offset + laneLen - 1;
|
| 449 |
+
for (let index = startPos; index < segmentLen; index++, offset++, prev++) {
|
| 450 |
+
perBlock();
|
| 451 |
+
processBlock(
|
| 452 |
+
B,
|
| 453 |
+
address,
|
| 454 |
+
l,
|
| 455 |
+
r,
|
| 456 |
+
s,
|
| 457 |
+
index,
|
| 458 |
+
laneLen,
|
| 459 |
+
segmentLen,
|
| 460 |
+
lanes,
|
| 461 |
+
offset,
|
| 462 |
+
prev,
|
| 463 |
+
dataIndependent,
|
| 464 |
+
needXor
|
| 465 |
+
);
|
| 466 |
+
// Date.now() is not monotonic, so in case if clock goes backwards we return return control too
|
| 467 |
+
const diff = Date.now() - ts;
|
| 468 |
+
if (!(diff >= 0 && diff < asyncTick)) {
|
| 469 |
+
await nextTick();
|
| 470 |
+
ts += diff;
|
| 471 |
+
}
|
| 472 |
+
}
|
| 473 |
+
}
|
| 474 |
+
}
|
| 475 |
+
}
|
| 476 |
+
clean(address);
|
| 477 |
+
return argon2Output(B, p, laneLen, dkLen);
|
| 478 |
+
}
|
| 479 |
+
|
| 480 |
+
/** argon2d async GPU-resistant version. */
|
| 481 |
+
export const argon2dAsync = (
|
| 482 |
+
password: KDFInput,
|
| 483 |
+
salt: KDFInput,
|
| 484 |
+
opts: ArgonOpts
|
| 485 |
+
): Promise<Uint8Array> => argon2Async(AT.Argond2d, password, salt, opts);
|
| 486 |
+
/** argon2i async side-channel-resistant version. */
|
| 487 |
+
export const argon2iAsync = (
|
| 488 |
+
password: KDFInput,
|
| 489 |
+
salt: KDFInput,
|
| 490 |
+
opts: ArgonOpts
|
| 491 |
+
): Promise<Uint8Array> => argon2Async(AT.Argon2i, password, salt, opts);
|
| 492 |
+
/** argon2id async, combining i+d, the most popular version from RFC 9106 */
|
| 493 |
+
export const argon2idAsync = (
|
| 494 |
+
password: KDFInput,
|
| 495 |
+
salt: KDFInput,
|
| 496 |
+
opts: ArgonOpts
|
| 497 |
+
): Promise<Uint8Array> => argon2Async(AT.Argon2id, password, salt, opts);
|
node_modules/@noble/hashes/src/blake1.ts
ADDED
|
@@ -0,0 +1,534 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Blake1 legacy hash function, one of SHA3 proposals.
|
| 3 |
+
* Rarely used. Check out blake2 or blake3 instead.
|
| 4 |
+
* https://www.aumasson.jp/blake/blake.pdf
|
| 5 |
+
*
|
| 6 |
+
* In the best case, there are 0 allocations.
|
| 7 |
+
*
|
| 8 |
+
* Differences from blake2:
|
| 9 |
+
*
|
| 10 |
+
* - BE instead of LE
|
| 11 |
+
* - Paddings, similar to MD5, RIPEMD, SHA1, SHA2, but:
|
| 12 |
+
* - length flag is located before actual length
|
| 13 |
+
* - padding block is compressed differently (no lengths)
|
| 14 |
+
* Instead of msg[sigma[k]], we have `msg[sigma[k]] ^ constants[sigma[k-1]]`
|
| 15 |
+
* (-1 for g1, g2 without -1)
|
| 16 |
+
* - Salt is XOR-ed into constants instead of state
|
| 17 |
+
* - Salt is XOR-ed with output in `compress`
|
| 18 |
+
* - Additional rows (+64 bytes) in SIGMA for new rounds
|
| 19 |
+
* - Different round count:
|
| 20 |
+
* - 14 / 10 rounds in blake256 / blake2s
|
| 21 |
+
* - 16 / 12 rounds in blake512 / blake2b
|
| 22 |
+
* - blake512: G1b: rotr 24 -> 25, G2b: rotr 63 -> 11
|
| 23 |
+
* @module
|
| 24 |
+
*/
|
| 25 |
+
import { BSIGMA, G1s, G2s } from './_blake.ts';
|
| 26 |
+
import { setBigUint64, SHA224_IV, SHA256_IV, SHA384_IV, SHA512_IV } from './_md.ts';
|
| 27 |
+
import * as u64 from './_u64.ts';
|
| 28 |
+
// prettier-ignore
|
| 29 |
+
import {
|
| 30 |
+
abytes, aexists, aoutput,
|
| 31 |
+
clean, createOptHasher,
|
| 32 |
+
createView, Hash, toBytes,
|
| 33 |
+
type CHashO, type Input,
|
| 34 |
+
} from './utils.ts';
|
| 35 |
+
|
| 36 |
+
/** Blake1 options. Basically just "salt" */
|
| 37 |
+
export type BlakeOpts = {
|
| 38 |
+
salt?: Uint8Array;
|
| 39 |
+
};
|
| 40 |
+
|
| 41 |
+
// Empty zero-filled salt
|
| 42 |
+
const EMPTY_SALT = /* @__PURE__ */ new Uint32Array(8);
|
| 43 |
+
|
| 44 |
+
abstract class BLAKE1<T extends BLAKE1<T>> extends Hash<T> {
|
| 45 |
+
protected finished = false;
|
| 46 |
+
protected length = 0;
|
| 47 |
+
protected pos = 0;
|
| 48 |
+
protected destroyed = false;
|
| 49 |
+
// For partial updates less than block size
|
| 50 |
+
protected buffer: Uint8Array;
|
| 51 |
+
protected view: DataView;
|
| 52 |
+
protected salt: Uint32Array;
|
| 53 |
+
abstract compress(view: DataView, offset: number, withLength?: boolean): void;
|
| 54 |
+
protected abstract get(): number[];
|
| 55 |
+
protected abstract set(...args: number[]): void;
|
| 56 |
+
|
| 57 |
+
readonly blockLen: number;
|
| 58 |
+
readonly outputLen: number;
|
| 59 |
+
private lengthFlag: number;
|
| 60 |
+
private counterLen: number;
|
| 61 |
+
protected constants: Uint32Array;
|
| 62 |
+
|
| 63 |
+
constructor(
|
| 64 |
+
blockLen: number,
|
| 65 |
+
outputLen: number,
|
| 66 |
+
lengthFlag: number,
|
| 67 |
+
counterLen: number,
|
| 68 |
+
saltLen: number,
|
| 69 |
+
constants: Uint32Array,
|
| 70 |
+
opts: BlakeOpts = {}
|
| 71 |
+
) {
|
| 72 |
+
super();
|
| 73 |
+
const { salt } = opts;
|
| 74 |
+
this.blockLen = blockLen;
|
| 75 |
+
this.outputLen = outputLen;
|
| 76 |
+
this.lengthFlag = lengthFlag;
|
| 77 |
+
this.counterLen = counterLen;
|
| 78 |
+
this.buffer = new Uint8Array(blockLen);
|
| 79 |
+
this.view = createView(this.buffer);
|
| 80 |
+
if (salt) {
|
| 81 |
+
let slt = salt;
|
| 82 |
+
slt = toBytes(slt);
|
| 83 |
+
abytes(slt);
|
| 84 |
+
if (slt.length !== 4 * saltLen) throw new Error('wrong salt length');
|
| 85 |
+
const salt32 = (this.salt = new Uint32Array(saltLen));
|
| 86 |
+
const sv = createView(slt);
|
| 87 |
+
this.constants = constants.slice();
|
| 88 |
+
for (let i = 0, offset = 0; i < salt32.length; i++, offset += 4) {
|
| 89 |
+
salt32[i] = sv.getUint32(offset, false);
|
| 90 |
+
this.constants[i] ^= salt32[i];
|
| 91 |
+
}
|
| 92 |
+
} else {
|
| 93 |
+
this.salt = EMPTY_SALT;
|
| 94 |
+
this.constants = constants;
|
| 95 |
+
}
|
| 96 |
+
}
|
| 97 |
+
update(data: Input): this {
|
| 98 |
+
aexists(this);
|
| 99 |
+
data = toBytes(data);
|
| 100 |
+
abytes(data);
|
| 101 |
+
// From _md, but update length before each compress
|
| 102 |
+
const { view, buffer, blockLen } = this;
|
| 103 |
+
const len = data.length;
|
| 104 |
+
let dataView;
|
| 105 |
+
for (let pos = 0; pos < len; ) {
|
| 106 |
+
const take = Math.min(blockLen - this.pos, len - pos);
|
| 107 |
+
// Fast path: we have at least one block in input, cast it to view and process
|
| 108 |
+
if (take === blockLen) {
|
| 109 |
+
if (!dataView) dataView = createView(data);
|
| 110 |
+
for (; blockLen <= len - pos; pos += blockLen) {
|
| 111 |
+
this.length += blockLen;
|
| 112 |
+
this.compress(dataView, pos);
|
| 113 |
+
}
|
| 114 |
+
continue;
|
| 115 |
+
}
|
| 116 |
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
| 117 |
+
this.pos += take;
|
| 118 |
+
pos += take;
|
| 119 |
+
if (this.pos === blockLen) {
|
| 120 |
+
this.length += blockLen;
|
| 121 |
+
this.compress(view, 0, true);
|
| 122 |
+
this.pos = 0;
|
| 123 |
+
}
|
| 124 |
+
}
|
| 125 |
+
return this;
|
| 126 |
+
}
|
| 127 |
+
destroy(): void {
|
| 128 |
+
this.destroyed = true;
|
| 129 |
+
if (this.salt !== EMPTY_SALT) {
|
| 130 |
+
clean(this.salt, this.constants);
|
| 131 |
+
}
|
| 132 |
+
}
|
| 133 |
+
_cloneInto(to?: T): T {
|
| 134 |
+
to ||= new (this.constructor as any)() as T;
|
| 135 |
+
to.set(...this.get());
|
| 136 |
+
const { buffer, length, finished, destroyed, constants, salt, pos } = this;
|
| 137 |
+
to.buffer.set(buffer);
|
| 138 |
+
to.constants = constants.slice();
|
| 139 |
+
to.destroyed = destroyed;
|
| 140 |
+
to.finished = finished;
|
| 141 |
+
to.length = length;
|
| 142 |
+
to.pos = pos;
|
| 143 |
+
to.salt = salt.slice();
|
| 144 |
+
return to;
|
| 145 |
+
}
|
| 146 |
+
clone(): T {
|
| 147 |
+
return this._cloneInto();
|
| 148 |
+
}
|
| 149 |
+
digestInto(out: Uint8Array): void {
|
| 150 |
+
aexists(this);
|
| 151 |
+
aoutput(out, this);
|
| 152 |
+
this.finished = true;
|
| 153 |
+
// Padding
|
| 154 |
+
const { buffer, blockLen, counterLen, lengthFlag, view } = this;
|
| 155 |
+
clean(buffer.subarray(this.pos)); // clean buf
|
| 156 |
+
const counter = BigInt((this.length + this.pos) * 8);
|
| 157 |
+
const counterPos = blockLen - counterLen - 1;
|
| 158 |
+
buffer[this.pos] |= 0b1000_0000; // End block flag
|
| 159 |
+
this.length += this.pos; // add unwritten length
|
| 160 |
+
// Not enough in buffer for length: write what we have.
|
| 161 |
+
if (this.pos > counterPos) {
|
| 162 |
+
this.compress(view, 0);
|
| 163 |
+
clean(buffer);
|
| 164 |
+
this.pos = 0;
|
| 165 |
+
}
|
| 166 |
+
// Difference with md: here we have lengthFlag!
|
| 167 |
+
buffer[counterPos] |= lengthFlag; // Length flag
|
| 168 |
+
// We always set 8 byte length flag. Because length will overflow significantly sooner.
|
| 169 |
+
setBigUint64(view, blockLen - 8, counter, false);
|
| 170 |
+
this.compress(view, 0, this.pos !== 0); // don't add length if length is not empty block?
|
| 171 |
+
// Write output
|
| 172 |
+
clean(buffer);
|
| 173 |
+
const v = createView(out);
|
| 174 |
+
const state = this.get();
|
| 175 |
+
for (let i = 0; i < this.outputLen / 4; ++i) v.setUint32(i * 4, state[i]);
|
| 176 |
+
}
|
| 177 |
+
digest(): Uint8Array {
|
| 178 |
+
const { buffer, outputLen } = this;
|
| 179 |
+
this.digestInto(buffer);
|
| 180 |
+
const res = buffer.slice(0, outputLen);
|
| 181 |
+
this.destroy();
|
| 182 |
+
return res;
|
| 183 |
+
}
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
// Constants
|
| 187 |
+
const B64C = /* @__PURE__ */ Uint32Array.from([
|
| 188 |
+
0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89,
|
| 189 |
+
0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917,
|
| 190 |
+
0x9216d5d9, 0x8979fb1b, 0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96,
|
| 191 |
+
0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, 0x636920d8, 0x71574e69,
|
| 192 |
+
]);
|
| 193 |
+
// first half of C512
|
| 194 |
+
const B32C = B64C.slice(0, 16);
|
| 195 |
+
|
| 196 |
+
const B256_IV = SHA256_IV.slice();
|
| 197 |
+
const B224_IV = SHA224_IV.slice();
|
| 198 |
+
const B384_IV = SHA384_IV.slice();
|
| 199 |
+
const B512_IV = SHA512_IV.slice();
|
| 200 |
+
|
| 201 |
+
function generateTBL256() {
|
| 202 |
+
const TBL = [];
|
| 203 |
+
for (let i = 0, j = 0; i < 14; i++, j += 16) {
|
| 204 |
+
for (let offset = 1; offset < 16; offset += 2) {
|
| 205 |
+
TBL.push(B32C[BSIGMA[j + offset]]);
|
| 206 |
+
TBL.push(B32C[BSIGMA[j + offset - 1]]);
|
| 207 |
+
}
|
| 208 |
+
}
|
| 209 |
+
return new Uint32Array(TBL);
|
| 210 |
+
}
|
| 211 |
+
const TBL256 = /* @__PURE__ */ generateTBL256(); // C256[SIGMA[X]] precompute
|
| 212 |
+
|
| 213 |
+
// Reusable temporary buffer
|
| 214 |
+
const BLAKE256_W = /* @__PURE__ */ new Uint32Array(16);
|
| 215 |
+
|
| 216 |
+
class Blake1_32 extends BLAKE1<Blake1_32> {
|
| 217 |
+
private v0: number;
|
| 218 |
+
private v1: number;
|
| 219 |
+
private v2: number;
|
| 220 |
+
private v3: number;
|
| 221 |
+
private v4: number;
|
| 222 |
+
private v5: number;
|
| 223 |
+
private v6: number;
|
| 224 |
+
private v7: number;
|
| 225 |
+
constructor(outputLen: number, IV: Uint32Array, lengthFlag: number, opts: BlakeOpts = {}) {
|
| 226 |
+
super(64, outputLen, lengthFlag, 8, 4, B32C, opts);
|
| 227 |
+
this.v0 = IV[0] | 0;
|
| 228 |
+
this.v1 = IV[1] | 0;
|
| 229 |
+
this.v2 = IV[2] | 0;
|
| 230 |
+
this.v3 = IV[3] | 0;
|
| 231 |
+
this.v4 = IV[4] | 0;
|
| 232 |
+
this.v5 = IV[5] | 0;
|
| 233 |
+
this.v6 = IV[6] | 0;
|
| 234 |
+
this.v7 = IV[7] | 0;
|
| 235 |
+
}
|
| 236 |
+
protected get(): [number, number, number, number, number, number, number, number] {
|
| 237 |
+
const { v0, v1, v2, v3, v4, v5, v6, v7 } = this;
|
| 238 |
+
return [v0, v1, v2, v3, v4, v5, v6, v7];
|
| 239 |
+
}
|
| 240 |
+
// prettier-ignore
|
| 241 |
+
protected set(
|
| 242 |
+
v0: number, v1: number, v2: number, v3: number, v4: number, v5: number, v6: number, v7: number
|
| 243 |
+
): void {
|
| 244 |
+
this.v0 = v0 | 0;
|
| 245 |
+
this.v1 = v1 | 0;
|
| 246 |
+
this.v2 = v2 | 0;
|
| 247 |
+
this.v3 = v3 | 0;
|
| 248 |
+
this.v4 = v4 | 0;
|
| 249 |
+
this.v5 = v5 | 0;
|
| 250 |
+
this.v6 = v6 | 0;
|
| 251 |
+
this.v7 = v7 | 0;
|
| 252 |
+
}
|
| 253 |
+
destroy(): void {
|
| 254 |
+
super.destroy();
|
| 255 |
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
| 256 |
+
}
|
| 257 |
+
compress(view: DataView, offset: number, withLength = true): void {
|
| 258 |
+
for (let i = 0; i < 16; i++, offset += 4) BLAKE256_W[i] = view.getUint32(offset, false);
|
| 259 |
+
// NOTE: we cannot re-use compress from blake2s, since there is additional xor over u256[SIGMA[e]]
|
| 260 |
+
let v00 = this.v0 | 0;
|
| 261 |
+
let v01 = this.v1 | 0;
|
| 262 |
+
let v02 = this.v2 | 0;
|
| 263 |
+
let v03 = this.v3 | 0;
|
| 264 |
+
let v04 = this.v4 | 0;
|
| 265 |
+
let v05 = this.v5 | 0;
|
| 266 |
+
let v06 = this.v6 | 0;
|
| 267 |
+
let v07 = this.v7 | 0;
|
| 268 |
+
let v08 = this.constants[0] | 0;
|
| 269 |
+
let v09 = this.constants[1] | 0;
|
| 270 |
+
let v10 = this.constants[2] | 0;
|
| 271 |
+
let v11 = this.constants[3] | 0;
|
| 272 |
+
const { h, l } = u64.fromBig(BigInt(withLength ? this.length * 8 : 0));
|
| 273 |
+
let v12 = (this.constants[4] ^ l) >>> 0;
|
| 274 |
+
let v13 = (this.constants[5] ^ l) >>> 0;
|
| 275 |
+
let v14 = (this.constants[6] ^ h) >>> 0;
|
| 276 |
+
let v15 = (this.constants[7] ^ h) >>> 0;
|
| 277 |
+
// prettier-ignore
|
| 278 |
+
for (let i = 0, k = 0, j = 0; i < 14; i++) {
|
| 279 |
+
({ a: v00, b: v04, c: v08, d: v12 } = G1s(v00, v04, v08, v12, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
| 280 |
+
({ a: v00, b: v04, c: v08, d: v12 } = G2s(v00, v04, v08, v12, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
| 281 |
+
({ a: v01, b: v05, c: v09, d: v13 } = G1s(v01, v05, v09, v13, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
| 282 |
+
({ a: v01, b: v05, c: v09, d: v13 } = G2s(v01, v05, v09, v13, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
| 283 |
+
({ a: v02, b: v06, c: v10, d: v14 } = G1s(v02, v06, v10, v14, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
| 284 |
+
({ a: v02, b: v06, c: v10, d: v14 } = G2s(v02, v06, v10, v14, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
| 285 |
+
({ a: v03, b: v07, c: v11, d: v15 } = G1s(v03, v07, v11, v15, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
| 286 |
+
({ a: v03, b: v07, c: v11, d: v15 } = G2s(v03, v07, v11, v15, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
| 287 |
+
({ a: v00, b: v05, c: v10, d: v15 } = G1s(v00, v05, v10, v15, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
| 288 |
+
({ a: v00, b: v05, c: v10, d: v15 } = G2s(v00, v05, v10, v15, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
| 289 |
+
({ a: v01, b: v06, c: v11, d: v12 } = G1s(v01, v06, v11, v12, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
| 290 |
+
({ a: v01, b: v06, c: v11, d: v12 } = G2s(v01, v06, v11, v12, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
| 291 |
+
({ a: v02, b: v07, c: v08, d: v13 } = G1s(v02, v07, v08, v13, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
| 292 |
+
({ a: v02, b: v07, c: v08, d: v13 } = G2s(v02, v07, v08, v13, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
| 293 |
+
({ a: v03, b: v04, c: v09, d: v14 } = G1s(v03, v04, v09, v14, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
| 294 |
+
({ a: v03, b: v04, c: v09, d: v14 } = G2s(v03, v04, v09, v14, BLAKE256_W[BSIGMA[k++]] ^ TBL256[j++]));
|
| 295 |
+
}
|
| 296 |
+
this.v0 = (this.v0 ^ v00 ^ v08 ^ this.salt[0]) >>> 0;
|
| 297 |
+
this.v1 = (this.v1 ^ v01 ^ v09 ^ this.salt[1]) >>> 0;
|
| 298 |
+
this.v2 = (this.v2 ^ v02 ^ v10 ^ this.salt[2]) >>> 0;
|
| 299 |
+
this.v3 = (this.v3 ^ v03 ^ v11 ^ this.salt[3]) >>> 0;
|
| 300 |
+
this.v4 = (this.v4 ^ v04 ^ v12 ^ this.salt[0]) >>> 0;
|
| 301 |
+
this.v5 = (this.v5 ^ v05 ^ v13 ^ this.salt[1]) >>> 0;
|
| 302 |
+
this.v6 = (this.v6 ^ v06 ^ v14 ^ this.salt[2]) >>> 0;
|
| 303 |
+
this.v7 = (this.v7 ^ v07 ^ v15 ^ this.salt[3]) >>> 0;
|
| 304 |
+
clean(BLAKE256_W);
|
| 305 |
+
}
|
| 306 |
+
}
|
| 307 |
+
|
| 308 |
+
const BBUF = /* @__PURE__ */ new Uint32Array(32);
|
| 309 |
+
const BLAKE512_W = /* @__PURE__ */ new Uint32Array(32);
|
| 310 |
+
|
| 311 |
+
function generateTBL512() {
|
| 312 |
+
const TBL = [];
|
| 313 |
+
for (let r = 0, k = 0; r < 16; r++, k += 16) {
|
| 314 |
+
for (let offset = 1; offset < 16; offset += 2) {
|
| 315 |
+
TBL.push(B64C[BSIGMA[k + offset] * 2 + 0]);
|
| 316 |
+
TBL.push(B64C[BSIGMA[k + offset] * 2 + 1]);
|
| 317 |
+
TBL.push(B64C[BSIGMA[k + offset - 1] * 2 + 0]);
|
| 318 |
+
TBL.push(B64C[BSIGMA[k + offset - 1] * 2 + 1]);
|
| 319 |
+
}
|
| 320 |
+
}
|
| 321 |
+
return new Uint32Array(TBL);
|
| 322 |
+
}
|
| 323 |
+
const TBL512 = /* @__PURE__ */ generateTBL512(); // C512[SIGMA[X]] precompute
|
| 324 |
+
|
| 325 |
+
// Mixing function G splitted in two halfs
|
| 326 |
+
function G1b(a: number, b: number, c: number, d: number, msg: Uint32Array, k: number) {
|
| 327 |
+
const Xpos = 2 * BSIGMA[k];
|
| 328 |
+
const Xl = msg[Xpos + 1] ^ TBL512[k * 2 + 1], Xh = msg[Xpos] ^ TBL512[k * 2]; // prettier-ignore
|
| 329 |
+
let Al = BBUF[2 * a + 1], Ah = BBUF[2 * a]; // prettier-ignore
|
| 330 |
+
let Bl = BBUF[2 * b + 1], Bh = BBUF[2 * b]; // prettier-ignore
|
| 331 |
+
let Cl = BBUF[2 * c + 1], Ch = BBUF[2 * c]; // prettier-ignore
|
| 332 |
+
let Dl = BBUF[2 * d + 1], Dh = BBUF[2 * d]; // prettier-ignore
|
| 333 |
+
// v[a] = (v[a] + v[b] + x) | 0;
|
| 334 |
+
let ll = u64.add3L(Al, Bl, Xl);
|
| 335 |
+
Ah = u64.add3H(ll, Ah, Bh, Xh) >>> 0;
|
| 336 |
+
Al = (ll | 0) >>> 0;
|
| 337 |
+
// v[d] = rotr(v[d] ^ v[a], 32)
|
| 338 |
+
({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
|
| 339 |
+
({ Dh, Dl } = { Dh: u64.rotr32H(Dh, Dl), Dl: u64.rotr32L(Dh, Dl) });
|
| 340 |
+
// v[c] = (v[c] + v[d]) | 0;
|
| 341 |
+
({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
|
| 342 |
+
// v[b] = rotr(v[b] ^ v[c], 25)
|
| 343 |
+
({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
|
| 344 |
+
({ Bh, Bl } = { Bh: u64.rotrSH(Bh, Bl, 25), Bl: u64.rotrSL(Bh, Bl, 25) });
|
| 345 |
+
(BBUF[2 * a + 1] = Al), (BBUF[2 * a] = Ah);
|
| 346 |
+
(BBUF[2 * b + 1] = Bl), (BBUF[2 * b] = Bh);
|
| 347 |
+
(BBUF[2 * c + 1] = Cl), (BBUF[2 * c] = Ch);
|
| 348 |
+
(BBUF[2 * d + 1] = Dl), (BBUF[2 * d] = Dh);
|
| 349 |
+
}
|
| 350 |
+
|
| 351 |
+
function G2b(a: number, b: number, c: number, d: number, msg: Uint32Array, k: number) {
|
| 352 |
+
const Xpos = 2 * BSIGMA[k];
|
| 353 |
+
const Xl = msg[Xpos + 1] ^ TBL512[k * 2 + 1], Xh = msg[Xpos] ^ TBL512[k * 2]; // prettier-ignore
|
| 354 |
+
let Al = BBUF[2 * a + 1], Ah = BBUF[2 * a]; // prettier-ignore
|
| 355 |
+
let Bl = BBUF[2 * b + 1], Bh = BBUF[2 * b]; // prettier-ignore
|
| 356 |
+
let Cl = BBUF[2 * c + 1], Ch = BBUF[2 * c]; // prettier-ignore
|
| 357 |
+
let Dl = BBUF[2 * d + 1], Dh = BBUF[2 * d]; // prettier-ignore
|
| 358 |
+
// v[a] = (v[a] + v[b] + x) | 0;
|
| 359 |
+
let ll = u64.add3L(Al, Bl, Xl);
|
| 360 |
+
Ah = u64.add3H(ll, Ah, Bh, Xh);
|
| 361 |
+
Al = ll | 0;
|
| 362 |
+
// v[d] = rotr(v[d] ^ v[a], 16)
|
| 363 |
+
({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
|
| 364 |
+
({ Dh, Dl } = { Dh: u64.rotrSH(Dh, Dl, 16), Dl: u64.rotrSL(Dh, Dl, 16) });
|
| 365 |
+
// v[c] = (v[c] + v[d]) | 0;
|
| 366 |
+
({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
|
| 367 |
+
// v[b] = rotr(v[b] ^ v[c], 11)
|
| 368 |
+
({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
|
| 369 |
+
({ Bh, Bl } = { Bh: u64.rotrSH(Bh, Bl, 11), Bl: u64.rotrSL(Bh, Bl, 11) });
|
| 370 |
+
(BBUF[2 * a + 1] = Al), (BBUF[2 * a] = Ah);
|
| 371 |
+
(BBUF[2 * b + 1] = Bl), (BBUF[2 * b] = Bh);
|
| 372 |
+
(BBUF[2 * c + 1] = Cl), (BBUF[2 * c] = Ch);
|
| 373 |
+
(BBUF[2 * d + 1] = Dl), (BBUF[2 * d] = Dh);
|
| 374 |
+
}
|
| 375 |
+
|
| 376 |
+
class Blake1_64 extends BLAKE1<Blake1_64> {
|
| 377 |
+
private v0l: number;
|
| 378 |
+
private v0h: number;
|
| 379 |
+
private v1l: number;
|
| 380 |
+
private v1h: number;
|
| 381 |
+
private v2l: number;
|
| 382 |
+
private v2h: number;
|
| 383 |
+
private v3l: number;
|
| 384 |
+
private v3h: number;
|
| 385 |
+
private v4l: number;
|
| 386 |
+
private v4h: number;
|
| 387 |
+
private v5l: number;
|
| 388 |
+
private v5h: number;
|
| 389 |
+
private v6l: number;
|
| 390 |
+
private v6h: number;
|
| 391 |
+
private v7l: number;
|
| 392 |
+
private v7h: number;
|
| 393 |
+
constructor(outputLen: number, IV: Uint32Array, lengthFlag: number, opts: BlakeOpts = {}) {
|
| 394 |
+
super(128, outputLen, lengthFlag, 16, 8, B64C, opts);
|
| 395 |
+
this.v0l = IV[0] | 0;
|
| 396 |
+
this.v0h = IV[1] | 0;
|
| 397 |
+
this.v1l = IV[2] | 0;
|
| 398 |
+
this.v1h = IV[3] | 0;
|
| 399 |
+
this.v2l = IV[4] | 0;
|
| 400 |
+
this.v2h = IV[5] | 0;
|
| 401 |
+
this.v3l = IV[6] | 0;
|
| 402 |
+
this.v3h = IV[7] | 0;
|
| 403 |
+
this.v4l = IV[8] | 0;
|
| 404 |
+
this.v4h = IV[9] | 0;
|
| 405 |
+
this.v5l = IV[10] | 0;
|
| 406 |
+
this.v5h = IV[11] | 0;
|
| 407 |
+
this.v6l = IV[12] | 0;
|
| 408 |
+
this.v6h = IV[13] | 0;
|
| 409 |
+
this.v7l = IV[14] | 0;
|
| 410 |
+
this.v7h = IV[15] | 0;
|
| 411 |
+
}
|
| 412 |
+
// prettier-ignore
|
| 413 |
+
protected get(): [
|
| 414 |
+
number, number, number, number, number, number, number, number,
|
| 415 |
+
number, number, number, number, number, number, number, number
|
| 416 |
+
] {
|
| 417 |
+
let { v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h } = this;
|
| 418 |
+
return [v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h];
|
| 419 |
+
}
|
| 420 |
+
// prettier-ignore
|
| 421 |
+
protected set(
|
| 422 |
+
v0l: number, v0h: number, v1l: number, v1h: number,
|
| 423 |
+
v2l: number, v2h: number, v3l: number, v3h: number,
|
| 424 |
+
v4l: number, v4h: number, v5l: number, v5h: number,
|
| 425 |
+
v6l: number, v6h: number, v7l: number, v7h: number
|
| 426 |
+
): void {
|
| 427 |
+
this.v0l = v0l | 0;
|
| 428 |
+
this.v0h = v0h | 0;
|
| 429 |
+
this.v1l = v1l | 0;
|
| 430 |
+
this.v1h = v1h | 0;
|
| 431 |
+
this.v2l = v2l | 0;
|
| 432 |
+
this.v2h = v2h | 0;
|
| 433 |
+
this.v3l = v3l | 0;
|
| 434 |
+
this.v3h = v3h | 0;
|
| 435 |
+
this.v4l = v4l | 0;
|
| 436 |
+
this.v4h = v4h | 0;
|
| 437 |
+
this.v5l = v5l | 0;
|
| 438 |
+
this.v5h = v5h | 0;
|
| 439 |
+
this.v6l = v6l | 0;
|
| 440 |
+
this.v6h = v6h | 0;
|
| 441 |
+
this.v7l = v7l | 0;
|
| 442 |
+
this.v7h = v7h | 0;
|
| 443 |
+
}
|
| 444 |
+
destroy(): void {
|
| 445 |
+
super.destroy();
|
| 446 |
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
| 447 |
+
}
|
| 448 |
+
compress(view: DataView, offset: number, withLength = true): void {
|
| 449 |
+
for (let i = 0; i < 32; i++, offset += 4) BLAKE512_W[i] = view.getUint32(offset, false);
|
| 450 |
+
|
| 451 |
+
this.get().forEach((v, i) => (BBUF[i] = v)); // First half from state.
|
| 452 |
+
BBUF.set(this.constants.subarray(0, 16), 16);
|
| 453 |
+
if (withLength) {
|
| 454 |
+
const { h, l } = u64.fromBig(BigInt(this.length * 8));
|
| 455 |
+
BBUF[24] = (BBUF[24] ^ h) >>> 0;
|
| 456 |
+
BBUF[25] = (BBUF[25] ^ l) >>> 0;
|
| 457 |
+
BBUF[26] = (BBUF[26] ^ h) >>> 0;
|
| 458 |
+
BBUF[27] = (BBUF[27] ^ l) >>> 0;
|
| 459 |
+
}
|
| 460 |
+
for (let i = 0, k = 0; i < 16; i++) {
|
| 461 |
+
G1b(0, 4, 8, 12, BLAKE512_W, k++);
|
| 462 |
+
G2b(0, 4, 8, 12, BLAKE512_W, k++);
|
| 463 |
+
G1b(1, 5, 9, 13, BLAKE512_W, k++);
|
| 464 |
+
G2b(1, 5, 9, 13, BLAKE512_W, k++);
|
| 465 |
+
G1b(2, 6, 10, 14, BLAKE512_W, k++);
|
| 466 |
+
G2b(2, 6, 10, 14, BLAKE512_W, k++);
|
| 467 |
+
G1b(3, 7, 11, 15, BLAKE512_W, k++);
|
| 468 |
+
G2b(3, 7, 11, 15, BLAKE512_W, k++);
|
| 469 |
+
|
| 470 |
+
G1b(0, 5, 10, 15, BLAKE512_W, k++);
|
| 471 |
+
G2b(0, 5, 10, 15, BLAKE512_W, k++);
|
| 472 |
+
G1b(1, 6, 11, 12, BLAKE512_W, k++);
|
| 473 |
+
G2b(1, 6, 11, 12, BLAKE512_W, k++);
|
| 474 |
+
G1b(2, 7, 8, 13, BLAKE512_W, k++);
|
| 475 |
+
G2b(2, 7, 8, 13, BLAKE512_W, k++);
|
| 476 |
+
G1b(3, 4, 9, 14, BLAKE512_W, k++);
|
| 477 |
+
G2b(3, 4, 9, 14, BLAKE512_W, k++);
|
| 478 |
+
}
|
| 479 |
+
this.v0l ^= BBUF[0] ^ BBUF[16] ^ this.salt[0];
|
| 480 |
+
this.v0h ^= BBUF[1] ^ BBUF[17] ^ this.salt[1];
|
| 481 |
+
this.v1l ^= BBUF[2] ^ BBUF[18] ^ this.salt[2];
|
| 482 |
+
this.v1h ^= BBUF[3] ^ BBUF[19] ^ this.salt[3];
|
| 483 |
+
this.v2l ^= BBUF[4] ^ BBUF[20] ^ this.salt[4];
|
| 484 |
+
this.v2h ^= BBUF[5] ^ BBUF[21] ^ this.salt[5];
|
| 485 |
+
this.v3l ^= BBUF[6] ^ BBUF[22] ^ this.salt[6];
|
| 486 |
+
this.v3h ^= BBUF[7] ^ BBUF[23] ^ this.salt[7];
|
| 487 |
+
this.v4l ^= BBUF[8] ^ BBUF[24] ^ this.salt[0];
|
| 488 |
+
this.v4h ^= BBUF[9] ^ BBUF[25] ^ this.salt[1];
|
| 489 |
+
this.v5l ^= BBUF[10] ^ BBUF[26] ^ this.salt[2];
|
| 490 |
+
this.v5h ^= BBUF[11] ^ BBUF[27] ^ this.salt[3];
|
| 491 |
+
this.v6l ^= BBUF[12] ^ BBUF[28] ^ this.salt[4];
|
| 492 |
+
this.v6h ^= BBUF[13] ^ BBUF[29] ^ this.salt[5];
|
| 493 |
+
this.v7l ^= BBUF[14] ^ BBUF[30] ^ this.salt[6];
|
| 494 |
+
this.v7h ^= BBUF[15] ^ BBUF[31] ^ this.salt[7];
|
| 495 |
+
clean(BBUF, BLAKE512_W);
|
| 496 |
+
}
|
| 497 |
+
}
|
| 498 |
+
|
| 499 |
+
export class BLAKE224 extends Blake1_32 {
|
| 500 |
+
constructor(opts: BlakeOpts = {}) {
|
| 501 |
+
super(28, B224_IV, 0b0000_0000, opts);
|
| 502 |
+
}
|
| 503 |
+
}
|
| 504 |
+
export class BLAKE256 extends Blake1_32 {
|
| 505 |
+
constructor(opts: BlakeOpts = {}) {
|
| 506 |
+
super(32, B256_IV, 0b0000_0001, opts);
|
| 507 |
+
}
|
| 508 |
+
}
|
| 509 |
+
export class BLAKE384 extends Blake1_64 {
|
| 510 |
+
constructor(opts: BlakeOpts = {}) {
|
| 511 |
+
super(48, B384_IV, 0b0000_0000, opts);
|
| 512 |
+
}
|
| 513 |
+
}
|
| 514 |
+
export class BLAKE512 extends Blake1_64 {
|
| 515 |
+
constructor(opts: BlakeOpts = {}) {
|
| 516 |
+
super(64, B512_IV, 0b0000_0001, opts);
|
| 517 |
+
}
|
| 518 |
+
}
|
| 519 |
+
/** blake1-224 hash function */
|
| 520 |
+
export const blake224: CHashO = /* @__PURE__ */ createOptHasher<BLAKE224, BlakeOpts>(
|
| 521 |
+
(opts) => new BLAKE224(opts)
|
| 522 |
+
);
|
| 523 |
+
/** blake1-256 hash function */
|
| 524 |
+
export const blake256: CHashO = /* @__PURE__ */ createOptHasher<BLAKE256, BlakeOpts>(
|
| 525 |
+
(opts) => new BLAKE256(opts)
|
| 526 |
+
);
|
| 527 |
+
/** blake1-384 hash function */
|
| 528 |
+
export const blake384: CHashO = /* @__PURE__ */ createOptHasher<BLAKE512, BlakeOpts>(
|
| 529 |
+
(opts) => new BLAKE384(opts)
|
| 530 |
+
);
|
| 531 |
+
/** blake1-512 hash function */
|
| 532 |
+
export const blake512: CHashO = /* @__PURE__ */ createOptHasher<BLAKE512, BlakeOpts>(
|
| 533 |
+
(opts) => new BLAKE512(opts)
|
| 534 |
+
);
|
node_modules/@noble/hashes/src/blake2.ts
ADDED
|
@@ -0,0 +1,486 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* blake2b (64-bit) & blake2s (8 to 32-bit) hash functions.
|
| 3 |
+
* b could have been faster, but there is no fast u64 in js, so s is 1.5x faster.
|
| 4 |
+
* @module
|
| 5 |
+
*/
|
| 6 |
+
import { BSIGMA, G1s, G2s } from './_blake.ts';
|
| 7 |
+
import { SHA256_IV } from './_md.ts';
|
| 8 |
+
import * as u64 from './_u64.ts';
|
| 9 |
+
// prettier-ignore
|
| 10 |
+
import {
|
| 11 |
+
abytes, aexists, anumber, aoutput,
|
| 12 |
+
clean, createOptHasher, Hash, swap32IfBE, swap8IfBE, toBytes, u32,
|
| 13 |
+
type CHashO, type Input
|
| 14 |
+
} from './utils.ts';
|
| 15 |
+
|
| 16 |
+
/** Blake hash options. dkLen is output length. key is used in MAC mode. salt is used in KDF mode. */
|
| 17 |
+
export type Blake2Opts = {
|
| 18 |
+
dkLen?: number;
|
| 19 |
+
key?: Input;
|
| 20 |
+
salt?: Input;
|
| 21 |
+
personalization?: Input;
|
| 22 |
+
};
|
| 23 |
+
|
| 24 |
+
// Same as SHA512_IV, but swapped endianness: LE instead of BE. iv[1] is iv[0], etc.
|
| 25 |
+
const B2B_IV = /* @__PURE__ */ Uint32Array.from([
|
| 26 |
+
0xf3bcc908, 0x6a09e667, 0x84caa73b, 0xbb67ae85, 0xfe94f82b, 0x3c6ef372, 0x5f1d36f1, 0xa54ff53a,
|
| 27 |
+
0xade682d1, 0x510e527f, 0x2b3e6c1f, 0x9b05688c, 0xfb41bd6b, 0x1f83d9ab, 0x137e2179, 0x5be0cd19,
|
| 28 |
+
]);
|
| 29 |
+
// Temporary buffer
|
| 30 |
+
const BBUF = /* @__PURE__ */ new Uint32Array(32);
|
| 31 |
+
|
| 32 |
+
// Mixing function G splitted in two halfs
|
| 33 |
+
function G1b(a: number, b: number, c: number, d: number, msg: Uint32Array, x: number) {
|
| 34 |
+
// NOTE: V is LE here
|
| 35 |
+
const Xl = msg[x], Xh = msg[x + 1]; // prettier-ignore
|
| 36 |
+
let Al = BBUF[2 * a], Ah = BBUF[2 * a + 1]; // prettier-ignore
|
| 37 |
+
let Bl = BBUF[2 * b], Bh = BBUF[2 * b + 1]; // prettier-ignore
|
| 38 |
+
let Cl = BBUF[2 * c], Ch = BBUF[2 * c + 1]; // prettier-ignore
|
| 39 |
+
let Dl = BBUF[2 * d], Dh = BBUF[2 * d + 1]; // prettier-ignore
|
| 40 |
+
// v[a] = (v[a] + v[b] + x) | 0;
|
| 41 |
+
let ll = u64.add3L(Al, Bl, Xl);
|
| 42 |
+
Ah = u64.add3H(ll, Ah, Bh, Xh);
|
| 43 |
+
Al = ll | 0;
|
| 44 |
+
// v[d] = rotr(v[d] ^ v[a], 32)
|
| 45 |
+
({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
|
| 46 |
+
({ Dh, Dl } = { Dh: u64.rotr32H(Dh, Dl), Dl: u64.rotr32L(Dh, Dl) });
|
| 47 |
+
// v[c] = (v[c] + v[d]) | 0;
|
| 48 |
+
({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
|
| 49 |
+
// v[b] = rotr(v[b] ^ v[c], 24)
|
| 50 |
+
({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
|
| 51 |
+
({ Bh, Bl } = { Bh: u64.rotrSH(Bh, Bl, 24), Bl: u64.rotrSL(Bh, Bl, 24) });
|
| 52 |
+
(BBUF[2 * a] = Al), (BBUF[2 * a + 1] = Ah);
|
| 53 |
+
(BBUF[2 * b] = Bl), (BBUF[2 * b + 1] = Bh);
|
| 54 |
+
(BBUF[2 * c] = Cl), (BBUF[2 * c + 1] = Ch);
|
| 55 |
+
(BBUF[2 * d] = Dl), (BBUF[2 * d + 1] = Dh);
|
| 56 |
+
}
|
| 57 |
+
|
| 58 |
+
function G2b(a: number, b: number, c: number, d: number, msg: Uint32Array, x: number) {
|
| 59 |
+
// NOTE: V is LE here
|
| 60 |
+
const Xl = msg[x], Xh = msg[x + 1]; // prettier-ignore
|
| 61 |
+
let Al = BBUF[2 * a], Ah = BBUF[2 * a + 1]; // prettier-ignore
|
| 62 |
+
let Bl = BBUF[2 * b], Bh = BBUF[2 * b + 1]; // prettier-ignore
|
| 63 |
+
let Cl = BBUF[2 * c], Ch = BBUF[2 * c + 1]; // prettier-ignore
|
| 64 |
+
let Dl = BBUF[2 * d], Dh = BBUF[2 * d + 1]; // prettier-ignore
|
| 65 |
+
// v[a] = (v[a] + v[b] + x) | 0;
|
| 66 |
+
let ll = u64.add3L(Al, Bl, Xl);
|
| 67 |
+
Ah = u64.add3H(ll, Ah, Bh, Xh);
|
| 68 |
+
Al = ll | 0;
|
| 69 |
+
// v[d] = rotr(v[d] ^ v[a], 16)
|
| 70 |
+
({ Dh, Dl } = { Dh: Dh ^ Ah, Dl: Dl ^ Al });
|
| 71 |
+
({ Dh, Dl } = { Dh: u64.rotrSH(Dh, Dl, 16), Dl: u64.rotrSL(Dh, Dl, 16) });
|
| 72 |
+
// v[c] = (v[c] + v[d]) | 0;
|
| 73 |
+
({ h: Ch, l: Cl } = u64.add(Ch, Cl, Dh, Dl));
|
| 74 |
+
// v[b] = rotr(v[b] ^ v[c], 63)
|
| 75 |
+
({ Bh, Bl } = { Bh: Bh ^ Ch, Bl: Bl ^ Cl });
|
| 76 |
+
({ Bh, Bl } = { Bh: u64.rotrBH(Bh, Bl, 63), Bl: u64.rotrBL(Bh, Bl, 63) });
|
| 77 |
+
(BBUF[2 * a] = Al), (BBUF[2 * a + 1] = Ah);
|
| 78 |
+
(BBUF[2 * b] = Bl), (BBUF[2 * b + 1] = Bh);
|
| 79 |
+
(BBUF[2 * c] = Cl), (BBUF[2 * c + 1] = Ch);
|
| 80 |
+
(BBUF[2 * d] = Dl), (BBUF[2 * d + 1] = Dh);
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
function checkBlake2Opts(
|
| 84 |
+
outputLen: number,
|
| 85 |
+
opts: Blake2Opts | undefined = {},
|
| 86 |
+
keyLen: number,
|
| 87 |
+
saltLen: number,
|
| 88 |
+
persLen: number
|
| 89 |
+
) {
|
| 90 |
+
anumber(keyLen);
|
| 91 |
+
if (outputLen < 0 || outputLen > keyLen) throw new Error('outputLen bigger than keyLen');
|
| 92 |
+
const { key, salt, personalization } = opts;
|
| 93 |
+
if (key !== undefined && (key.length < 1 || key.length > keyLen))
|
| 94 |
+
throw new Error('key length must be undefined or 1..' + keyLen);
|
| 95 |
+
if (salt !== undefined && salt.length !== saltLen)
|
| 96 |
+
throw new Error('salt must be undefined or ' + saltLen);
|
| 97 |
+
if (personalization !== undefined && personalization.length !== persLen)
|
| 98 |
+
throw new Error('personalization must be undefined or ' + persLen);
|
| 99 |
+
}
|
| 100 |
+
|
| 101 |
+
/** Class, from which others are subclassed. */
|
| 102 |
+
export abstract class BLAKE2<T extends BLAKE2<T>> extends Hash<T> {
|
| 103 |
+
protected abstract compress(msg: Uint32Array, offset: number, isLast: boolean): void;
|
| 104 |
+
protected abstract get(): number[];
|
| 105 |
+
protected abstract set(...args: number[]): void;
|
| 106 |
+
abstract destroy(): void;
|
| 107 |
+
protected buffer: Uint8Array;
|
| 108 |
+
protected buffer32: Uint32Array;
|
| 109 |
+
protected finished = false;
|
| 110 |
+
protected destroyed = false;
|
| 111 |
+
protected length: number = 0;
|
| 112 |
+
protected pos: number = 0;
|
| 113 |
+
readonly blockLen: number;
|
| 114 |
+
readonly outputLen: number;
|
| 115 |
+
|
| 116 |
+
constructor(blockLen: number, outputLen: number) {
|
| 117 |
+
super();
|
| 118 |
+
anumber(blockLen);
|
| 119 |
+
anumber(outputLen);
|
| 120 |
+
this.blockLen = blockLen;
|
| 121 |
+
this.outputLen = outputLen;
|
| 122 |
+
this.buffer = new Uint8Array(blockLen);
|
| 123 |
+
this.buffer32 = u32(this.buffer);
|
| 124 |
+
}
|
| 125 |
+
update(data: Input): this {
|
| 126 |
+
aexists(this);
|
| 127 |
+
data = toBytes(data);
|
| 128 |
+
abytes(data);
|
| 129 |
+
// Main difference with other hashes: there is flag for last block,
|
| 130 |
+
// so we cannot process current block before we know that there
|
| 131 |
+
// is the next one. This significantly complicates logic and reduces ability
|
| 132 |
+
// to do zero-copy processing
|
| 133 |
+
const { blockLen, buffer, buffer32 } = this;
|
| 134 |
+
const len = data.length;
|
| 135 |
+
const offset = data.byteOffset;
|
| 136 |
+
const buf = data.buffer;
|
| 137 |
+
for (let pos = 0; pos < len; ) {
|
| 138 |
+
// If buffer is full and we still have input (don't process last block, same as blake2s)
|
| 139 |
+
if (this.pos === blockLen) {
|
| 140 |
+
swap32IfBE(buffer32);
|
| 141 |
+
this.compress(buffer32, 0, false);
|
| 142 |
+
swap32IfBE(buffer32);
|
| 143 |
+
this.pos = 0;
|
| 144 |
+
}
|
| 145 |
+
const take = Math.min(blockLen - this.pos, len - pos);
|
| 146 |
+
const dataOffset = offset + pos;
|
| 147 |
+
// full block && aligned to 4 bytes && not last in input
|
| 148 |
+
if (take === blockLen && !(dataOffset % 4) && pos + take < len) {
|
| 149 |
+
const data32 = new Uint32Array(buf, dataOffset, Math.floor((len - pos) / 4));
|
| 150 |
+
swap32IfBE(data32);
|
| 151 |
+
for (let pos32 = 0; pos + blockLen < len; pos32 += buffer32.length, pos += blockLen) {
|
| 152 |
+
this.length += blockLen;
|
| 153 |
+
this.compress(data32, pos32, false);
|
| 154 |
+
}
|
| 155 |
+
swap32IfBE(data32);
|
| 156 |
+
continue;
|
| 157 |
+
}
|
| 158 |
+
buffer.set(data.subarray(pos, pos + take), this.pos);
|
| 159 |
+
this.pos += take;
|
| 160 |
+
this.length += take;
|
| 161 |
+
pos += take;
|
| 162 |
+
}
|
| 163 |
+
return this;
|
| 164 |
+
}
|
| 165 |
+
digestInto(out: Uint8Array): void {
|
| 166 |
+
aexists(this);
|
| 167 |
+
aoutput(out, this);
|
| 168 |
+
const { pos, buffer32 } = this;
|
| 169 |
+
this.finished = true;
|
| 170 |
+
// Padding
|
| 171 |
+
clean(this.buffer.subarray(pos));
|
| 172 |
+
swap32IfBE(buffer32);
|
| 173 |
+
this.compress(buffer32, 0, true);
|
| 174 |
+
swap32IfBE(buffer32);
|
| 175 |
+
const out32 = u32(out);
|
| 176 |
+
this.get().forEach((v, i) => (out32[i] = swap8IfBE(v)));
|
| 177 |
+
}
|
| 178 |
+
digest(): Uint8Array {
|
| 179 |
+
const { buffer, outputLen } = this;
|
| 180 |
+
this.digestInto(buffer);
|
| 181 |
+
const res = buffer.slice(0, outputLen);
|
| 182 |
+
this.destroy();
|
| 183 |
+
return res;
|
| 184 |
+
}
|
| 185 |
+
_cloneInto(to?: T): T {
|
| 186 |
+
const { buffer, length, finished, destroyed, outputLen, pos } = this;
|
| 187 |
+
to ||= new (this.constructor as any)({ dkLen: outputLen }) as T;
|
| 188 |
+
to.set(...this.get());
|
| 189 |
+
to.buffer.set(buffer);
|
| 190 |
+
to.destroyed = destroyed;
|
| 191 |
+
to.finished = finished;
|
| 192 |
+
to.length = length;
|
| 193 |
+
to.pos = pos;
|
| 194 |
+
// @ts-ignore
|
| 195 |
+
to.outputLen = outputLen;
|
| 196 |
+
return to;
|
| 197 |
+
}
|
| 198 |
+
clone(): T {
|
| 199 |
+
return this._cloneInto();
|
| 200 |
+
}
|
| 201 |
+
}
|
| 202 |
+
|
| 203 |
+
export class BLAKE2b extends BLAKE2<BLAKE2b> {
|
| 204 |
+
// Same as SHA-512, but LE
|
| 205 |
+
private v0l = B2B_IV[0] | 0;
|
| 206 |
+
private v0h = B2B_IV[1] | 0;
|
| 207 |
+
private v1l = B2B_IV[2] | 0;
|
| 208 |
+
private v1h = B2B_IV[3] | 0;
|
| 209 |
+
private v2l = B2B_IV[4] | 0;
|
| 210 |
+
private v2h = B2B_IV[5] | 0;
|
| 211 |
+
private v3l = B2B_IV[6] | 0;
|
| 212 |
+
private v3h = B2B_IV[7] | 0;
|
| 213 |
+
private v4l = B2B_IV[8] | 0;
|
| 214 |
+
private v4h = B2B_IV[9] | 0;
|
| 215 |
+
private v5l = B2B_IV[10] | 0;
|
| 216 |
+
private v5h = B2B_IV[11] | 0;
|
| 217 |
+
private v6l = B2B_IV[12] | 0;
|
| 218 |
+
private v6h = B2B_IV[13] | 0;
|
| 219 |
+
private v7l = B2B_IV[14] | 0;
|
| 220 |
+
private v7h = B2B_IV[15] | 0;
|
| 221 |
+
|
| 222 |
+
constructor(opts: Blake2Opts = {}) {
|
| 223 |
+
const olen = opts.dkLen === undefined ? 64 : opts.dkLen;
|
| 224 |
+
super(128, olen);
|
| 225 |
+
checkBlake2Opts(olen, opts, 64, 16, 16);
|
| 226 |
+
let { key, personalization, salt } = opts;
|
| 227 |
+
let keyLength = 0;
|
| 228 |
+
if (key !== undefined) {
|
| 229 |
+
key = toBytes(key);
|
| 230 |
+
keyLength = key.length;
|
| 231 |
+
}
|
| 232 |
+
this.v0l ^= this.outputLen | (keyLength << 8) | (0x01 << 16) | (0x01 << 24);
|
| 233 |
+
if (salt !== undefined) {
|
| 234 |
+
salt = toBytes(salt);
|
| 235 |
+
const slt = u32(salt);
|
| 236 |
+
this.v4l ^= swap8IfBE(slt[0]);
|
| 237 |
+
this.v4h ^= swap8IfBE(slt[1]);
|
| 238 |
+
this.v5l ^= swap8IfBE(slt[2]);
|
| 239 |
+
this.v5h ^= swap8IfBE(slt[3]);
|
| 240 |
+
}
|
| 241 |
+
if (personalization !== undefined) {
|
| 242 |
+
personalization = toBytes(personalization);
|
| 243 |
+
const pers = u32(personalization);
|
| 244 |
+
this.v6l ^= swap8IfBE(pers[0]);
|
| 245 |
+
this.v6h ^= swap8IfBE(pers[1]);
|
| 246 |
+
this.v7l ^= swap8IfBE(pers[2]);
|
| 247 |
+
this.v7h ^= swap8IfBE(pers[3]);
|
| 248 |
+
}
|
| 249 |
+
if (key !== undefined) {
|
| 250 |
+
// Pad to blockLen and update
|
| 251 |
+
const tmp = new Uint8Array(this.blockLen);
|
| 252 |
+
tmp.set(key);
|
| 253 |
+
this.update(tmp);
|
| 254 |
+
}
|
| 255 |
+
}
|
| 256 |
+
// prettier-ignore
|
| 257 |
+
protected get(): [
|
| 258 |
+
number, number, number, number, number, number, number, number,
|
| 259 |
+
number, number, number, number, number, number, number, number
|
| 260 |
+
] {
|
| 261 |
+
let { v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h } = this;
|
| 262 |
+
return [v0l, v0h, v1l, v1h, v2l, v2h, v3l, v3h, v4l, v4h, v5l, v5h, v6l, v6h, v7l, v7h];
|
| 263 |
+
}
|
| 264 |
+
// prettier-ignore
|
| 265 |
+
protected set(
|
| 266 |
+
v0l: number, v0h: number, v1l: number, v1h: number,
|
| 267 |
+
v2l: number, v2h: number, v3l: number, v3h: number,
|
| 268 |
+
v4l: number, v4h: number, v5l: number, v5h: number,
|
| 269 |
+
v6l: number, v6h: number, v7l: number, v7h: number
|
| 270 |
+
): void {
|
| 271 |
+
this.v0l = v0l | 0;
|
| 272 |
+
this.v0h = v0h | 0;
|
| 273 |
+
this.v1l = v1l | 0;
|
| 274 |
+
this.v1h = v1h | 0;
|
| 275 |
+
this.v2l = v2l | 0;
|
| 276 |
+
this.v2h = v2h | 0;
|
| 277 |
+
this.v3l = v3l | 0;
|
| 278 |
+
this.v3h = v3h | 0;
|
| 279 |
+
this.v4l = v4l | 0;
|
| 280 |
+
this.v4h = v4h | 0;
|
| 281 |
+
this.v5l = v5l | 0;
|
| 282 |
+
this.v5h = v5h | 0;
|
| 283 |
+
this.v6l = v6l | 0;
|
| 284 |
+
this.v6h = v6h | 0;
|
| 285 |
+
this.v7l = v7l | 0;
|
| 286 |
+
this.v7h = v7h | 0;
|
| 287 |
+
}
|
| 288 |
+
protected compress(msg: Uint32Array, offset: number, isLast: boolean): void {
|
| 289 |
+
this.get().forEach((v, i) => (BBUF[i] = v)); // First half from state.
|
| 290 |
+
BBUF.set(B2B_IV, 16); // Second half from IV.
|
| 291 |
+
let { h, l } = u64.fromBig(BigInt(this.length));
|
| 292 |
+
BBUF[24] = B2B_IV[8] ^ l; // Low word of the offset.
|
| 293 |
+
BBUF[25] = B2B_IV[9] ^ h; // High word.
|
| 294 |
+
// Invert all bits for last block
|
| 295 |
+
if (isLast) {
|
| 296 |
+
BBUF[28] = ~BBUF[28];
|
| 297 |
+
BBUF[29] = ~BBUF[29];
|
| 298 |
+
}
|
| 299 |
+
let j = 0;
|
| 300 |
+
const s = BSIGMA;
|
| 301 |
+
for (let i = 0; i < 12; i++) {
|
| 302 |
+
G1b(0, 4, 8, 12, msg, offset + 2 * s[j++]);
|
| 303 |
+
G2b(0, 4, 8, 12, msg, offset + 2 * s[j++]);
|
| 304 |
+
G1b(1, 5, 9, 13, msg, offset + 2 * s[j++]);
|
| 305 |
+
G2b(1, 5, 9, 13, msg, offset + 2 * s[j++]);
|
| 306 |
+
G1b(2, 6, 10, 14, msg, offset + 2 * s[j++]);
|
| 307 |
+
G2b(2, 6, 10, 14, msg, offset + 2 * s[j++]);
|
| 308 |
+
G1b(3, 7, 11, 15, msg, offset + 2 * s[j++]);
|
| 309 |
+
G2b(3, 7, 11, 15, msg, offset + 2 * s[j++]);
|
| 310 |
+
|
| 311 |
+
G1b(0, 5, 10, 15, msg, offset + 2 * s[j++]);
|
| 312 |
+
G2b(0, 5, 10, 15, msg, offset + 2 * s[j++]);
|
| 313 |
+
G1b(1, 6, 11, 12, msg, offset + 2 * s[j++]);
|
| 314 |
+
G2b(1, 6, 11, 12, msg, offset + 2 * s[j++]);
|
| 315 |
+
G1b(2, 7, 8, 13, msg, offset + 2 * s[j++]);
|
| 316 |
+
G2b(2, 7, 8, 13, msg, offset + 2 * s[j++]);
|
| 317 |
+
G1b(3, 4, 9, 14, msg, offset + 2 * s[j++]);
|
| 318 |
+
G2b(3, 4, 9, 14, msg, offset + 2 * s[j++]);
|
| 319 |
+
}
|
| 320 |
+
this.v0l ^= BBUF[0] ^ BBUF[16];
|
| 321 |
+
this.v0h ^= BBUF[1] ^ BBUF[17];
|
| 322 |
+
this.v1l ^= BBUF[2] ^ BBUF[18];
|
| 323 |
+
this.v1h ^= BBUF[3] ^ BBUF[19];
|
| 324 |
+
this.v2l ^= BBUF[4] ^ BBUF[20];
|
| 325 |
+
this.v2h ^= BBUF[5] ^ BBUF[21];
|
| 326 |
+
this.v3l ^= BBUF[6] ^ BBUF[22];
|
| 327 |
+
this.v3h ^= BBUF[7] ^ BBUF[23];
|
| 328 |
+
this.v4l ^= BBUF[8] ^ BBUF[24];
|
| 329 |
+
this.v4h ^= BBUF[9] ^ BBUF[25];
|
| 330 |
+
this.v5l ^= BBUF[10] ^ BBUF[26];
|
| 331 |
+
this.v5h ^= BBUF[11] ^ BBUF[27];
|
| 332 |
+
this.v6l ^= BBUF[12] ^ BBUF[28];
|
| 333 |
+
this.v6h ^= BBUF[13] ^ BBUF[29];
|
| 334 |
+
this.v7l ^= BBUF[14] ^ BBUF[30];
|
| 335 |
+
this.v7h ^= BBUF[15] ^ BBUF[31];
|
| 336 |
+
clean(BBUF);
|
| 337 |
+
}
|
| 338 |
+
destroy(): void {
|
| 339 |
+
this.destroyed = true;
|
| 340 |
+
clean(this.buffer32);
|
| 341 |
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
| 342 |
+
}
|
| 343 |
+
}
|
| 344 |
+
|
| 345 |
+
/**
|
| 346 |
+
* Blake2b hash function. 64-bit. 1.5x slower than blake2s in JS.
|
| 347 |
+
* @param msg - message that would be hashed
|
| 348 |
+
* @param opts - dkLen output length, key for MAC mode, salt, personalization
|
| 349 |
+
*/
|
| 350 |
+
export const blake2b: CHashO = /* @__PURE__ */ createOptHasher<BLAKE2b, Blake2Opts>(
|
| 351 |
+
(opts) => new BLAKE2b(opts)
|
| 352 |
+
);
|
| 353 |
+
|
| 354 |
+
// =================
|
| 355 |
+
// Blake2S
|
| 356 |
+
// =================
|
| 357 |
+
|
| 358 |
+
// prettier-ignore
|
| 359 |
+
export type Num16 = {
|
| 360 |
+
v0: number; v1: number; v2: number; v3: number;
|
| 361 |
+
v4: number; v5: number; v6: number; v7: number;
|
| 362 |
+
v8: number; v9: number; v10: number; v11: number;
|
| 363 |
+
v12: number; v13: number; v14: number; v15: number;
|
| 364 |
+
};
|
| 365 |
+
|
| 366 |
+
// prettier-ignore
|
| 367 |
+
export function compress(s: Uint8Array, offset: number, msg: Uint32Array, rounds: number,
|
| 368 |
+
v0: number, v1: number, v2: number, v3: number, v4: number, v5: number, v6: number, v7: number,
|
| 369 |
+
v8: number, v9: number, v10: number, v11: number, v12: number, v13: number, v14: number, v15: number,
|
| 370 |
+
): Num16 {
|
| 371 |
+
let j = 0;
|
| 372 |
+
for (let i = 0; i < rounds; i++) {
|
| 373 |
+
({ a: v0, b: v4, c: v8, d: v12 } = G1s(v0, v4, v8, v12, msg[offset + s[j++]]));
|
| 374 |
+
({ a: v0, b: v4, c: v8, d: v12 } = G2s(v0, v4, v8, v12, msg[offset + s[j++]]));
|
| 375 |
+
({ a: v1, b: v5, c: v9, d: v13 } = G1s(v1, v5, v9, v13, msg[offset + s[j++]]));
|
| 376 |
+
({ a: v1, b: v5, c: v9, d: v13 } = G2s(v1, v5, v9, v13, msg[offset + s[j++]]));
|
| 377 |
+
({ a: v2, b: v6, c: v10, d: v14 } = G1s(v2, v6, v10, v14, msg[offset + s[j++]]));
|
| 378 |
+
({ a: v2, b: v6, c: v10, d: v14 } = G2s(v2, v6, v10, v14, msg[offset + s[j++]]));
|
| 379 |
+
({ a: v3, b: v7, c: v11, d: v15 } = G1s(v3, v7, v11, v15, msg[offset + s[j++]]));
|
| 380 |
+
({ a: v3, b: v7, c: v11, d: v15 } = G2s(v3, v7, v11, v15, msg[offset + s[j++]]));
|
| 381 |
+
|
| 382 |
+
({ a: v0, b: v5, c: v10, d: v15 } = G1s(v0, v5, v10, v15, msg[offset + s[j++]]));
|
| 383 |
+
({ a: v0, b: v5, c: v10, d: v15 } = G2s(v0, v5, v10, v15, msg[offset + s[j++]]));
|
| 384 |
+
({ a: v1, b: v6, c: v11, d: v12 } = G1s(v1, v6, v11, v12, msg[offset + s[j++]]));
|
| 385 |
+
({ a: v1, b: v6, c: v11, d: v12 } = G2s(v1, v6, v11, v12, msg[offset + s[j++]]));
|
| 386 |
+
({ a: v2, b: v7, c: v8, d: v13 } = G1s(v2, v7, v8, v13, msg[offset + s[j++]]));
|
| 387 |
+
({ a: v2, b: v7, c: v8, d: v13 } = G2s(v2, v7, v8, v13, msg[offset + s[j++]]));
|
| 388 |
+
({ a: v3, b: v4, c: v9, d: v14 } = G1s(v3, v4, v9, v14, msg[offset + s[j++]]));
|
| 389 |
+
({ a: v3, b: v4, c: v9, d: v14 } = G2s(v3, v4, v9, v14, msg[offset + s[j++]]));
|
| 390 |
+
}
|
| 391 |
+
return { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 };
|
| 392 |
+
}
|
| 393 |
+
|
| 394 |
+
const B2S_IV = SHA256_IV;
|
| 395 |
+
export class BLAKE2s extends BLAKE2<BLAKE2s> {
|
| 396 |
+
// Internal state, same as SHA-256
|
| 397 |
+
private v0 = B2S_IV[0] | 0;
|
| 398 |
+
private v1 = B2S_IV[1] | 0;
|
| 399 |
+
private v2 = B2S_IV[2] | 0;
|
| 400 |
+
private v3 = B2S_IV[3] | 0;
|
| 401 |
+
private v4 = B2S_IV[4] | 0;
|
| 402 |
+
private v5 = B2S_IV[5] | 0;
|
| 403 |
+
private v6 = B2S_IV[6] | 0;
|
| 404 |
+
private v7 = B2S_IV[7] | 0;
|
| 405 |
+
|
| 406 |
+
constructor(opts: Blake2Opts = {}) {
|
| 407 |
+
const olen = opts.dkLen === undefined ? 32 : opts.dkLen;
|
| 408 |
+
super(64, olen);
|
| 409 |
+
checkBlake2Opts(olen, opts, 32, 8, 8);
|
| 410 |
+
let { key, personalization, salt } = opts;
|
| 411 |
+
let keyLength = 0;
|
| 412 |
+
if (key !== undefined) {
|
| 413 |
+
key = toBytes(key);
|
| 414 |
+
keyLength = key.length;
|
| 415 |
+
}
|
| 416 |
+
this.v0 ^= this.outputLen | (keyLength << 8) | (0x01 << 16) | (0x01 << 24);
|
| 417 |
+
if (salt !== undefined) {
|
| 418 |
+
salt = toBytes(salt);
|
| 419 |
+
const slt = u32(salt as Uint8Array);
|
| 420 |
+
this.v4 ^= swap8IfBE(slt[0]);
|
| 421 |
+
this.v5 ^= swap8IfBE(slt[1]);
|
| 422 |
+
}
|
| 423 |
+
if (personalization !== undefined) {
|
| 424 |
+
personalization = toBytes(personalization);
|
| 425 |
+
const pers = u32(personalization as Uint8Array);
|
| 426 |
+
this.v6 ^= swap8IfBE(pers[0]);
|
| 427 |
+
this.v7 ^= swap8IfBE(pers[1]);
|
| 428 |
+
}
|
| 429 |
+
if (key !== undefined) {
|
| 430 |
+
// Pad to blockLen and update
|
| 431 |
+
abytes(key);
|
| 432 |
+
const tmp = new Uint8Array(this.blockLen);
|
| 433 |
+
tmp.set(key);
|
| 434 |
+
this.update(tmp);
|
| 435 |
+
}
|
| 436 |
+
}
|
| 437 |
+
protected get(): [number, number, number, number, number, number, number, number] {
|
| 438 |
+
const { v0, v1, v2, v3, v4, v5, v6, v7 } = this;
|
| 439 |
+
return [v0, v1, v2, v3, v4, v5, v6, v7];
|
| 440 |
+
}
|
| 441 |
+
// prettier-ignore
|
| 442 |
+
protected set(
|
| 443 |
+
v0: number, v1: number, v2: number, v3: number, v4: number, v5: number, v6: number, v7: number
|
| 444 |
+
): void {
|
| 445 |
+
this.v0 = v0 | 0;
|
| 446 |
+
this.v1 = v1 | 0;
|
| 447 |
+
this.v2 = v2 | 0;
|
| 448 |
+
this.v3 = v3 | 0;
|
| 449 |
+
this.v4 = v4 | 0;
|
| 450 |
+
this.v5 = v5 | 0;
|
| 451 |
+
this.v6 = v6 | 0;
|
| 452 |
+
this.v7 = v7 | 0;
|
| 453 |
+
}
|
| 454 |
+
protected compress(msg: Uint32Array, offset: number, isLast: boolean): void {
|
| 455 |
+
const { h, l } = u64.fromBig(BigInt(this.length));
|
| 456 |
+
// prettier-ignore
|
| 457 |
+
const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } =
|
| 458 |
+
compress(
|
| 459 |
+
BSIGMA, offset, msg, 10,
|
| 460 |
+
this.v0, this.v1, this.v2, this.v3, this.v4, this.v5, this.v6, this.v7,
|
| 461 |
+
B2S_IV[0], B2S_IV[1], B2S_IV[2], B2S_IV[3], l ^ B2S_IV[4], h ^ B2S_IV[5], isLast ? ~B2S_IV[6] : B2S_IV[6], B2S_IV[7]
|
| 462 |
+
);
|
| 463 |
+
this.v0 ^= v0 ^ v8;
|
| 464 |
+
this.v1 ^= v1 ^ v9;
|
| 465 |
+
this.v2 ^= v2 ^ v10;
|
| 466 |
+
this.v3 ^= v3 ^ v11;
|
| 467 |
+
this.v4 ^= v4 ^ v12;
|
| 468 |
+
this.v5 ^= v5 ^ v13;
|
| 469 |
+
this.v6 ^= v6 ^ v14;
|
| 470 |
+
this.v7 ^= v7 ^ v15;
|
| 471 |
+
}
|
| 472 |
+
destroy(): void {
|
| 473 |
+
this.destroyed = true;
|
| 474 |
+
clean(this.buffer32);
|
| 475 |
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
| 476 |
+
}
|
| 477 |
+
}
|
| 478 |
+
|
| 479 |
+
/**
|
| 480 |
+
* Blake2s hash function. Focuses on 8-bit to 32-bit platforms. 1.5x faster than blake2b in JS.
|
| 481 |
+
* @param msg - message that would be hashed
|
| 482 |
+
* @param opts - dkLen output length, key for MAC mode, salt, personalization
|
| 483 |
+
*/
|
| 484 |
+
export const blake2s: CHashO = /* @__PURE__ */ createOptHasher<BLAKE2s, Blake2Opts>(
|
| 485 |
+
(opts) => new BLAKE2s(opts)
|
| 486 |
+
);
|
node_modules/@noble/hashes/src/blake2b.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Blake2b hash function. Focuses on 64-bit platforms, but in JS speed different from Blake2s is negligible.
|
| 3 |
+
* @module
|
| 4 |
+
* @deprecated
|
| 5 |
+
*/
|
| 6 |
+
import { BLAKE2b as B2B, blake2b as b2b } from './blake2.ts';
|
| 7 |
+
/** @deprecated Use import from `noble/hashes/blake2` module */
|
| 8 |
+
export const BLAKE2b: typeof B2B = B2B;
|
| 9 |
+
/** @deprecated Use import from `noble/hashes/blake2` module */
|
| 10 |
+
export const blake2b: typeof b2b = b2b;
|
node_modules/@noble/hashes/src/blake2s.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Blake2s hash function. Focuses on 8-bit to 32-bit platforms. blake2b for 64-bit, but in JS it is slower.
|
| 3 |
+
* @module
|
| 4 |
+
* @deprecated
|
| 5 |
+
*/
|
| 6 |
+
import { G1s as G1s_n, G2s as G2s_n } from './_blake.ts';
|
| 7 |
+
import { SHA256_IV } from './_md.ts';
|
| 8 |
+
import { BLAKE2s as B2S, blake2s as b2s, compress as compress_n } from './blake2.ts';
|
| 9 |
+
/** @deprecated Use import from `noble/hashes/blake2` module */
|
| 10 |
+
export const B2S_IV: Uint32Array = SHA256_IV;
|
| 11 |
+
/** @deprecated Use import from `noble/hashes/blake2` module */
|
| 12 |
+
export const G1s: typeof G1s_n = G1s_n;
|
| 13 |
+
/** @deprecated Use import from `noble/hashes/blake2` module */
|
| 14 |
+
export const G2s: typeof G2s_n = G2s_n;
|
| 15 |
+
/** @deprecated Use import from `noble/hashes/blake2` module */
|
| 16 |
+
export const compress: typeof compress_n = compress_n;
|
| 17 |
+
/** @deprecated Use import from `noble/hashes/blake2` module */
|
| 18 |
+
export const BLAKE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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Blake3 fast hash is Blake2 with reduced security (round count). Can also be used as MAC & KDF.
|
| 3 |
+
*
|
| 4 |
+
* It is advertised as "the fastest cryptographic hash". However, it isn't true in JS.
|
| 5 |
+
* Why is this so slow? While it should be 6x faster than blake2b, perf diff is only 20%:
|
| 6 |
+
*
|
| 7 |
+
* * There is only 30% reduction in number of rounds from blake2s
|
| 8 |
+
* * Speed-up comes from tree structure, which is parallelized using SIMD & threading.
|
| 9 |
+
* These features are not present in JS, so we only get overhead from trees.
|
| 10 |
+
* * Parallelization only happens on 1024-byte chunks: there is no benefit for small inputs.
|
| 11 |
+
* * It is still possible to make it faster using: a) loop unrolling b) web workers c) wasm
|
| 12 |
+
* @module
|
| 13 |
+
*/
|
| 14 |
+
import { SHA256_IV } from './_md.ts';
|
| 15 |
+
import { fromBig } from './_u64.ts';
|
| 16 |
+
import { BLAKE2, compress } from './blake2.ts';
|
| 17 |
+
// prettier-ignore
|
| 18 |
+
import {
|
| 19 |
+
abytes, aexists, anumber, aoutput,
|
| 20 |
+
clean, createXOFer, swap32IfBE, toBytes, u32, u8,
|
| 21 |
+
type CHashXO, type HashXOF, type Input
|
| 22 |
+
} from './utils.ts';
|
| 23 |
+
|
| 24 |
+
// Flag bitset
|
| 25 |
+
const B3_Flags = {
|
| 26 |
+
CHUNK_START: 0b1,
|
| 27 |
+
CHUNK_END: 0b10,
|
| 28 |
+
PARENT: 0b100,
|
| 29 |
+
ROOT: 0b1000,
|
| 30 |
+
KEYED_HASH: 0b10000,
|
| 31 |
+
DERIVE_KEY_CONTEXT: 0b100000,
|
| 32 |
+
DERIVE_KEY_MATERIAL: 0b1000000,
|
| 33 |
+
} as const;
|
| 34 |
+
|
| 35 |
+
const B3_IV = SHA256_IV.slice();
|
| 36 |
+
|
| 37 |
+
const B3_SIGMA: Uint8Array = /* @__PURE__ */ (() => {
|
| 38 |
+
const Id = Array.from({ length: 16 }, (_, i) => i);
|
| 39 |
+
const permute = (arr: number[]) =>
|
| 40 |
+
[2, 6, 3, 10, 7, 0, 4, 13, 1, 11, 12, 5, 9, 14, 15, 8].map((i) => arr[i]);
|
| 41 |
+
const res: number[] = [];
|
| 42 |
+
for (let i = 0, v = Id; i < 7; i++, v = permute(v)) res.push(...v);
|
| 43 |
+
return Uint8Array.from(res);
|
| 44 |
+
})();
|
| 45 |
+
|
| 46 |
+
/**
|
| 47 |
+
* Ensure to use EITHER `key` OR `context`, not both.
|
| 48 |
+
*
|
| 49 |
+
* * `key`: 32-byte MAC key.
|
| 50 |
+
* * `context`: string for KDF. Should be hardcoded, globally unique, and application - specific.
|
| 51 |
+
* A good default format for the context string is "[application] [commit timestamp] [purpose]".
|
| 52 |
+
*/
|
| 53 |
+
export type Blake3Opts = { dkLen?: number; key?: Input; context?: Input };
|
| 54 |
+
|
| 55 |
+
/** Blake3 hash. Can be used as MAC and KDF. */
|
| 56 |
+
export class BLAKE3 extends BLAKE2<BLAKE3> implements HashXOF<BLAKE3> {
|
| 57 |
+
private chunkPos = 0; // Position of current block in chunk
|
| 58 |
+
private chunksDone = 0; // How many chunks we already have
|
| 59 |
+
private flags = 0 | 0;
|
| 60 |
+
private IV: Uint32Array;
|
| 61 |
+
private state: Uint32Array;
|
| 62 |
+
private stack: Uint32Array[] = [];
|
| 63 |
+
// Output
|
| 64 |
+
private posOut = 0;
|
| 65 |
+
private bufferOut32 = new Uint32Array(16);
|
| 66 |
+
private bufferOut: Uint8Array;
|
| 67 |
+
private chunkOut = 0; // index of output chunk
|
| 68 |
+
private enableXOF = true;
|
| 69 |
+
|
| 70 |
+
constructor(opts: Blake3Opts = {}, flags = 0) {
|
| 71 |
+
super(64, opts.dkLen === undefined ? 32 : opts.dkLen);
|
| 72 |
+
const { key, context } = opts;
|
| 73 |
+
const hasContext = context !== undefined;
|
| 74 |
+
if (key !== undefined) {
|
| 75 |
+
if (hasContext) throw new Error('Only "key" or "context" can be specified at same time');
|
| 76 |
+
const k = toBytes(key).slice();
|
| 77 |
+
abytes(k, 32);
|
| 78 |
+
this.IV = u32(k);
|
| 79 |
+
swap32IfBE(this.IV);
|
| 80 |
+
this.flags = flags | B3_Flags.KEYED_HASH;
|
| 81 |
+
} else if (hasContext) {
|
| 82 |
+
const ctx = toBytes(context);
|
| 83 |
+
const contextKey = new BLAKE3({ dkLen: 32 }, B3_Flags.DERIVE_KEY_CONTEXT)
|
| 84 |
+
.update(ctx)
|
| 85 |
+
.digest();
|
| 86 |
+
this.IV = u32(contextKey);
|
| 87 |
+
swap32IfBE(this.IV);
|
| 88 |
+
this.flags = flags | B3_Flags.DERIVE_KEY_MATERIAL;
|
| 89 |
+
} else {
|
| 90 |
+
this.IV = B3_IV.slice();
|
| 91 |
+
this.flags = flags;
|
| 92 |
+
}
|
| 93 |
+
this.state = this.IV.slice();
|
| 94 |
+
this.bufferOut = u8(this.bufferOut32);
|
| 95 |
+
}
|
| 96 |
+
// Unused
|
| 97 |
+
protected get(): [] {
|
| 98 |
+
return [];
|
| 99 |
+
}
|
| 100 |
+
protected set(): void {}
|
| 101 |
+
private b2Compress(counter: number, flags: number, buf: Uint32Array, bufPos: number = 0) {
|
| 102 |
+
const { state: s, pos } = this;
|
| 103 |
+
const { h, l } = fromBig(BigInt(counter), true);
|
| 104 |
+
// prettier-ignore
|
| 105 |
+
const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } =
|
| 106 |
+
compress(
|
| 107 |
+
B3_SIGMA, bufPos, buf, 7,
|
| 108 |
+
s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7],
|
| 109 |
+
B3_IV[0], B3_IV[1], B3_IV[2], B3_IV[3], h, l, pos, flags
|
| 110 |
+
);
|
| 111 |
+
s[0] = v0 ^ v8;
|
| 112 |
+
s[1] = v1 ^ v9;
|
| 113 |
+
s[2] = v2 ^ v10;
|
| 114 |
+
s[3] = v3 ^ v11;
|
| 115 |
+
s[4] = v4 ^ v12;
|
| 116 |
+
s[5] = v5 ^ v13;
|
| 117 |
+
s[6] = v6 ^ v14;
|
| 118 |
+
s[7] = v7 ^ v15;
|
| 119 |
+
}
|
| 120 |
+
protected compress(buf: Uint32Array, bufPos: number = 0, isLast: boolean = false): void {
|
| 121 |
+
// Compress last block
|
| 122 |
+
let flags = this.flags;
|
| 123 |
+
if (!this.chunkPos) flags |= B3_Flags.CHUNK_START;
|
| 124 |
+
if (this.chunkPos === 15 || isLast) flags |= B3_Flags.CHUNK_END;
|
| 125 |
+
if (!isLast) this.pos = this.blockLen;
|
| 126 |
+
this.b2Compress(this.chunksDone, flags, buf, bufPos);
|
| 127 |
+
this.chunkPos += 1;
|
| 128 |
+
// If current block is last in chunk (16 blocks), then compress chunks
|
| 129 |
+
if (this.chunkPos === 16 || isLast) {
|
| 130 |
+
let chunk = this.state;
|
| 131 |
+
this.state = this.IV.slice();
|
| 132 |
+
// If not the last one, compress only when there are trailing zeros in chunk counter
|
| 133 |
+
// chunks used as binary tree where current stack is path. Zero means current leaf is finished and can be compressed.
|
| 134 |
+
// 1 (001) - leaf not finished (just push current chunk to stack)
|
| 135 |
+
// 2 (010) - leaf finished at depth=1 (merge with last elm on stack and push back)
|
| 136 |
+
// 3 (011) - last leaf not finished
|
| 137 |
+
// 4 (100) - leafs finished at depth=1 and depth=2
|
| 138 |
+
for (let last, chunks = this.chunksDone + 1; isLast || !(chunks & 1); chunks >>= 1) {
|
| 139 |
+
if (!(last = this.stack.pop())) break;
|
| 140 |
+
this.buffer32.set(last, 0);
|
| 141 |
+
this.buffer32.set(chunk, 8);
|
| 142 |
+
this.pos = this.blockLen;
|
| 143 |
+
this.b2Compress(0, this.flags | B3_Flags.PARENT, this.buffer32, 0);
|
| 144 |
+
chunk = this.state;
|
| 145 |
+
this.state = this.IV.slice();
|
| 146 |
+
}
|
| 147 |
+
this.chunksDone++;
|
| 148 |
+
this.chunkPos = 0;
|
| 149 |
+
this.stack.push(chunk);
|
| 150 |
+
}
|
| 151 |
+
this.pos = 0;
|
| 152 |
+
}
|
| 153 |
+
_cloneInto(to?: BLAKE3): BLAKE3 {
|
| 154 |
+
to = super._cloneInto(to) as BLAKE3;
|
| 155 |
+
const { IV, flags, state, chunkPos, posOut, chunkOut, stack, chunksDone } = this;
|
| 156 |
+
to.state.set(state.slice());
|
| 157 |
+
to.stack = stack.map((i) => Uint32Array.from(i));
|
| 158 |
+
to.IV.set(IV);
|
| 159 |
+
to.flags = flags;
|
| 160 |
+
to.chunkPos = chunkPos;
|
| 161 |
+
to.chunksDone = chunksDone;
|
| 162 |
+
to.posOut = posOut;
|
| 163 |
+
to.chunkOut = chunkOut;
|
| 164 |
+
to.enableXOF = this.enableXOF;
|
| 165 |
+
to.bufferOut32.set(this.bufferOut32);
|
| 166 |
+
return to;
|
| 167 |
+
}
|
| 168 |
+
destroy(): void {
|
| 169 |
+
this.destroyed = true;
|
| 170 |
+
clean(this.state, this.buffer32, this.IV, this.bufferOut32);
|
| 171 |
+
clean(...this.stack);
|
| 172 |
+
}
|
| 173 |
+
// Same as b2Compress, but doesn't modify state and returns 16 u32 array (instead of 8)
|
| 174 |
+
private b2CompressOut() {
|
| 175 |
+
const { state: s, pos, flags, buffer32, bufferOut32: out32 } = this;
|
| 176 |
+
const { h, l } = fromBig(BigInt(this.chunkOut++));
|
| 177 |
+
swap32IfBE(buffer32);
|
| 178 |
+
// prettier-ignore
|
| 179 |
+
const { v0, v1, v2, v3, v4, v5, v6, v7, v8, v9, v10, v11, v12, v13, v14, v15 } =
|
| 180 |
+
compress(
|
| 181 |
+
B3_SIGMA, 0, buffer32, 7,
|
| 182 |
+
s[0], s[1], s[2], s[3], s[4], s[5], s[6], s[7],
|
| 183 |
+
B3_IV[0], B3_IV[1], B3_IV[2], B3_IV[3], l, h, pos, flags
|
| 184 |
+
);
|
| 185 |
+
out32[0] = v0 ^ v8;
|
| 186 |
+
out32[1] = v1 ^ v9;
|
| 187 |
+
out32[2] = v2 ^ v10;
|
| 188 |
+
out32[3] = v3 ^ v11;
|
| 189 |
+
out32[4] = v4 ^ v12;
|
| 190 |
+
out32[5] = v5 ^ v13;
|
| 191 |
+
out32[6] = v6 ^ v14;
|
| 192 |
+
out32[7] = v7 ^ v15;
|
| 193 |
+
out32[8] = s[0] ^ v8;
|
| 194 |
+
out32[9] = s[1] ^ v9;
|
| 195 |
+
out32[10] = s[2] ^ v10;
|
| 196 |
+
out32[11] = s[3] ^ v11;
|
| 197 |
+
out32[12] = s[4] ^ v12;
|
| 198 |
+
out32[13] = s[5] ^ v13;
|
| 199 |
+
out32[14] = s[6] ^ v14;
|
| 200 |
+
out32[15] = s[7] ^ v15;
|
| 201 |
+
swap32IfBE(buffer32);
|
| 202 |
+
swap32IfBE(out32);
|
| 203 |
+
this.posOut = 0;
|
| 204 |
+
}
|
| 205 |
+
protected finish(): void {
|
| 206 |
+
if (this.finished) return;
|
| 207 |
+
this.finished = true;
|
| 208 |
+
// Padding
|
| 209 |
+
clean(this.buffer.subarray(this.pos));
|
| 210 |
+
// Process last chunk
|
| 211 |
+
let flags = this.flags | B3_Flags.ROOT;
|
| 212 |
+
if (this.stack.length) {
|
| 213 |
+
flags |= B3_Flags.PARENT;
|
| 214 |
+
swap32IfBE(this.buffer32);
|
| 215 |
+
this.compress(this.buffer32, 0, true);
|
| 216 |
+
swap32IfBE(this.buffer32);
|
| 217 |
+
this.chunksDone = 0;
|
| 218 |
+
this.pos = this.blockLen;
|
| 219 |
+
} else {
|
| 220 |
+
flags |= (!this.chunkPos ? B3_Flags.CHUNK_START : 0) | B3_Flags.CHUNK_END;
|
| 221 |
+
}
|
| 222 |
+
this.flags = flags;
|
| 223 |
+
this.b2CompressOut();
|
| 224 |
+
}
|
| 225 |
+
private writeInto(out: Uint8Array) {
|
| 226 |
+
aexists(this, false);
|
| 227 |
+
abytes(out);
|
| 228 |
+
this.finish();
|
| 229 |
+
const { blockLen, bufferOut } = this;
|
| 230 |
+
for (let pos = 0, len = out.length; pos < len; ) {
|
| 231 |
+
if (this.posOut >= blockLen) this.b2CompressOut();
|
| 232 |
+
const take = Math.min(blockLen - this.posOut, len - pos);
|
| 233 |
+
out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
|
| 234 |
+
this.posOut += take;
|
| 235 |
+
pos += take;
|
| 236 |
+
}
|
| 237 |
+
return out;
|
| 238 |
+
}
|
| 239 |
+
xofInto(out: Uint8Array): Uint8Array {
|
| 240 |
+
if (!this.enableXOF) throw new Error('XOF is not possible after digest call');
|
| 241 |
+
return this.writeInto(out);
|
| 242 |
+
}
|
| 243 |
+
xof(bytes: number): Uint8Array {
|
| 244 |
+
anumber(bytes);
|
| 245 |
+
return this.xofInto(new Uint8Array(bytes));
|
| 246 |
+
}
|
| 247 |
+
digestInto(out: Uint8Array): Uint8Array {
|
| 248 |
+
aoutput(out, this);
|
| 249 |
+
if (this.finished) throw new Error('digest() was already called');
|
| 250 |
+
this.enableXOF = false;
|
| 251 |
+
this.writeInto(out);
|
| 252 |
+
this.destroy();
|
| 253 |
+
return out;
|
| 254 |
+
}
|
| 255 |
+
digest(): Uint8Array {
|
| 256 |
+
return this.digestInto(new Uint8Array(this.outputLen));
|
| 257 |
+
}
|
| 258 |
+
}
|
| 259 |
+
|
| 260 |
+
/**
|
| 261 |
+
* BLAKE3 hash function. Can be used as MAC and KDF.
|
| 262 |
+
* @param msg - message that would be hashed
|
| 263 |
+
* @param opts - `dkLen` for output length, `key` for MAC mode, `context` for KDF mode
|
| 264 |
+
* @example
|
| 265 |
+
* const data = new Uint8Array(32);
|
| 266 |
+
* const hash = blake3(data);
|
| 267 |
+
* const mac = blake3(data, { key: new Uint8Array(32) });
|
| 268 |
+
* const kdf = blake3(data, { context: 'application name' });
|
| 269 |
+
*/
|
| 270 |
+
export const blake3: CHashXO = /* @__PURE__ */ createXOFer<BLAKE3, Blake3Opts>(
|
| 271 |
+
(opts) => new BLAKE3(opts)
|
| 272 |
+
);
|
node_modules/@noble/hashes/src/crypto.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Internal webcrypto alias.
|
| 3 |
+
* We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
|
| 4 |
+
* See utils.ts for details.
|
| 5 |
+
* @module
|
| 6 |
+
*/
|
| 7 |
+
declare const globalThis: Record<string, any> | undefined;
|
| 8 |
+
export const crypto: any =
|
| 9 |
+
typeof globalThis === 'object' && 'crypto' in globalThis ? globalThis.crypto : undefined;
|
node_modules/@noble/hashes/src/cryptoNode.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Internal webcrypto alias.
|
| 3 |
+
* We prefer WebCrypto aka globalThis.crypto, which exists in node.js 16+.
|
| 4 |
+
* Falls back to Node.js built-in crypto for Node.js <=v14.
|
| 5 |
+
* See utils.ts for details.
|
| 6 |
+
* @module
|
| 7 |
+
*/
|
| 8 |
+
// @ts-ignore
|
| 9 |
+
import * as nc from 'node:crypto';
|
| 10 |
+
export const crypto: any =
|
| 11 |
+
nc && typeof nc === 'object' && 'webcrypto' in nc
|
| 12 |
+
? (nc.webcrypto as any)
|
| 13 |
+
: nc && typeof nc === 'object' && 'randomBytes' in nc
|
| 14 |
+
? nc
|
| 15 |
+
: undefined;
|
node_modules/@noble/hashes/src/eskdf.ts
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Experimental KDF for AES.
|
| 3 |
+
*/
|
| 4 |
+
import { hkdf } from './hkdf.ts';
|
| 5 |
+
import { pbkdf2 as _pbkdf2 } from './pbkdf2.ts';
|
| 6 |
+
import { scrypt as _scrypt } from './scrypt.ts';
|
| 7 |
+
import { sha256 } from './sha256.ts';
|
| 8 |
+
import { abytes, bytesToHex, clean, createView, hexToBytes, kdfInputToBytes } from './utils.ts';
|
| 9 |
+
|
| 10 |
+
// A tiny KDF for various applications like AES key-gen.
|
| 11 |
+
// Uses HKDF in a non-standard way, so it's not "KDF-secure", only "PRF-secure".
|
| 12 |
+
// Which is good enough: assume sha2-256 retained preimage resistance.
|
| 13 |
+
|
| 14 |
+
const SCRYPT_FACTOR = 2 ** 19;
|
| 15 |
+
const PBKDF2_FACTOR = 2 ** 17;
|
| 16 |
+
|
| 17 |
+
// Scrypt KDF
|
| 18 |
+
export function scrypt(password: string, salt: string): Uint8Array {
|
| 19 |
+
return _scrypt(password, salt, { N: SCRYPT_FACTOR, r: 8, p: 1, dkLen: 32 });
|
| 20 |
+
}
|
| 21 |
+
|
| 22 |
+
// PBKDF2-HMAC-SHA256
|
| 23 |
+
export function pbkdf2(password: string, salt: string): Uint8Array {
|
| 24 |
+
return _pbkdf2(sha256, password, salt, { c: PBKDF2_FACTOR, dkLen: 32 });
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
// Combines two 32-byte byte arrays
|
| 28 |
+
function xor32(a: Uint8Array, b: Uint8Array): Uint8Array {
|
| 29 |
+
abytes(a, 32);
|
| 30 |
+
abytes(b, 32);
|
| 31 |
+
const arr = new Uint8Array(32);
|
| 32 |
+
for (let i = 0; i < 32; i++) {
|
| 33 |
+
arr[i] = a[i] ^ b[i];
|
| 34 |
+
}
|
| 35 |
+
return arr;
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
function strHasLength(str: string, min: number, max: number): boolean {
|
| 39 |
+
return typeof str === 'string' && str.length >= min && str.length <= max;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
/**
|
| 43 |
+
* Derives main seed. Takes a lot of time. Prefer `eskdf` method instead.
|
| 44 |
+
*/
|
| 45 |
+
export function deriveMainSeed(username: string, password: string): Uint8Array {
|
| 46 |
+
if (!strHasLength(username, 8, 255)) throw new Error('invalid username');
|
| 47 |
+
if (!strHasLength(password, 8, 255)) throw new Error('invalid password');
|
| 48 |
+
// Declared like this to throw off minifiers which auto-convert .fromCharCode(1) to actual string.
|
| 49 |
+
// String with non-ascii may be problematic in some envs
|
| 50 |
+
const codes = { _1: 1, _2: 2 };
|
| 51 |
+
const sep = { s: String.fromCharCode(codes._1), p: String.fromCharCode(codes._2) };
|
| 52 |
+
const scr = scrypt(password + sep.s, username + sep.s);
|
| 53 |
+
const pbk = pbkdf2(password + sep.p, username + sep.p);
|
| 54 |
+
const res = xor32(scr, pbk);
|
| 55 |
+
clean(scr, pbk);
|
| 56 |
+
return res;
|
| 57 |
+
}
|
| 58 |
+
|
| 59 |
+
type AccountID = number | string;
|
| 60 |
+
|
| 61 |
+
/**
|
| 62 |
+
* Converts protocol & accountId pair to HKDF salt & info params.
|
| 63 |
+
*/
|
| 64 |
+
function getSaltInfo(protocol: string, accountId: AccountID = 0) {
|
| 65 |
+
// Note that length here also repeats two lines below
|
| 66 |
+
// We do an additional length check here to reduce the scope of DoS attacks
|
| 67 |
+
if (!(strHasLength(protocol, 3, 15) && /^[a-z0-9]{3,15}$/.test(protocol))) {
|
| 68 |
+
throw new Error('invalid protocol');
|
| 69 |
+
}
|
| 70 |
+
|
| 71 |
+
// Allow string account ids for some protocols
|
| 72 |
+
const allowsStr = /^password\d{0,3}|ssh|tor|file$/.test(protocol);
|
| 73 |
+
let salt: Uint8Array; // Extract salt. Default is undefined.
|
| 74 |
+
if (typeof accountId === 'string') {
|
| 75 |
+
if (!allowsStr) throw new Error('accountId must be a number');
|
| 76 |
+
if (!strHasLength(accountId, 1, 255))
|
| 77 |
+
throw new Error('accountId must be string of length 1..255');
|
| 78 |
+
salt = kdfInputToBytes(accountId);
|
| 79 |
+
} else if (Number.isSafeInteger(accountId)) {
|
| 80 |
+
if (accountId < 0 || accountId > Math.pow(2, 32) - 1) throw new Error('invalid accountId');
|
| 81 |
+
// Convert to Big Endian Uint32
|
| 82 |
+
salt = new Uint8Array(4);
|
| 83 |
+
createView(salt).setUint32(0, accountId, false);
|
| 84 |
+
} else {
|
| 85 |
+
throw new Error('accountId must be a number' + (allowsStr ? ' or string' : ''));
|
| 86 |
+
}
|
| 87 |
+
const info = kdfInputToBytes(protocol);
|
| 88 |
+
return { salt, info };
|
| 89 |
+
}
|
| 90 |
+
|
| 91 |
+
type OptsLength = { keyLength: number };
|
| 92 |
+
type OptsMod = { modulus: bigint };
|
| 93 |
+
type KeyOpts = undefined | OptsLength | OptsMod;
|
| 94 |
+
|
| 95 |
+
function countBytes(num: bigint): number {
|
| 96 |
+
if (typeof num !== 'bigint' || num <= BigInt(128)) throw new Error('invalid number');
|
| 97 |
+
return Math.ceil(num.toString(2).length / 8);
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
/**
|
| 101 |
+
* Parses keyLength and modulus options to extract length of result key.
|
| 102 |
+
* If modulus is used, adds 64 bits to it as per FIPS 186 B.4.1 to combat modulo bias.
|
| 103 |
+
*/
|
| 104 |
+
function getKeyLength(options: KeyOpts): number {
|
| 105 |
+
if (!options || typeof options !== 'object') return 32;
|
| 106 |
+
const hasLen = 'keyLength' in options;
|
| 107 |
+
const hasMod = 'modulus' in options;
|
| 108 |
+
if (hasLen && hasMod) throw new Error('cannot combine keyLength and modulus options');
|
| 109 |
+
if (!hasLen && !hasMod) throw new Error('must have either keyLength or modulus option');
|
| 110 |
+
// FIPS 186 B.4.1 requires at least 64 more bits
|
| 111 |
+
const l = hasMod ? countBytes(options.modulus) + 8 : options.keyLength;
|
| 112 |
+
if (!(typeof l === 'number' && l >= 16 && l <= 8192)) throw new Error('invalid keyLength');
|
| 113 |
+
return l;
|
| 114 |
+
}
|
| 115 |
+
|
| 116 |
+
/**
|
| 117 |
+
* Converts key to bigint and divides it by modulus. Big Endian.
|
| 118 |
+
* Implements FIPS 186 B.4.1, which removes 0 and modulo bias from output.
|
| 119 |
+
*/
|
| 120 |
+
function modReduceKey(key: Uint8Array, modulus: bigint): Uint8Array {
|
| 121 |
+
const _1 = BigInt(1);
|
| 122 |
+
const num = BigInt('0x' + bytesToHex(key)); // check for ui8a, then bytesToNumber()
|
| 123 |
+
const res = (num % (modulus - _1)) + _1; // Remove 0 from output
|
| 124 |
+
if (res < _1) throw new Error('expected positive number'); // Guard against bad values
|
| 125 |
+
const len = key.length - 8; // FIPS requires 64 more bits = 8 bytes
|
| 126 |
+
const hex = res.toString(16).padStart(len * 2, '0'); // numberToHex()
|
| 127 |
+
const bytes = hexToBytes(hex);
|
| 128 |
+
if (bytes.length !== len) throw new Error('invalid length of result key');
|
| 129 |
+
return bytes;
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
+
// We are not using classes because constructor cannot be async
|
| 133 |
+
export interface ESKDF {
|
| 134 |
+
/**
|
| 135 |
+
* Derives a child key. Child key will not be associated with any
|
| 136 |
+
* other child key because of properties of underlying KDF.
|
| 137 |
+
*
|
| 138 |
+
* @param protocol - 3-15 character protocol name
|
| 139 |
+
* @param accountId - numeric identifier of account
|
| 140 |
+
* @param options - `keyLength: 64` or `modulus: 41920438n`
|
| 141 |
+
* @example deriveChildKey('aes', 0)
|
| 142 |
+
*/
|
| 143 |
+
deriveChildKey: (protocol: string, accountId: AccountID, options?: KeyOpts) => Uint8Array;
|
| 144 |
+
/**
|
| 145 |
+
* Deletes the main seed from eskdf instance
|
| 146 |
+
*/
|
| 147 |
+
expire: () => void;
|
| 148 |
+
/**
|
| 149 |
+
* Account fingerprint
|
| 150 |
+
*/
|
| 151 |
+
fingerprint: string;
|
| 152 |
+
}
|
| 153 |
+
|
| 154 |
+
/**
|
| 155 |
+
* ESKDF
|
| 156 |
+
* @param username - username, email, or identifier, min: 8 characters, should have enough entropy
|
| 157 |
+
* @param password - password, min: 8 characters, should have enough entropy
|
| 158 |
+
* @example
|
| 159 |
+
* const kdf = await eskdf('example-university', 'beginning-new-example');
|
| 160 |
+
* const key = kdf.deriveChildKey('aes', 0);
|
| 161 |
+
* console.log(kdf.fingerprint);
|
| 162 |
+
* kdf.expire();
|
| 163 |
+
*/
|
| 164 |
+
export async function eskdf(username: string, password: string): Promise<ESKDF> {
|
| 165 |
+
// We are using closure + object instead of class because
|
| 166 |
+
// we want to make `seed` non-accessible for any external function.
|
| 167 |
+
let seed: Uint8Array | undefined = deriveMainSeed(username, password);
|
| 168 |
+
|
| 169 |
+
function deriveCK(protocol: string, accountId: AccountID = 0, options?: KeyOpts): Uint8Array {
|
| 170 |
+
abytes(seed, 32);
|
| 171 |
+
const { salt, info } = getSaltInfo(protocol, accountId); // validate protocol & accountId
|
| 172 |
+
const keyLength = getKeyLength(options); // validate options
|
| 173 |
+
const key = hkdf(sha256, seed!, salt, info, keyLength);
|
| 174 |
+
// Modulus has already been validated
|
| 175 |
+
return options && 'modulus' in options ? modReduceKey(key, options.modulus) : key;
|
| 176 |
+
}
|
| 177 |
+
function expire() {
|
| 178 |
+
if (seed) seed.fill(1);
|
| 179 |
+
seed = undefined;
|
| 180 |
+
}
|
| 181 |
+
// prettier-ignore
|
| 182 |
+
const fingerprint = Array.from(deriveCK('fingerprint', 0))
|
| 183 |
+
.slice(0, 6)
|
| 184 |
+
.map((char) => char.toString(16).padStart(2, '0').toUpperCase())
|
| 185 |
+
.join(':');
|
| 186 |
+
return Object.freeze({ deriveChildKey: deriveCK, expire, fingerprint });
|
| 187 |
+
}
|
node_modules/@noble/hashes/src/hkdf.ts
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* HKDF (RFC 5869): extract + expand in one step.
|
| 3 |
+
* See https://soatok.blog/2021/11/17/understanding-hkdf/.
|
| 4 |
+
* @module
|
| 5 |
+
*/
|
| 6 |
+
import { hmac } from './hmac.ts';
|
| 7 |
+
import { ahash, anumber, type CHash, clean, type Input, toBytes } from './utils.ts';
|
| 8 |
+
|
| 9 |
+
/**
|
| 10 |
+
* HKDF-extract from spec. Less important part. `HKDF-Extract(IKM, salt) -> PRK`
|
| 11 |
+
* Arguments position differs from spec (IKM is first one, since it is not optional)
|
| 12 |
+
* @param hash - hash function that would be used (e.g. sha256)
|
| 13 |
+
* @param ikm - input keying material, the initial key
|
| 14 |
+
* @param salt - optional salt value (a non-secret random value)
|
| 15 |
+
*/
|
| 16 |
+
export function extract(hash: CHash, ikm: Input, salt?: Input): Uint8Array {
|
| 17 |
+
ahash(hash);
|
| 18 |
+
// NOTE: some libraries treat zero-length array as 'not provided';
|
| 19 |
+
// we don't, since we have undefined as 'not provided'
|
| 20 |
+
// https://github.com/RustCrypto/KDFs/issues/15
|
| 21 |
+
if (salt === undefined) salt = new Uint8Array(hash.outputLen);
|
| 22 |
+
return hmac(hash, toBytes(salt), toBytes(ikm));
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
const HKDF_COUNTER = /* @__PURE__ */ Uint8Array.from([0]);
|
| 26 |
+
const EMPTY_BUFFER = /* @__PURE__ */ Uint8Array.of();
|
| 27 |
+
|
| 28 |
+
/**
|
| 29 |
+
* HKDF-expand from the spec. The most important part. `HKDF-Expand(PRK, info, L) -> OKM`
|
| 30 |
+
* @param hash - hash function that would be used (e.g. sha256)
|
| 31 |
+
* @param prk - a pseudorandom key of at least HashLen octets (usually, the output from the extract step)
|
| 32 |
+
* @param info - optional context and application specific information (can be a zero-length string)
|
| 33 |
+
* @param length - length of output keying material in bytes
|
| 34 |
+
*/
|
| 35 |
+
export function expand(hash: CHash, prk: Input, info?: Input, length: number = 32): Uint8Array {
|
| 36 |
+
ahash(hash);
|
| 37 |
+
anumber(length);
|
| 38 |
+
const olen = hash.outputLen;
|
| 39 |
+
if (length > 255 * olen) throw new Error('Length should be <= 255*HashLen');
|
| 40 |
+
const blocks = Math.ceil(length / olen);
|
| 41 |
+
if (info === undefined) info = EMPTY_BUFFER;
|
| 42 |
+
// first L(ength) octets of T
|
| 43 |
+
const okm = new Uint8Array(blocks * olen);
|
| 44 |
+
// Re-use HMAC instance between blocks
|
| 45 |
+
const HMAC = hmac.create(hash, prk);
|
| 46 |
+
const HMACTmp = HMAC._cloneInto();
|
| 47 |
+
const T = new Uint8Array(HMAC.outputLen);
|
| 48 |
+
for (let counter = 0; counter < blocks; counter++) {
|
| 49 |
+
HKDF_COUNTER[0] = counter + 1;
|
| 50 |
+
// T(0) = empty string (zero length)
|
| 51 |
+
// T(N) = HMAC-Hash(PRK, T(N-1) | info | N)
|
| 52 |
+
HMACTmp.update(counter === 0 ? EMPTY_BUFFER : T)
|
| 53 |
+
.update(info)
|
| 54 |
+
.update(HKDF_COUNTER)
|
| 55 |
+
.digestInto(T);
|
| 56 |
+
okm.set(T, olen * counter);
|
| 57 |
+
HMAC._cloneInto(HMACTmp);
|
| 58 |
+
}
|
| 59 |
+
HMAC.destroy();
|
| 60 |
+
HMACTmp.destroy();
|
| 61 |
+
clean(T, HKDF_COUNTER);
|
| 62 |
+
return okm.slice(0, length);
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
/**
|
| 66 |
+
* HKDF (RFC 5869): derive keys from an initial input.
|
| 67 |
+
* Combines hkdf_extract + hkdf_expand in one step
|
| 68 |
+
* @param hash - hash function that would be used (e.g. sha256)
|
| 69 |
+
* @param ikm - input keying material, the initial key
|
| 70 |
+
* @param salt - optional salt value (a non-secret random value)
|
| 71 |
+
* @param info - optional context and application specific information (can be a zero-length string)
|
| 72 |
+
* @param length - length of output keying material in bytes
|
| 73 |
+
* @example
|
| 74 |
+
* import { hkdf } from '@noble/hashes/hkdf';
|
| 75 |
+
* import { sha256 } from '@noble/hashes/sha2';
|
| 76 |
+
* import { randomBytes } from '@noble/hashes/utils';
|
| 77 |
+
* const inputKey = randomBytes(32);
|
| 78 |
+
* const salt = randomBytes(32);
|
| 79 |
+
* const info = 'application-key';
|
| 80 |
+
* const hk1 = hkdf(sha256, inputKey, salt, info, 32);
|
| 81 |
+
*/
|
| 82 |
+
export const hkdf = (
|
| 83 |
+
hash: CHash,
|
| 84 |
+
ikm: Input,
|
| 85 |
+
salt: Input | undefined,
|
| 86 |
+
info: Input | undefined,
|
| 87 |
+
length: number
|
| 88 |
+
): Uint8Array => expand(hash, extract(hash, ikm, salt), info, length);
|
node_modules/@noble/hashes/src/hmac.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* HMAC: RFC2104 message authentication code.
|
| 3 |
+
* @module
|
| 4 |
+
*/
|
| 5 |
+
import { abytes, aexists, ahash, clean, Hash, toBytes, type CHash, type Input } from './utils.ts';
|
| 6 |
+
|
| 7 |
+
export class HMAC<T extends Hash<T>> extends Hash<HMAC<T>> {
|
| 8 |
+
oHash: T;
|
| 9 |
+
iHash: T;
|
| 10 |
+
blockLen: number;
|
| 11 |
+
outputLen: number;
|
| 12 |
+
private finished = false;
|
| 13 |
+
private destroyed = false;
|
| 14 |
+
|
| 15 |
+
constructor(hash: CHash, _key: Input) {
|
| 16 |
+
super();
|
| 17 |
+
ahash(hash);
|
| 18 |
+
const key = toBytes(_key);
|
| 19 |
+
this.iHash = hash.create() as T;
|
| 20 |
+
if (typeof this.iHash.update !== 'function')
|
| 21 |
+
throw new Error('Expected instance of class which extends utils.Hash');
|
| 22 |
+
this.blockLen = this.iHash.blockLen;
|
| 23 |
+
this.outputLen = this.iHash.outputLen;
|
| 24 |
+
const blockLen = this.blockLen;
|
| 25 |
+
const pad = new Uint8Array(blockLen);
|
| 26 |
+
// blockLen can be bigger than outputLen
|
| 27 |
+
pad.set(key.length > blockLen ? hash.create().update(key).digest() : key);
|
| 28 |
+
for (let i = 0; i < pad.length; i++) pad[i] ^= 0x36;
|
| 29 |
+
this.iHash.update(pad);
|
| 30 |
+
// By doing update (processing of first block) of outer hash here we can re-use it between multiple calls via clone
|
| 31 |
+
this.oHash = hash.create() as T;
|
| 32 |
+
// Undo internal XOR && apply outer XOR
|
| 33 |
+
for (let i = 0; i < pad.length; i++) pad[i] ^= 0x36 ^ 0x5c;
|
| 34 |
+
this.oHash.update(pad);
|
| 35 |
+
clean(pad);
|
| 36 |
+
}
|
| 37 |
+
update(buf: Input): this {
|
| 38 |
+
aexists(this);
|
| 39 |
+
this.iHash.update(buf);
|
| 40 |
+
return this;
|
| 41 |
+
}
|
| 42 |
+
digestInto(out: Uint8Array): void {
|
| 43 |
+
aexists(this);
|
| 44 |
+
abytes(out, this.outputLen);
|
| 45 |
+
this.finished = true;
|
| 46 |
+
this.iHash.digestInto(out);
|
| 47 |
+
this.oHash.update(out);
|
| 48 |
+
this.oHash.digestInto(out);
|
| 49 |
+
this.destroy();
|
| 50 |
+
}
|
| 51 |
+
digest(): Uint8Array {
|
| 52 |
+
const out = new Uint8Array(this.oHash.outputLen);
|
| 53 |
+
this.digestInto(out);
|
| 54 |
+
return out;
|
| 55 |
+
}
|
| 56 |
+
_cloneInto(to?: HMAC<T>): HMAC<T> {
|
| 57 |
+
// Create new instance without calling constructor since key already in state and we don't know it.
|
| 58 |
+
to ||= Object.create(Object.getPrototypeOf(this), {});
|
| 59 |
+
const { oHash, iHash, finished, destroyed, blockLen, outputLen } = this;
|
| 60 |
+
to = to as this;
|
| 61 |
+
to.finished = finished;
|
| 62 |
+
to.destroyed = destroyed;
|
| 63 |
+
to.blockLen = blockLen;
|
| 64 |
+
to.outputLen = outputLen;
|
| 65 |
+
to.oHash = oHash._cloneInto(to.oHash);
|
| 66 |
+
to.iHash = iHash._cloneInto(to.iHash);
|
| 67 |
+
return to;
|
| 68 |
+
}
|
| 69 |
+
clone(): HMAC<T> {
|
| 70 |
+
return this._cloneInto();
|
| 71 |
+
}
|
| 72 |
+
destroy(): void {
|
| 73 |
+
this.destroyed = true;
|
| 74 |
+
this.oHash.destroy();
|
| 75 |
+
this.iHash.destroy();
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
+
|
| 79 |
+
/**
|
| 80 |
+
* HMAC: RFC2104 message authentication code.
|
| 81 |
+
* @param hash - function that would be used e.g. sha256
|
| 82 |
+
* @param key - message key
|
| 83 |
+
* @param message - message data
|
| 84 |
+
* @example
|
| 85 |
+
* import { hmac } from '@noble/hashes/hmac';
|
| 86 |
+
* import { sha256 } from '@noble/hashes/sha2';
|
| 87 |
+
* const mac1 = hmac(sha256, 'key', 'message');
|
| 88 |
+
*/
|
| 89 |
+
export const hmac: {
|
| 90 |
+
(hash: CHash, key: Input, message: Input): Uint8Array;
|
| 91 |
+
create(hash: CHash, key: Input): HMAC<any>;
|
| 92 |
+
} = (hash: CHash, key: Input, message: Input): Uint8Array =>
|
| 93 |
+
new HMAC<any>(hash, key).update(message).digest();
|
| 94 |
+
hmac.create = (hash: CHash, key: Input) => new HMAC<any>(hash, key);
|
node_modules/@noble/hashes/src/index.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Audited & minimal JS implementation of hash functions, MACs and KDFs. Check out individual modules.
|
| 3 |
+
* @module
|
| 4 |
+
* @example
|
| 5 |
+
```js
|
| 6 |
+
import {
|
| 7 |
+
sha256, sha384, sha512, sha224, sha512_224, sha512_256
|
| 8 |
+
} from '@noble/hashes/sha2';
|
| 9 |
+
import {
|
| 10 |
+
sha3_224, sha3_256, sha3_384, sha3_512,
|
| 11 |
+
keccak_224, keccak_256, keccak_384, keccak_512,
|
| 12 |
+
shake128, shake256
|
| 13 |
+
} from '@noble/hashes/sha3';
|
| 14 |
+
import {
|
| 15 |
+
cshake128, cshake256,
|
| 16 |
+
turboshake128, turboshake256,
|
| 17 |
+
kmac128, kmac256,
|
| 18 |
+
tuplehash256, parallelhash256,
|
| 19 |
+
k12, m14, keccakprg
|
| 20 |
+
} from '@noble/hashes/sha3-addons';
|
| 21 |
+
import { blake3 } from '@noble/hashes/blake3';
|
| 22 |
+
import { blake2b, blake2s } from '@noble/hashes/blake2';
|
| 23 |
+
import { hmac } from '@noble/hashes/hmac';
|
| 24 |
+
import { hkdf } from '@noble/hashes/hkdf';
|
| 25 |
+
import { pbkdf2, pbkdf2Async } from '@noble/hashes/pbkdf2';
|
| 26 |
+
import { scrypt, scryptAsync } from '@noble/hashes/scrypt';
|
| 27 |
+
import { md5, ripemd160, sha1 } from '@noble/hashes/legacy';
|
| 28 |
+
import * as utils from '@noble/hashes/utils';
|
| 29 |
+
```
|
| 30 |
+
*/
|
| 31 |
+
throw new Error('root module cannot be imported: import submodules instead. Check out README');
|
node_modules/@noble/hashes/src/legacy.ts
ADDED
|
@@ -0,0 +1,293 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 4 |
+
Don't use them in a new protocol. What "weak" means:
|
| 5 |
+
|
| 6 |
+
- Collisions can be made with 2^18 effort in MD5, 2^60 in SHA1, 2^80 in RIPEMD160.
|
| 7 |
+
- No practical pre-image attacks (only theoretical, 2^123.4)
|
| 8 |
+
- HMAC seems kinda ok: https://datatracker.ietf.org/doc/html/rfc6151
|
| 9 |
+
* @module
|
| 10 |
+
*/
|
| 11 |
+
import { Chi, HashMD, Maj } from './_md.ts';
|
| 12 |
+
import { type CHash, clean, createHasher, rotl } from './utils.ts';
|
| 13 |
+
|
| 14 |
+
/** Initial SHA1 state */
|
| 15 |
+
const SHA1_IV = /* @__PURE__ */ Uint32Array.from([
|
| 16 |
+
0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0,
|
| 17 |
+
]);
|
| 18 |
+
|
| 19 |
+
// Reusable temporary buffer
|
| 20 |
+
const SHA1_W = /* @__PURE__ */ new Uint32Array(80);
|
| 21 |
+
|
| 22 |
+
/** SHA1 legacy hash class. */
|
| 23 |
+
export class SHA1 extends HashMD<SHA1> {
|
| 24 |
+
private A = SHA1_IV[0] | 0;
|
| 25 |
+
private B = SHA1_IV[1] | 0;
|
| 26 |
+
private C = SHA1_IV[2] | 0;
|
| 27 |
+
private D = SHA1_IV[3] | 0;
|
| 28 |
+
private E = SHA1_IV[4] | 0;
|
| 29 |
+
|
| 30 |
+
constructor() {
|
| 31 |
+
super(64, 20, 8, false);
|
| 32 |
+
}
|
| 33 |
+
protected get(): [number, number, number, number, number] {
|
| 34 |
+
const { A, B, C, D, E } = this;
|
| 35 |
+
return [A, B, C, D, E];
|
| 36 |
+
}
|
| 37 |
+
protected set(A: number, B: number, C: number, D: number, E: number): void {
|
| 38 |
+
this.A = A | 0;
|
| 39 |
+
this.B = B | 0;
|
| 40 |
+
this.C = C | 0;
|
| 41 |
+
this.D = D | 0;
|
| 42 |
+
this.E = E | 0;
|
| 43 |
+
}
|
| 44 |
+
protected process(view: DataView, offset: number): void {
|
| 45 |
+
for (let i = 0; i < 16; i++, offset += 4) SHA1_W[i] = view.getUint32(offset, false);
|
| 46 |
+
for (let i = 16; i < 80; i++)
|
| 47 |
+
SHA1_W[i] = rotl(SHA1_W[i - 3] ^ SHA1_W[i - 8] ^ SHA1_W[i - 14] ^ SHA1_W[i - 16], 1);
|
| 48 |
+
// Compression function main loop, 80 rounds
|
| 49 |
+
let { A, B, C, D, E } = this;
|
| 50 |
+
for (let i = 0; i < 80; i++) {
|
| 51 |
+
let F, K;
|
| 52 |
+
if (i < 20) {
|
| 53 |
+
F = Chi(B, C, D);
|
| 54 |
+
K = 0x5a827999;
|
| 55 |
+
} else if (i < 40) {
|
| 56 |
+
F = B ^ C ^ D;
|
| 57 |
+
K = 0x6ed9eba1;
|
| 58 |
+
} else if (i < 60) {
|
| 59 |
+
F = Maj(B, C, D);
|
| 60 |
+
K = 0x8f1bbcdc;
|
| 61 |
+
} else {
|
| 62 |
+
F = B ^ C ^ D;
|
| 63 |
+
K = 0xca62c1d6;
|
| 64 |
+
}
|
| 65 |
+
const T = (rotl(A, 5) + F + E + K + SHA1_W[i]) | 0;
|
| 66 |
+
E = D;
|
| 67 |
+
D = C;
|
| 68 |
+
C = rotl(B, 30);
|
| 69 |
+
B = A;
|
| 70 |
+
A = T;
|
| 71 |
+
}
|
| 72 |
+
// Add the compressed chunk to the current hash value
|
| 73 |
+
A = (A + this.A) | 0;
|
| 74 |
+
B = (B + this.B) | 0;
|
| 75 |
+
C = (C + this.C) | 0;
|
| 76 |
+
D = (D + this.D) | 0;
|
| 77 |
+
E = (E + this.E) | 0;
|
| 78 |
+
this.set(A, B, C, D, E);
|
| 79 |
+
}
|
| 80 |
+
protected roundClean(): void {
|
| 81 |
+
clean(SHA1_W);
|
| 82 |
+
}
|
| 83 |
+
destroy(): void {
|
| 84 |
+
this.set(0, 0, 0, 0, 0);
|
| 85 |
+
clean(this.buffer);
|
| 86 |
+
}
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
/** SHA1 (RFC 3174) legacy hash function. It was cryptographically broken. */
|
| 90 |
+
export const sha1: CHash = /* @__PURE__ */ createHasher(() => new SHA1());
|
| 91 |
+
|
| 92 |
+
/** Per-round constants */
|
| 93 |
+
const p32 = /* @__PURE__ */ Math.pow(2, 32);
|
| 94 |
+
const K = /* @__PURE__ */ Array.from({ length: 64 }, (_, i) =>
|
| 95 |
+
Math.floor(p32 * Math.abs(Math.sin(i + 1)))
|
| 96 |
+
);
|
| 97 |
+
|
| 98 |
+
/** md5 initial state: same as sha1, but 4 u32 instead of 5. */
|
| 99 |
+
const MD5_IV = /* @__PURE__ */ SHA1_IV.slice(0, 4);
|
| 100 |
+
|
| 101 |
+
// Reusable temporary buffer
|
| 102 |
+
const MD5_W = /* @__PURE__ */ new Uint32Array(16);
|
| 103 |
+
/** MD5 legacy hash class. */
|
| 104 |
+
export class MD5 extends HashMD<MD5> {
|
| 105 |
+
private A = MD5_IV[0] | 0;
|
| 106 |
+
private B = MD5_IV[1] | 0;
|
| 107 |
+
private C = MD5_IV[2] | 0;
|
| 108 |
+
private D = MD5_IV[3] | 0;
|
| 109 |
+
|
| 110 |
+
constructor() {
|
| 111 |
+
super(64, 16, 8, true);
|
| 112 |
+
}
|
| 113 |
+
protected get(): [number, number, number, number] {
|
| 114 |
+
const { A, B, C, D } = this;
|
| 115 |
+
return [A, B, C, D];
|
| 116 |
+
}
|
| 117 |
+
protected set(A: number, B: number, C: number, D: number): void {
|
| 118 |
+
this.A = A | 0;
|
| 119 |
+
this.B = B | 0;
|
| 120 |
+
this.C = C | 0;
|
| 121 |
+
this.D = D | 0;
|
| 122 |
+
}
|
| 123 |
+
protected process(view: DataView, offset: number): void {
|
| 124 |
+
for (let i = 0; i < 16; i++, offset += 4) MD5_W[i] = view.getUint32(offset, true);
|
| 125 |
+
// Compression function main loop, 64 rounds
|
| 126 |
+
let { A, B, C, D } = this;
|
| 127 |
+
for (let i = 0; i < 64; i++) {
|
| 128 |
+
let F, g, s;
|
| 129 |
+
if (i < 16) {
|
| 130 |
+
F = Chi(B, C, D);
|
| 131 |
+
g = i;
|
| 132 |
+
s = [7, 12, 17, 22];
|
| 133 |
+
} else if (i < 32) {
|
| 134 |
+
F = Chi(D, B, C);
|
| 135 |
+
g = (5 * i + 1) % 16;
|
| 136 |
+
s = [5, 9, 14, 20];
|
| 137 |
+
} else if (i < 48) {
|
| 138 |
+
F = B ^ C ^ D;
|
| 139 |
+
g = (3 * i + 5) % 16;
|
| 140 |
+
s = [4, 11, 16, 23];
|
| 141 |
+
} else {
|
| 142 |
+
F = C ^ (B | ~D);
|
| 143 |
+
g = (7 * i) % 16;
|
| 144 |
+
s = [6, 10, 15, 21];
|
| 145 |
+
}
|
| 146 |
+
F = F + A + K[i] + MD5_W[g];
|
| 147 |
+
A = D;
|
| 148 |
+
D = C;
|
| 149 |
+
C = B;
|
| 150 |
+
B = B + rotl(F, s[i % 4]);
|
| 151 |
+
}
|
| 152 |
+
// Add the compressed chunk to the current hash value
|
| 153 |
+
A = (A + this.A) | 0;
|
| 154 |
+
B = (B + this.B) | 0;
|
| 155 |
+
C = (C + this.C) | 0;
|
| 156 |
+
D = (D + this.D) | 0;
|
| 157 |
+
this.set(A, B, C, D);
|
| 158 |
+
}
|
| 159 |
+
protected roundClean(): void {
|
| 160 |
+
clean(MD5_W);
|
| 161 |
+
}
|
| 162 |
+
destroy(): void {
|
| 163 |
+
this.set(0, 0, 0, 0);
|
| 164 |
+
clean(this.buffer);
|
| 165 |
+
}
|
| 166 |
+
}
|
| 167 |
+
|
| 168 |
+
/**
|
| 169 |
+
* MD5 (RFC 1321) legacy hash function. It was cryptographically broken.
|
| 170 |
+
* MD5 architecture is similar to SHA1, with some differences:
|
| 171 |
+
* - Reduced output length: 16 bytes (128 bit) instead of 20
|
| 172 |
+
* - 64 rounds, instead of 80
|
| 173 |
+
* - Little-endian: could be faster, but will require more code
|
| 174 |
+
* - Non-linear index selection: huge speed-up for unroll
|
| 175 |
+
* - Per round constants: more memory accesses, additional speed-up for unroll
|
| 176 |
+
*/
|
| 177 |
+
export const md5: CHash = /* @__PURE__ */ createHasher(() => new MD5());
|
| 178 |
+
|
| 179 |
+
// RIPEMD-160
|
| 180 |
+
|
| 181 |
+
const Rho160 = /* @__PURE__ */ Uint8Array.from([
|
| 182 |
+
7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
|
| 183 |
+
]);
|
| 184 |
+
const Id160 = /* @__PURE__ */ (() => Uint8Array.from(new Array(16).fill(0).map((_, i) => i)))();
|
| 185 |
+
const Pi160 = /* @__PURE__ */ (() => Id160.map((i) => (9 * i + 5) % 16))();
|
| 186 |
+
const idxLR = /* @__PURE__ */ (() => {
|
| 187 |
+
const L = [Id160];
|
| 188 |
+
const R = [Pi160];
|
| 189 |
+
const res = [L, R];
|
| 190 |
+
for (let i = 0; i < 4; i++) for (let j of res) j.push(j[i].map((k) => Rho160[k]));
|
| 191 |
+
return res;
|
| 192 |
+
})();
|
| 193 |
+
const idxL = /* @__PURE__ */ (() => idxLR[0])();
|
| 194 |
+
const idxR = /* @__PURE__ */ (() => idxLR[1])();
|
| 195 |
+
// const [idxL, idxR] = idxLR;
|
| 196 |
+
|
| 197 |
+
const shifts160 = /* @__PURE__ */ [
|
| 198 |
+
[11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8],
|
| 199 |
+
[12, 13, 11, 15, 6, 9, 9, 7, 12, 15, 11, 13, 7, 8, 7, 7],
|
| 200 |
+
[13, 15, 14, 11, 7, 7, 6, 8, 13, 14, 13, 12, 5, 5, 6, 9],
|
| 201 |
+
[14, 11, 12, 14, 8, 6, 5, 5, 15, 12, 15, 14, 9, 9, 8, 6],
|
| 202 |
+
[15, 12, 13, 13, 9, 5, 8, 6, 14, 11, 12, 11, 8, 6, 5, 5],
|
| 203 |
+
].map((i) => Uint8Array.from(i));
|
| 204 |
+
const shiftsL160 = /* @__PURE__ */ idxL.map((idx, i) => idx.map((j) => shifts160[i][j]));
|
| 205 |
+
const shiftsR160 = /* @__PURE__ */ idxR.map((idx, i) => idx.map((j) => shifts160[i][j]));
|
| 206 |
+
const Kl160 = /* @__PURE__ */ Uint32Array.from([
|
| 207 |
+
0x00000000, 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xa953fd4e,
|
| 208 |
+
]);
|
| 209 |
+
const Kr160 = /* @__PURE__ */ Uint32Array.from([
|
| 210 |
+
0x50a28be6, 0x5c4dd124, 0x6d703ef3, 0x7a6d76e9, 0x00000000,
|
| 211 |
+
]);
|
| 212 |
+
// It's called f() in spec.
|
| 213 |
+
function ripemd_f(group: number, x: number, y: number, z: number): number {
|
| 214 |
+
if (group === 0) return x ^ y ^ z;
|
| 215 |
+
if (group === 1) return (x & y) | (~x & z);
|
| 216 |
+
if (group === 2) return (x | ~y) ^ z;
|
| 217 |
+
if (group === 3) return (x & z) | (y & ~z);
|
| 218 |
+
return x ^ (y | ~z);
|
| 219 |
+
}
|
| 220 |
+
// Reusable temporary buffer
|
| 221 |
+
const BUF_160 = /* @__PURE__ */ new Uint32Array(16);
|
| 222 |
+
export class RIPEMD160 extends HashMD<RIPEMD160> {
|
| 223 |
+
private h0 = 0x67452301 | 0;
|
| 224 |
+
private h1 = 0xefcdab89 | 0;
|
| 225 |
+
private h2 = 0x98badcfe | 0;
|
| 226 |
+
private h3 = 0x10325476 | 0;
|
| 227 |
+
private h4 = 0xc3d2e1f0 | 0;
|
| 228 |
+
|
| 229 |
+
constructor() {
|
| 230 |
+
super(64, 20, 8, true);
|
| 231 |
+
}
|
| 232 |
+
protected get(): [number, number, number, number, number] {
|
| 233 |
+
const { h0, h1, h2, h3, h4 } = this;
|
| 234 |
+
return [h0, h1, h2, h3, h4];
|
| 235 |
+
}
|
| 236 |
+
protected set(h0: number, h1: number, h2: number, h3: number, h4: number): void {
|
| 237 |
+
this.h0 = h0 | 0;
|
| 238 |
+
this.h1 = h1 | 0;
|
| 239 |
+
this.h2 = h2 | 0;
|
| 240 |
+
this.h3 = h3 | 0;
|
| 241 |
+
this.h4 = h4 | 0;
|
| 242 |
+
}
|
| 243 |
+
protected process(view: DataView, offset: number): void {
|
| 244 |
+
for (let i = 0; i < 16; i++, offset += 4) BUF_160[i] = view.getUint32(offset, true);
|
| 245 |
+
// prettier-ignore
|
| 246 |
+
let al = this.h0 | 0, ar = al,
|
| 247 |
+
bl = this.h1 | 0, br = bl,
|
| 248 |
+
cl = this.h2 | 0, cr = cl,
|
| 249 |
+
dl = this.h3 | 0, dr = dl,
|
| 250 |
+
el = this.h4 | 0, er = el;
|
| 251 |
+
|
| 252 |
+
// Instead of iterating 0 to 80, we split it into 5 groups
|
| 253 |
+
// And use the groups in constants, functions, etc. Much simpler
|
| 254 |
+
for (let group = 0; group < 5; group++) {
|
| 255 |
+
const rGroup = 4 - group;
|
| 256 |
+
const hbl = Kl160[group], hbr = Kr160[group]; // prettier-ignore
|
| 257 |
+
const rl = idxL[group], rr = idxR[group]; // prettier-ignore
|
| 258 |
+
const sl = shiftsL160[group], sr = shiftsR160[group]; // prettier-ignore
|
| 259 |
+
for (let i = 0; i < 16; i++) {
|
| 260 |
+
const tl = (rotl(al + ripemd_f(group, bl, cl, dl) + BUF_160[rl[i]] + hbl, sl[i]) + el) | 0;
|
| 261 |
+
al = el, el = dl, dl = rotl(cl, 10) | 0, cl = bl, bl = tl; // prettier-ignore
|
| 262 |
+
}
|
| 263 |
+
// 2 loops are 10% faster
|
| 264 |
+
for (let i = 0; i < 16; i++) {
|
| 265 |
+
const tr = (rotl(ar + ripemd_f(rGroup, br, cr, dr) + BUF_160[rr[i]] + hbr, sr[i]) + er) | 0;
|
| 266 |
+
ar = er, er = dr, dr = rotl(cr, 10) | 0, cr = br, br = tr; // prettier-ignore
|
| 267 |
+
}
|
| 268 |
+
}
|
| 269 |
+
// Add the compressed chunk to the current hash value
|
| 270 |
+
this.set(
|
| 271 |
+
(this.h1 + cl + dr) | 0,
|
| 272 |
+
(this.h2 + dl + er) | 0,
|
| 273 |
+
(this.h3 + el + ar) | 0,
|
| 274 |
+
(this.h4 + al + br) | 0,
|
| 275 |
+
(this.h0 + bl + cr) | 0
|
| 276 |
+
);
|
| 277 |
+
}
|
| 278 |
+
protected roundClean(): void {
|
| 279 |
+
clean(BUF_160);
|
| 280 |
+
}
|
| 281 |
+
destroy(): void {
|
| 282 |
+
this.destroyed = true;
|
| 283 |
+
clean(this.buffer);
|
| 284 |
+
this.set(0, 0, 0, 0, 0);
|
| 285 |
+
}
|
| 286 |
+
}
|
| 287 |
+
|
| 288 |
+
/**
|
| 289 |
+
* RIPEMD-160 - a legacy hash function from 1990s.
|
| 290 |
+
* * https://homes.esat.kuleuven.be/~bosselae/ripemd160.html
|
| 291 |
+
* * https://homes.esat.kuleuven.be/~bosselae/ripemd160/pdf/AB-9601/AB-9601.pdf
|
| 292 |
+
*/
|
| 293 |
+
export const ripemd160: CHash = /* @__PURE__ */ createHasher(() => new RIPEMD160());
|
node_modules/@noble/hashes/src/pbkdf2.ts
ADDED
|
@@ -0,0 +1,122 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* PBKDF (RFC 2898). Can be used to create a key from password and salt.
|
| 3 |
+
* @module
|
| 4 |
+
*/
|
| 5 |
+
import { hmac } from './hmac.ts';
|
| 6 |
+
// prettier-ignore
|
| 7 |
+
import {
|
| 8 |
+
ahash, anumber,
|
| 9 |
+
asyncLoop, checkOpts, clean, createView, Hash, kdfInputToBytes,
|
| 10 |
+
type CHash,
|
| 11 |
+
type KDFInput
|
| 12 |
+
} from './utils.ts';
|
| 13 |
+
|
| 14 |
+
export type Pbkdf2Opt = {
|
| 15 |
+
c: number; // Iterations
|
| 16 |
+
dkLen?: number; // Desired key length in bytes (Intended output length in octets of the derived key
|
| 17 |
+
asyncTick?: number; // Maximum time in ms for which async function can block execution
|
| 18 |
+
};
|
| 19 |
+
// Common prologue and epilogue for sync/async functions
|
| 20 |
+
function pbkdf2Init(hash: CHash, _password: KDFInput, _salt: KDFInput, _opts: Pbkdf2Opt) {
|
| 21 |
+
ahash(hash);
|
| 22 |
+
const opts = checkOpts({ dkLen: 32, asyncTick: 10 }, _opts);
|
| 23 |
+
const { c, dkLen, asyncTick } = opts;
|
| 24 |
+
anumber(c);
|
| 25 |
+
anumber(dkLen);
|
| 26 |
+
anumber(asyncTick);
|
| 27 |
+
if (c < 1) throw new Error('iterations (c) should be >= 1');
|
| 28 |
+
const password = kdfInputToBytes(_password);
|
| 29 |
+
const salt = kdfInputToBytes(_salt);
|
| 30 |
+
// DK = PBKDF2(PRF, Password, Salt, c, dkLen);
|
| 31 |
+
const DK = new Uint8Array(dkLen);
|
| 32 |
+
// U1 = PRF(Password, Salt + INT_32_BE(i))
|
| 33 |
+
const PRF = hmac.create(hash, password);
|
| 34 |
+
const PRFSalt = PRF._cloneInto().update(salt);
|
| 35 |
+
return { c, dkLen, asyncTick, DK, PRF, PRFSalt };
|
| 36 |
+
}
|
| 37 |
+
|
| 38 |
+
function pbkdf2Output<T extends Hash<T>>(
|
| 39 |
+
PRF: Hash<T>,
|
| 40 |
+
PRFSalt: Hash<T>,
|
| 41 |
+
DK: Uint8Array,
|
| 42 |
+
prfW: Hash<T>,
|
| 43 |
+
u: Uint8Array
|
| 44 |
+
) {
|
| 45 |
+
PRF.destroy();
|
| 46 |
+
PRFSalt.destroy();
|
| 47 |
+
if (prfW) prfW.destroy();
|
| 48 |
+
clean(u);
|
| 49 |
+
return DK;
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
/**
|
| 53 |
+
* PBKDF2-HMAC: RFC 2898 key derivation function
|
| 54 |
+
* @param hash - hash function that would be used e.g. sha256
|
| 55 |
+
* @param password - password from which a derived key is generated
|
| 56 |
+
* @param salt - cryptographic salt
|
| 57 |
+
* @param opts - {c, dkLen} where c is work factor and dkLen is output message size
|
| 58 |
+
* @example
|
| 59 |
+
* const key = pbkdf2(sha256, 'password', 'salt', { dkLen: 32, c: Math.pow(2, 18) });
|
| 60 |
+
*/
|
| 61 |
+
export function pbkdf2(
|
| 62 |
+
hash: CHash,
|
| 63 |
+
password: KDFInput,
|
| 64 |
+
salt: KDFInput,
|
| 65 |
+
opts: Pbkdf2Opt
|
| 66 |
+
): Uint8Array {
|
| 67 |
+
const { c, dkLen, DK, PRF, PRFSalt } = pbkdf2Init(hash, password, salt, opts);
|
| 68 |
+
let prfW: any; // Working copy
|
| 69 |
+
const arr = new Uint8Array(4);
|
| 70 |
+
const view = createView(arr);
|
| 71 |
+
const u = new Uint8Array(PRF.outputLen);
|
| 72 |
+
// DK = T1 + T2 + ⋯ + Tdklen/hlen
|
| 73 |
+
for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += PRF.outputLen) {
|
| 74 |
+
// Ti = F(Password, Salt, c, i)
|
| 75 |
+
const Ti = DK.subarray(pos, pos + PRF.outputLen);
|
| 76 |
+
view.setInt32(0, ti, false);
|
| 77 |
+
// F(Password, Salt, c, i) = U1 ^ U2 ^ ⋯ ^ Uc
|
| 78 |
+
// U1 = PRF(Password, Salt + INT_32_BE(i))
|
| 79 |
+
(prfW = PRFSalt._cloneInto(prfW)).update(arr).digestInto(u);
|
| 80 |
+
Ti.set(u.subarray(0, Ti.length));
|
| 81 |
+
for (let ui = 1; ui < c; ui++) {
|
| 82 |
+
// Uc = PRF(Password, Uc−1)
|
| 83 |
+
PRF._cloneInto(prfW).update(u).digestInto(u);
|
| 84 |
+
for (let i = 0; i < Ti.length; i++) Ti[i] ^= u[i];
|
| 85 |
+
}
|
| 86 |
+
}
|
| 87 |
+
return pbkdf2Output(PRF, PRFSalt, DK, prfW, u);
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
/**
|
| 91 |
+
* PBKDF2-HMAC: RFC 2898 key derivation function. Async version.
|
| 92 |
+
* @example
|
| 93 |
+
* await pbkdf2Async(sha256, 'password', 'salt', { dkLen: 32, c: 500_000 });
|
| 94 |
+
*/
|
| 95 |
+
export async function pbkdf2Async(
|
| 96 |
+
hash: CHash,
|
| 97 |
+
password: KDFInput,
|
| 98 |
+
salt: KDFInput,
|
| 99 |
+
opts: Pbkdf2Opt
|
| 100 |
+
): Promise<Uint8Array> {
|
| 101 |
+
const { c, dkLen, asyncTick, DK, PRF, PRFSalt } = pbkdf2Init(hash, password, salt, opts);
|
| 102 |
+
let prfW: any; // Working copy
|
| 103 |
+
const arr = new Uint8Array(4);
|
| 104 |
+
const view = createView(arr);
|
| 105 |
+
const u = new Uint8Array(PRF.outputLen);
|
| 106 |
+
// DK = T1 + T2 + ⋯ + Tdklen/hlen
|
| 107 |
+
for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += PRF.outputLen) {
|
| 108 |
+
// Ti = F(Password, Salt, c, i)
|
| 109 |
+
const Ti = DK.subarray(pos, pos + PRF.outputLen);
|
| 110 |
+
view.setInt32(0, ti, false);
|
| 111 |
+
// F(Password, Salt, c, i) = U1 ^ U2 ^ ⋯ ^ Uc
|
| 112 |
+
// U1 = PRF(Password, Salt + INT_32_BE(i))
|
| 113 |
+
(prfW = PRFSalt._cloneInto(prfW)).update(arr).digestInto(u);
|
| 114 |
+
Ti.set(u.subarray(0, Ti.length));
|
| 115 |
+
await asyncLoop(c - 1, asyncTick, () => {
|
| 116 |
+
// Uc = PRF(Password, Uc−1)
|
| 117 |
+
PRF._cloneInto(prfW).update(u).digestInto(u);
|
| 118 |
+
for (let i = 0; i < Ti.length; i++) Ti[i] ^= u[i];
|
| 119 |
+
});
|
| 120 |
+
}
|
| 121 |
+
return pbkdf2Output(PRF, PRFSalt, DK, prfW, u);
|
| 122 |
+
}
|
node_modules/@noble/hashes/src/ripemd160.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* RIPEMD-160 legacy hash function.
|
| 3 |
+
* https://homes.esat.kuleuven.be/~bosselae/ripemd160.html
|
| 4 |
+
* https://homes.esat.kuleuven.be/~bosselae/ripemd160/pdf/AB-9601/AB-9601.pdf
|
| 5 |
+
* @module
|
| 6 |
+
* @deprecated
|
| 7 |
+
*/
|
| 8 |
+
import { RIPEMD160 as RIPEMD160n, ripemd160 as ripemd160n } from './legacy.ts';
|
| 9 |
+
/** @deprecated Use import from `noble/hashes/legacy` module */
|
| 10 |
+
export const RIPEMD160: typeof RIPEMD160n = RIPEMD160n;
|
| 11 |
+
/** @deprecated Use import from `noble/hashes/legacy` module */
|
| 12 |
+
export const ripemd160: typeof ripemd160n = ripemd160n;
|
node_modules/@noble/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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* RFC 7914 Scrypt KDF. Can be used to create a key from password and salt.
|
| 3 |
+
* @module
|
| 4 |
+
*/
|
| 5 |
+
import { pbkdf2 } from './pbkdf2.ts';
|
| 6 |
+
import { sha256 } from './sha2.ts';
|
| 7 |
+
// prettier-ignore
|
| 8 |
+
import {
|
| 9 |
+
anumber, asyncLoop,
|
| 10 |
+
checkOpts, clean,
|
| 11 |
+
type KDFInput, rotl,
|
| 12 |
+
swap32IfBE,
|
| 13 |
+
u32
|
| 14 |
+
} from './utils.ts';
|
| 15 |
+
|
| 16 |
+
// The main Scrypt loop: uses Salsa extensively.
|
| 17 |
+
// Six versions of the function were tried, this is the fastest one.
|
| 18 |
+
// prettier-ignore
|
| 19 |
+
function XorAndSalsa(
|
| 20 |
+
prev: Uint32Array,
|
| 21 |
+
pi: number,
|
| 22 |
+
input: Uint32Array,
|
| 23 |
+
ii: number,
|
| 24 |
+
out: Uint32Array,
|
| 25 |
+
oi: number
|
| 26 |
+
) {
|
| 27 |
+
// Based on https://cr.yp.to/salsa20.html
|
| 28 |
+
// Xor blocks
|
| 29 |
+
let y00 = prev[pi++] ^ input[ii++], y01 = prev[pi++] ^ input[ii++];
|
| 30 |
+
let y02 = prev[pi++] ^ input[ii++], y03 = prev[pi++] ^ input[ii++];
|
| 31 |
+
let y04 = prev[pi++] ^ input[ii++], y05 = prev[pi++] ^ input[ii++];
|
| 32 |
+
let y06 = prev[pi++] ^ input[ii++], y07 = prev[pi++] ^ input[ii++];
|
| 33 |
+
let y08 = prev[pi++] ^ input[ii++], y09 = prev[pi++] ^ input[ii++];
|
| 34 |
+
let y10 = prev[pi++] ^ input[ii++], y11 = prev[pi++] ^ input[ii++];
|
| 35 |
+
let y12 = prev[pi++] ^ input[ii++], y13 = prev[pi++] ^ input[ii++];
|
| 36 |
+
let y14 = prev[pi++] ^ input[ii++], y15 = prev[pi++] ^ input[ii++];
|
| 37 |
+
// Save state to temporary variables (salsa)
|
| 38 |
+
let x00 = y00, x01 = y01, x02 = y02, x03 = y03,
|
| 39 |
+
x04 = y04, x05 = y05, x06 = y06, x07 = y07,
|
| 40 |
+
x08 = y08, x09 = y09, x10 = y10, x11 = y11,
|
| 41 |
+
x12 = y12, x13 = y13, x14 = y14, x15 = y15;
|
| 42 |
+
// Main loop (salsa)
|
| 43 |
+
for (let i = 0; i < 8; i += 2) {
|
| 44 |
+
x04 ^= rotl(x00 + x12 | 0, 7); x08 ^= rotl(x04 + x00 | 0, 9);
|
| 45 |
+
x12 ^= rotl(x08 + x04 | 0, 13); x00 ^= rotl(x12 + x08 | 0, 18);
|
| 46 |
+
x09 ^= rotl(x05 + x01 | 0, 7); x13 ^= rotl(x09 + x05 | 0, 9);
|
| 47 |
+
x01 ^= rotl(x13 + x09 | 0, 13); x05 ^= rotl(x01 + x13 | 0, 18);
|
| 48 |
+
x14 ^= rotl(x10 + x06 | 0, 7); x02 ^= rotl(x14 + x10 | 0, 9);
|
| 49 |
+
x06 ^= rotl(x02 + x14 | 0, 13); x10 ^= rotl(x06 + x02 | 0, 18);
|
| 50 |
+
x03 ^= rotl(x15 + x11 | 0, 7); x07 ^= rotl(x03 + x15 | 0, 9);
|
| 51 |
+
x11 ^= rotl(x07 + x03 | 0, 13); x15 ^= rotl(x11 + x07 | 0, 18);
|
| 52 |
+
x01 ^= rotl(x00 + x03 | 0, 7); x02 ^= rotl(x01 + x00 | 0, 9);
|
| 53 |
+
x03 ^= rotl(x02 + x01 | 0, 13); x00 ^= rotl(x03 + x02 | 0, 18);
|
| 54 |
+
x06 ^= rotl(x05 + x04 | 0, 7); x07 ^= rotl(x06 + x05 | 0, 9);
|
| 55 |
+
x04 ^= rotl(x07 + x06 | 0, 13); x05 ^= rotl(x04 + x07 | 0, 18);
|
| 56 |
+
x11 ^= rotl(x10 + x09 | 0, 7); x08 ^= rotl(x11 + x10 | 0, 9);
|
| 57 |
+
x09 ^= rotl(x08 + x11 | 0, 13); x10 ^= rotl(x09 + x08 | 0, 18);
|
| 58 |
+
x12 ^= rotl(x15 + x14 | 0, 7); x13 ^= rotl(x12 + x15 | 0, 9);
|
| 59 |
+
x14 ^= rotl(x13 + x12 | 0, 13); x15 ^= rotl(x14 + x13 | 0, 18);
|
| 60 |
+
}
|
| 61 |
+
// Write output (salsa)
|
| 62 |
+
out[oi++] = (y00 + x00) | 0; out[oi++] = (y01 + x01) | 0;
|
| 63 |
+
out[oi++] = (y02 + x02) | 0; out[oi++] = (y03 + x03) | 0;
|
| 64 |
+
out[oi++] = (y04 + x04) | 0; out[oi++] = (y05 + x05) | 0;
|
| 65 |
+
out[oi++] = (y06 + x06) | 0; out[oi++] = (y07 + x07) | 0;
|
| 66 |
+
out[oi++] = (y08 + x08) | 0; out[oi++] = (y09 + x09) | 0;
|
| 67 |
+
out[oi++] = (y10 + x10) | 0; out[oi++] = (y11 + x11) | 0;
|
| 68 |
+
out[oi++] = (y12 + x12) | 0; out[oi++] = (y13 + x13) | 0;
|
| 69 |
+
out[oi++] = (y14 + x14) | 0; out[oi++] = (y15 + x15) | 0;
|
| 70 |
+
}
|
| 71 |
+
|
| 72 |
+
function BlockMix(input: Uint32Array, ii: number, out: Uint32Array, oi: number, r: number) {
|
| 73 |
+
// The block B is r 128-byte chunks (which is equivalent of 2r 64-byte chunks)
|
| 74 |
+
let head = oi + 0;
|
| 75 |
+
let tail = oi + 16 * r;
|
| 76 |
+
for (let i = 0; i < 16; i++) out[tail + i] = input[ii + (2 * r - 1) * 16 + i]; // X ← B[2r−1]
|
| 77 |
+
for (let i = 0; i < r; i++, head += 16, ii += 16) {
|
| 78 |
+
// We write odd & even Yi at same time. Even: 0bXXXXX0 Odd: 0bXXXXX1
|
| 79 |
+
XorAndSalsa(out, tail, input, ii, out, head); // head[i] = Salsa(blockIn[2*i] ^ tail[i-1])
|
| 80 |
+
if (i > 0) tail += 16; // First iteration overwrites tmp value in tail
|
| 81 |
+
XorAndSalsa(out, head, input, (ii += 16), out, tail); // tail[i] = Salsa(blockIn[2*i+1] ^ head[i])
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
export type ScryptOpts = {
|
| 86 |
+
N: number; // cost factor
|
| 87 |
+
r: number; // block size
|
| 88 |
+
p: number; // parallelization
|
| 89 |
+
dkLen?: number; // key length
|
| 90 |
+
asyncTick?: number; // block execution max time
|
| 91 |
+
maxmem?: number;
|
| 92 |
+
onProgress?: (progress: number) => void;
|
| 93 |
+
};
|
| 94 |
+
|
| 95 |
+
// Common prologue and epilogue for sync/async functions
|
| 96 |
+
function scryptInit(password: KDFInput, salt: KDFInput, _opts?: ScryptOpts) {
|
| 97 |
+
// Maxmem - 1GB+1KB by default
|
| 98 |
+
const opts = checkOpts(
|
| 99 |
+
{
|
| 100 |
+
dkLen: 32,
|
| 101 |
+
asyncTick: 10,
|
| 102 |
+
maxmem: 1024 ** 3 + 1024,
|
| 103 |
+
},
|
| 104 |
+
_opts
|
| 105 |
+
);
|
| 106 |
+
const { N, r, p, dkLen, asyncTick, maxmem, onProgress } = opts;
|
| 107 |
+
anumber(N);
|
| 108 |
+
anumber(r);
|
| 109 |
+
anumber(p);
|
| 110 |
+
anumber(dkLen);
|
| 111 |
+
anumber(asyncTick);
|
| 112 |
+
anumber(maxmem);
|
| 113 |
+
if (onProgress !== undefined && typeof onProgress !== 'function')
|
| 114 |
+
throw new Error('progressCb should be function');
|
| 115 |
+
const blockSize = 128 * r;
|
| 116 |
+
const blockSize32 = blockSize / 4;
|
| 117 |
+
|
| 118 |
+
// Max N is 2^32 (Integrify is 32-bit). Real limit is 2^22: JS engines Uint8Array limit is 4GB in 2024.
|
| 119 |
+
// Spec check `N >= 2^(blockSize / 8)` is not done for compat with popular libs,
|
| 120 |
+
// which used incorrect r: 1, p: 8. Also, the check seems to be a spec error:
|
| 121 |
+
// https://www.rfc-editor.org/errata_search.php?rfc=7914
|
| 122 |
+
const pow32 = Math.pow(2, 32);
|
| 123 |
+
if (N <= 1 || (N & (N - 1)) !== 0 || N > pow32) {
|
| 124 |
+
throw new Error('Scrypt: N must be larger than 1, a power of 2, and less than 2^32');
|
| 125 |
+
}
|
| 126 |
+
if (p < 0 || p > ((pow32 - 1) * 32) / blockSize) {
|
| 127 |
+
throw new Error(
|
| 128 |
+
'Scrypt: p must be a positive integer less than or equal to ((2^32 - 1) * 32) / (128 * r)'
|
| 129 |
+
);
|
| 130 |
+
}
|
| 131 |
+
if (dkLen < 0 || dkLen > (pow32 - 1) * 32) {
|
| 132 |
+
throw new Error(
|
| 133 |
+
'Scrypt: dkLen should be positive integer less than or equal to (2^32 - 1) * 32'
|
| 134 |
+
);
|
| 135 |
+
}
|
| 136 |
+
const memUsed = blockSize * (N + p);
|
| 137 |
+
if (memUsed > maxmem) {
|
| 138 |
+
throw new Error(
|
| 139 |
+
'Scrypt: memused is bigger than maxMem. Expected 128 * r * (N + p) > maxmem of ' + maxmem
|
| 140 |
+
);
|
| 141 |
+
}
|
| 142 |
+
// [B0...Bp−1] ← PBKDF2HMAC-SHA256(Passphrase, Salt, 1, blockSize*ParallelizationFactor)
|
| 143 |
+
// Since it has only one iteration there is no reason to use async variant
|
| 144 |
+
const B = pbkdf2(sha256, password, salt, { c: 1, dkLen: blockSize * p });
|
| 145 |
+
const B32 = u32(B);
|
| 146 |
+
// Re-used between parallel iterations. Array(iterations) of B
|
| 147 |
+
const V = u32(new Uint8Array(blockSize * N));
|
| 148 |
+
const tmp = u32(new Uint8Array(blockSize));
|
| 149 |
+
let blockMixCb = () => {};
|
| 150 |
+
if (onProgress) {
|
| 151 |
+
const totalBlockMix = 2 * N * p;
|
| 152 |
+
// Invoke callback if progress changes from 10.01 to 10.02
|
| 153 |
+
// Allows to draw smooth progress bar on up to 8K screen
|
| 154 |
+
const callbackPer = Math.max(Math.floor(totalBlockMix / 10000), 1);
|
| 155 |
+
let blockMixCnt = 0;
|
| 156 |
+
blockMixCb = () => {
|
| 157 |
+
blockMixCnt++;
|
| 158 |
+
if (onProgress && (!(blockMixCnt % callbackPer) || blockMixCnt === totalBlockMix))
|
| 159 |
+
onProgress(blockMixCnt / totalBlockMix);
|
| 160 |
+
};
|
| 161 |
+
}
|
| 162 |
+
return { N, r, p, dkLen, blockSize32, V, B32, B, tmp, blockMixCb, asyncTick };
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
function scryptOutput(
|
| 166 |
+
password: KDFInput,
|
| 167 |
+
dkLen: number,
|
| 168 |
+
B: Uint8Array,
|
| 169 |
+
V: Uint32Array,
|
| 170 |
+
tmp: Uint32Array
|
| 171 |
+
) {
|
| 172 |
+
const res = pbkdf2(sha256, password, B, { c: 1, dkLen });
|
| 173 |
+
clean(B, V, tmp);
|
| 174 |
+
return res;
|
| 175 |
+
}
|
| 176 |
+
|
| 177 |
+
/**
|
| 178 |
+
* Scrypt KDF from RFC 7914.
|
| 179 |
+
* @param password - pass
|
| 180 |
+
* @param salt - salt
|
| 181 |
+
* @param opts - parameters
|
| 182 |
+
* - `N` is cpu/mem work factor (power of 2 e.g. 2**18)
|
| 183 |
+
* - `r` is block size (8 is common), fine-tunes sequential memory read size and performance
|
| 184 |
+
* - `p` is parallelization factor (1 is common)
|
| 185 |
+
* - `dkLen` is output key length in bytes e.g. 32.
|
| 186 |
+
* - `asyncTick` - (default: 10) max time in ms for which async function can block execution
|
| 187 |
+
* - `maxmem` - (default: `1024 ** 3 + 1024` aka 1GB+1KB). A limit that the app could use for scrypt
|
| 188 |
+
* - `onProgress` - callback function that would be executed for progress report
|
| 189 |
+
* @returns Derived key
|
| 190 |
+
* @example
|
| 191 |
+
* scrypt('password', 'salt', { N: 2**18, r: 8, p: 1, dkLen: 32 });
|
| 192 |
+
*/
|
| 193 |
+
export function scrypt(password: KDFInput, salt: KDFInput, opts: ScryptOpts): Uint8Array {
|
| 194 |
+
const { N, r, p, dkLen, blockSize32, V, B32, B, tmp, blockMixCb } = scryptInit(
|
| 195 |
+
password,
|
| 196 |
+
salt,
|
| 197 |
+
opts
|
| 198 |
+
);
|
| 199 |
+
swap32IfBE(B32);
|
| 200 |
+
for (let pi = 0; pi < p; pi++) {
|
| 201 |
+
const Pi = blockSize32 * pi;
|
| 202 |
+
for (let i = 0; i < blockSize32; i++) V[i] = B32[Pi + i]; // V[0] = B[i]
|
| 203 |
+
for (let i = 0, pos = 0; i < N - 1; i++) {
|
| 204 |
+
BlockMix(V, pos, V, (pos += blockSize32), r); // V[i] = BlockMix(V[i-1]);
|
| 205 |
+
blockMixCb();
|
| 206 |
+
}
|
| 207 |
+
BlockMix(V, (N - 1) * blockSize32, B32, Pi, r); // Process last element
|
| 208 |
+
blockMixCb();
|
| 209 |
+
for (let i = 0; i < N; i++) {
|
| 210 |
+
// First u32 of the last 64-byte block (u32 is LE)
|
| 211 |
+
const j = B32[Pi + blockSize32 - 16] % N; // j = Integrify(X) % iterations
|
| 212 |
+
for (let k = 0; k < blockSize32; k++) tmp[k] = B32[Pi + k] ^ V[j * blockSize32 + k]; // tmp = B ^ V[j]
|
| 213 |
+
BlockMix(tmp, 0, B32, Pi, r); // B = BlockMix(B ^ V[j])
|
| 214 |
+
blockMixCb();
|
| 215 |
+
}
|
| 216 |
+
}
|
| 217 |
+
swap32IfBE(B32);
|
| 218 |
+
return scryptOutput(password, dkLen, B, V, tmp);
|
| 219 |
+
}
|
| 220 |
+
|
| 221 |
+
/**
|
| 222 |
+
* Scrypt KDF from RFC 7914. Async version.
|
| 223 |
+
* @example
|
| 224 |
+
* await scryptAsync('password', 'salt', { N: 2**18, r: 8, p: 1, dkLen: 32 });
|
| 225 |
+
*/
|
| 226 |
+
export async function scryptAsync(
|
| 227 |
+
password: KDFInput,
|
| 228 |
+
salt: KDFInput,
|
| 229 |
+
opts: ScryptOpts
|
| 230 |
+
): Promise<Uint8Array> {
|
| 231 |
+
const { N, r, p, dkLen, blockSize32, V, B32, B, tmp, blockMixCb, asyncTick } = scryptInit(
|
| 232 |
+
password,
|
| 233 |
+
salt,
|
| 234 |
+
opts
|
| 235 |
+
);
|
| 236 |
+
swap32IfBE(B32);
|
| 237 |
+
for (let pi = 0; pi < p; pi++) {
|
| 238 |
+
const Pi = blockSize32 * pi;
|
| 239 |
+
for (let i = 0; i < blockSize32; i++) V[i] = B32[Pi + i]; // V[0] = B[i]
|
| 240 |
+
let pos = 0;
|
| 241 |
+
await asyncLoop(N - 1, asyncTick, () => {
|
| 242 |
+
BlockMix(V, pos, V, (pos += blockSize32), r); // V[i] = BlockMix(V[i-1]);
|
| 243 |
+
blockMixCb();
|
| 244 |
+
});
|
| 245 |
+
BlockMix(V, (N - 1) * blockSize32, B32, Pi, r); // Process last element
|
| 246 |
+
blockMixCb();
|
| 247 |
+
await asyncLoop(N, asyncTick, () => {
|
| 248 |
+
// First u32 of the last 64-byte block (u32 is LE)
|
| 249 |
+
const j = B32[Pi + blockSize32 - 16] % N; // j = Integrify(X) % iterations
|
| 250 |
+
for (let k = 0; k < blockSize32; k++) tmp[k] = B32[Pi + k] ^ V[j * blockSize32 + k]; // tmp = B ^ V[j]
|
| 251 |
+
BlockMix(tmp, 0, B32, Pi, r); // B = BlockMix(B ^ V[j])
|
| 252 |
+
blockMixCb();
|
| 253 |
+
});
|
| 254 |
+
}
|
| 255 |
+
swap32IfBE(B32);
|
| 256 |
+
return scryptOutput(password, dkLen, B, V, tmp);
|
| 257 |
+
}
|
node_modules/@noble/hashes/src/sha1.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* SHA1 (RFC 3174) legacy hash function.
|
| 3 |
+
* @module
|
| 4 |
+
* @deprecated
|
| 5 |
+
*/
|
| 6 |
+
import { SHA1 as SHA1n, sha1 as sha1n } from './legacy.ts';
|
| 7 |
+
/** @deprecated Use import from `noble/hashes/legacy` module */
|
| 8 |
+
export const SHA1: typeof SHA1n = SHA1n;
|
| 9 |
+
/** @deprecated Use import from `noble/hashes/legacy` module */
|
| 10 |
+
export const sha1: typeof sha1n = sha1n;
|
node_modules/@noble/hashes/src/sha2.ts
ADDED
|
@@ -0,0 +1,402 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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. sha256, sha384, sha512, sha512_224, sha512_256.
|
| 3 |
+
* SHA256 is the fastest hash implementable in JS, even faster than Blake3.
|
| 4 |
+
* Check out [RFC 4634](https://datatracker.ietf.org/doc/html/rfc4634) and
|
| 5 |
+
* [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
|
| 6 |
+
* @module
|
| 7 |
+
*/
|
| 8 |
+
import { Chi, HashMD, Maj, SHA224_IV, SHA256_IV, SHA384_IV, SHA512_IV } from './_md.ts';
|
| 9 |
+
import * as u64 from './_u64.ts';
|
| 10 |
+
import { type CHash, clean, createHasher, rotr } from './utils.ts';
|
| 11 |
+
|
| 12 |
+
/**
|
| 13 |
+
* Round constants:
|
| 14 |
+
* First 32 bits of fractional parts of the cube roots of the first 64 primes 2..311)
|
| 15 |
+
*/
|
| 16 |
+
// prettier-ignore
|
| 17 |
+
const SHA256_K = /* @__PURE__ */ Uint32Array.from([
|
| 18 |
+
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
|
| 19 |
+
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
|
| 20 |
+
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
|
| 21 |
+
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
|
| 22 |
+
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
|
| 23 |
+
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
|
| 24 |
+
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
|
| 25 |
+
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
|
| 26 |
+
]);
|
| 27 |
+
|
| 28 |
+
/** Reusable temporary buffer. "W" comes straight from spec. */
|
| 29 |
+
const SHA256_W = /* @__PURE__ */ new Uint32Array(64);
|
| 30 |
+
export class SHA256 extends HashMD<SHA256> {
|
| 31 |
+
// We cannot use array here since array allows indexing by variable
|
| 32 |
+
// which means optimizer/compiler cannot use registers.
|
| 33 |
+
protected A: number = SHA256_IV[0] | 0;
|
| 34 |
+
protected B: number = SHA256_IV[1] | 0;
|
| 35 |
+
protected C: number = SHA256_IV[2] | 0;
|
| 36 |
+
protected D: number = SHA256_IV[3] | 0;
|
| 37 |
+
protected E: number = SHA256_IV[4] | 0;
|
| 38 |
+
protected F: number = SHA256_IV[5] | 0;
|
| 39 |
+
protected G: number = SHA256_IV[6] | 0;
|
| 40 |
+
protected H: number = SHA256_IV[7] | 0;
|
| 41 |
+
|
| 42 |
+
constructor(outputLen: number = 32) {
|
| 43 |
+
super(64, outputLen, 8, false);
|
| 44 |
+
}
|
| 45 |
+
protected get(): [number, number, number, number, number, number, number, number] {
|
| 46 |
+
const { A, B, C, D, E, F, G, H } = this;
|
| 47 |
+
return [A, B, C, D, E, F, G, H];
|
| 48 |
+
}
|
| 49 |
+
// prettier-ignore
|
| 50 |
+
protected set(
|
| 51 |
+
A: number, B: number, C: number, D: number, E: number, F: number, G: number, H: number
|
| 52 |
+
): void {
|
| 53 |
+
this.A = A | 0;
|
| 54 |
+
this.B = B | 0;
|
| 55 |
+
this.C = C | 0;
|
| 56 |
+
this.D = D | 0;
|
| 57 |
+
this.E = E | 0;
|
| 58 |
+
this.F = F | 0;
|
| 59 |
+
this.G = G | 0;
|
| 60 |
+
this.H = H | 0;
|
| 61 |
+
}
|
| 62 |
+
protected process(view: DataView, offset: number): void {
|
| 63 |
+
// Extend the first 16 words into the remaining 48 words w[16..63] of the message schedule array
|
| 64 |
+
for (let i = 0; i < 16; i++, offset += 4) SHA256_W[i] = view.getUint32(offset, false);
|
| 65 |
+
for (let i = 16; i < 64; i++) {
|
| 66 |
+
const W15 = SHA256_W[i - 15];
|
| 67 |
+
const W2 = SHA256_W[i - 2];
|
| 68 |
+
const s0 = rotr(W15, 7) ^ rotr(W15, 18) ^ (W15 >>> 3);
|
| 69 |
+
const s1 = rotr(W2, 17) ^ rotr(W2, 19) ^ (W2 >>> 10);
|
| 70 |
+
SHA256_W[i] = (s1 + SHA256_W[i - 7] + s0 + SHA256_W[i - 16]) | 0;
|
| 71 |
+
}
|
| 72 |
+
// Compression function main loop, 64 rounds
|
| 73 |
+
let { A, B, C, D, E, F, G, H } = this;
|
| 74 |
+
for (let i = 0; i < 64; i++) {
|
| 75 |
+
const sigma1 = rotr(E, 6) ^ rotr(E, 11) ^ rotr(E, 25);
|
| 76 |
+
const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
|
| 77 |
+
const sigma0 = rotr(A, 2) ^ rotr(A, 13) ^ rotr(A, 22);
|
| 78 |
+
const T2 = (sigma0 + Maj(A, B, C)) | 0;
|
| 79 |
+
H = G;
|
| 80 |
+
G = F;
|
| 81 |
+
F = E;
|
| 82 |
+
E = (D + T1) | 0;
|
| 83 |
+
D = C;
|
| 84 |
+
C = B;
|
| 85 |
+
B = A;
|
| 86 |
+
A = (T1 + T2) | 0;
|
| 87 |
+
}
|
| 88 |
+
// Add the compressed chunk to the current hash value
|
| 89 |
+
A = (A + this.A) | 0;
|
| 90 |
+
B = (B + this.B) | 0;
|
| 91 |
+
C = (C + this.C) | 0;
|
| 92 |
+
D = (D + this.D) | 0;
|
| 93 |
+
E = (E + this.E) | 0;
|
| 94 |
+
F = (F + this.F) | 0;
|
| 95 |
+
G = (G + this.G) | 0;
|
| 96 |
+
H = (H + this.H) | 0;
|
| 97 |
+
this.set(A, B, C, D, E, F, G, H);
|
| 98 |
+
}
|
| 99 |
+
protected roundClean(): void {
|
| 100 |
+
clean(SHA256_W);
|
| 101 |
+
}
|
| 102 |
+
destroy(): void {
|
| 103 |
+
this.set(0, 0, 0, 0, 0, 0, 0, 0);
|
| 104 |
+
clean(this.buffer);
|
| 105 |
+
}
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
export class SHA224 extends SHA256 {
|
| 109 |
+
protected A: number = SHA224_IV[0] | 0;
|
| 110 |
+
protected B: number = SHA224_IV[1] | 0;
|
| 111 |
+
protected C: number = SHA224_IV[2] | 0;
|
| 112 |
+
protected D: number = SHA224_IV[3] | 0;
|
| 113 |
+
protected E: number = SHA224_IV[4] | 0;
|
| 114 |
+
protected F: number = SHA224_IV[5] | 0;
|
| 115 |
+
protected G: number = SHA224_IV[6] | 0;
|
| 116 |
+
protected H: number = SHA224_IV[7] | 0;
|
| 117 |
+
constructor() {
|
| 118 |
+
super(28);
|
| 119 |
+
}
|
| 120 |
+
}
|
| 121 |
+
|
| 122 |
+
// SHA2-512 is slower than sha256 in js because u64 operations are slow.
|
| 123 |
+
|
| 124 |
+
// Round contants
|
| 125 |
+
// First 32 bits of the fractional parts of the cube roots of the first 80 primes 2..409
|
| 126 |
+
// prettier-ignore
|
| 127 |
+
const K512 = /* @__PURE__ */ (() => u64.split([
|
| 128 |
+
'0x428a2f98d728ae22', '0x7137449123ef65cd', '0xb5c0fbcfec4d3b2f', '0xe9b5dba58189dbbc',
|
| 129 |
+
'0x3956c25bf348b538', '0x59f111f1b605d019', '0x923f82a4af194f9b', '0xab1c5ed5da6d8118',
|
| 130 |
+
'0xd807aa98a3030242', '0x12835b0145706fbe', '0x243185be4ee4b28c', '0x550c7dc3d5ffb4e2',
|
| 131 |
+
'0x72be5d74f27b896f', '0x80deb1fe3b1696b1', '0x9bdc06a725c71235', '0xc19bf174cf692694',
|
| 132 |
+
'0xe49b69c19ef14ad2', '0xefbe4786384f25e3', '0x0fc19dc68b8cd5b5', '0x240ca1cc77ac9c65',
|
| 133 |
+
'0x2de92c6f592b0275', '0x4a7484aa6ea6e483', '0x5cb0a9dcbd41fbd4', '0x76f988da831153b5',
|
| 134 |
+
'0x983e5152ee66dfab', '0xa831c66d2db43210', '0xb00327c898fb213f', '0xbf597fc7beef0ee4',
|
| 135 |
+
'0xc6e00bf33da88fc2', '0xd5a79147930aa725', '0x06ca6351e003826f', '0x142929670a0e6e70',
|
| 136 |
+
'0x27b70a8546d22ffc', '0x2e1b21385c26c926', '0x4d2c6dfc5ac42aed', '0x53380d139d95b3df',
|
| 137 |
+
'0x650a73548baf63de', '0x766a0abb3c77b2a8', '0x81c2c92e47edaee6', '0x92722c851482353b',
|
| 138 |
+
'0xa2bfe8a14cf10364', '0xa81a664bbc423001', '0xc24b8b70d0f89791', '0xc76c51a30654be30',
|
| 139 |
+
'0xd192e819d6ef5218', '0xd69906245565a910', '0xf40e35855771202a', '0x106aa07032bbd1b8',
|
| 140 |
+
'0x19a4c116b8d2d0c8', '0x1e376c085141ab53', '0x2748774cdf8eeb99', '0x34b0bcb5e19b48a8',
|
| 141 |
+
'0x391c0cb3c5c95a63', '0x4ed8aa4ae3418acb', '0x5b9cca4f7763e373', '0x682e6ff3d6b2b8a3',
|
| 142 |
+
'0x748f82ee5defb2fc', '0x78a5636f43172f60', '0x84c87814a1f0ab72', '0x8cc702081a6439ec',
|
| 143 |
+
'0x90befffa23631e28', '0xa4506cebde82bde9', '0xbef9a3f7b2c67915', '0xc67178f2e372532b',
|
| 144 |
+
'0xca273eceea26619c', '0xd186b8c721c0c207', '0xeada7dd6cde0eb1e', '0xf57d4f7fee6ed178',
|
| 145 |
+
'0x06f067aa72176fba', '0x0a637dc5a2c898a6', '0x113f9804bef90dae', '0x1b710b35131c471b',
|
| 146 |
+
'0x28db77f523047d84', '0x32caab7b40c72493', '0x3c9ebe0a15c9bebc', '0x431d67c49c100d4c',
|
| 147 |
+
'0x4cc5d4becb3e42b6', '0x597f299cfc657e2a', '0x5fcb6fab3ad6faec', '0x6c44198c4a475817'
|
| 148 |
+
].map(n => BigInt(n))))();
|
| 149 |
+
const SHA512_Kh = /* @__PURE__ */ (() => K512[0])();
|
| 150 |
+
const SHA512_Kl = /* @__PURE__ */ (() => K512[1])();
|
| 151 |
+
|
| 152 |
+
// Reusable temporary buffers
|
| 153 |
+
const SHA512_W_H = /* @__PURE__ */ new Uint32Array(80);
|
| 154 |
+
const SHA512_W_L = /* @__PURE__ */ new Uint32Array(80);
|
| 155 |
+
|
| 156 |
+
export class SHA512 extends HashMD<SHA512> {
|
| 157 |
+
// We cannot use array here since array allows indexing by variable
|
| 158 |
+
// which means optimizer/compiler cannot use registers.
|
| 159 |
+
// h -- high 32 bits, l -- low 32 bits
|
| 160 |
+
protected Ah: number = SHA512_IV[0] | 0;
|
| 161 |
+
protected Al: number = SHA512_IV[1] | 0;
|
| 162 |
+
protected Bh: number = SHA512_IV[2] | 0;
|
| 163 |
+
protected Bl: number = SHA512_IV[3] | 0;
|
| 164 |
+
protected Ch: number = SHA512_IV[4] | 0;
|
| 165 |
+
protected Cl: number = SHA512_IV[5] | 0;
|
| 166 |
+
protected Dh: number = SHA512_IV[6] | 0;
|
| 167 |
+
protected Dl: number = SHA512_IV[7] | 0;
|
| 168 |
+
protected Eh: number = SHA512_IV[8] | 0;
|
| 169 |
+
protected El: number = SHA512_IV[9] | 0;
|
| 170 |
+
protected Fh: number = SHA512_IV[10] | 0;
|
| 171 |
+
protected Fl: number = SHA512_IV[11] | 0;
|
| 172 |
+
protected Gh: number = SHA512_IV[12] | 0;
|
| 173 |
+
protected Gl: number = SHA512_IV[13] | 0;
|
| 174 |
+
protected Hh: number = SHA512_IV[14] | 0;
|
| 175 |
+
protected Hl: number = SHA512_IV[15] | 0;
|
| 176 |
+
|
| 177 |
+
constructor(outputLen: number = 64) {
|
| 178 |
+
super(128, outputLen, 16, false);
|
| 179 |
+
}
|
| 180 |
+
// prettier-ignore
|
| 181 |
+
protected get(): [
|
| 182 |
+
number, number, number, number, number, number, number, number,
|
| 183 |
+
number, number, number, number, number, number, number, number
|
| 184 |
+
] {
|
| 185 |
+
const { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
| 186 |
+
return [Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl];
|
| 187 |
+
}
|
| 188 |
+
// prettier-ignore
|
| 189 |
+
protected set(
|
| 190 |
+
Ah: number, Al: number, Bh: number, Bl: number, Ch: number, Cl: number, Dh: number, Dl: number,
|
| 191 |
+
Eh: number, El: number, Fh: number, Fl: number, Gh: number, Gl: number, Hh: number, Hl: number
|
| 192 |
+
): void {
|
| 193 |
+
this.Ah = Ah | 0;
|
| 194 |
+
this.Al = Al | 0;
|
| 195 |
+
this.Bh = Bh | 0;
|
| 196 |
+
this.Bl = Bl | 0;
|
| 197 |
+
this.Ch = Ch | 0;
|
| 198 |
+
this.Cl = Cl | 0;
|
| 199 |
+
this.Dh = Dh | 0;
|
| 200 |
+
this.Dl = Dl | 0;
|
| 201 |
+
this.Eh = Eh | 0;
|
| 202 |
+
this.El = El | 0;
|
| 203 |
+
this.Fh = Fh | 0;
|
| 204 |
+
this.Fl = Fl | 0;
|
| 205 |
+
this.Gh = Gh | 0;
|
| 206 |
+
this.Gl = Gl | 0;
|
| 207 |
+
this.Hh = Hh | 0;
|
| 208 |
+
this.Hl = Hl | 0;
|
| 209 |
+
}
|
| 210 |
+
protected process(view: DataView, offset: number): void {
|
| 211 |
+
// Extend the first 16 words into the remaining 64 words w[16..79] of the message schedule array
|
| 212 |
+
for (let i = 0; i < 16; i++, offset += 4) {
|
| 213 |
+
SHA512_W_H[i] = view.getUint32(offset);
|
| 214 |
+
SHA512_W_L[i] = view.getUint32((offset += 4));
|
| 215 |
+
}
|
| 216 |
+
for (let i = 16; i < 80; i++) {
|
| 217 |
+
// s0 := (w[i-15] rightrotate 1) xor (w[i-15] rightrotate 8) xor (w[i-15] rightshift 7)
|
| 218 |
+
const W15h = SHA512_W_H[i - 15] | 0;
|
| 219 |
+
const W15l = SHA512_W_L[i - 15] | 0;
|
| 220 |
+
const s0h = u64.rotrSH(W15h, W15l, 1) ^ u64.rotrSH(W15h, W15l, 8) ^ u64.shrSH(W15h, W15l, 7);
|
| 221 |
+
const s0l = u64.rotrSL(W15h, W15l, 1) ^ u64.rotrSL(W15h, W15l, 8) ^ u64.shrSL(W15h, W15l, 7);
|
| 222 |
+
// s1 := (w[i-2] rightrotate 19) xor (w[i-2] rightrotate 61) xor (w[i-2] rightshift 6)
|
| 223 |
+
const W2h = SHA512_W_H[i - 2] | 0;
|
| 224 |
+
const W2l = SHA512_W_L[i - 2] | 0;
|
| 225 |
+
const s1h = u64.rotrSH(W2h, W2l, 19) ^ u64.rotrBH(W2h, W2l, 61) ^ u64.shrSH(W2h, W2l, 6);
|
| 226 |
+
const s1l = u64.rotrSL(W2h, W2l, 19) ^ u64.rotrBL(W2h, W2l, 61) ^ u64.shrSL(W2h, W2l, 6);
|
| 227 |
+
// SHA256_W[i] = s0 + s1 + SHA256_W[i - 7] + SHA256_W[i - 16];
|
| 228 |
+
const SUMl = u64.add4L(s0l, s1l, SHA512_W_L[i - 7], SHA512_W_L[i - 16]);
|
| 229 |
+
const SUMh = u64.add4H(SUMl, s0h, s1h, SHA512_W_H[i - 7], SHA512_W_H[i - 16]);
|
| 230 |
+
SHA512_W_H[i] = SUMh | 0;
|
| 231 |
+
SHA512_W_L[i] = SUMl | 0;
|
| 232 |
+
}
|
| 233 |
+
let { Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl } = this;
|
| 234 |
+
// Compression function main loop, 80 rounds
|
| 235 |
+
for (let i = 0; i < 80; i++) {
|
| 236 |
+
// S1 := (e rightrotate 14) xor (e rightrotate 18) xor (e rightrotate 41)
|
| 237 |
+
const sigma1h = u64.rotrSH(Eh, El, 14) ^ u64.rotrSH(Eh, El, 18) ^ u64.rotrBH(Eh, El, 41);
|
| 238 |
+
const sigma1l = u64.rotrSL(Eh, El, 14) ^ u64.rotrSL(Eh, El, 18) ^ u64.rotrBL(Eh, El, 41);
|
| 239 |
+
//const T1 = (H + sigma1 + Chi(E, F, G) + SHA256_K[i] + SHA256_W[i]) | 0;
|
| 240 |
+
const CHIh = (Eh & Fh) ^ (~Eh & Gh);
|
| 241 |
+
const CHIl = (El & Fl) ^ (~El & Gl);
|
| 242 |
+
// T1 = H + sigma1 + Chi(E, F, G) + SHA512_K[i] + SHA512_W[i]
|
| 243 |
+
// prettier-ignore
|
| 244 |
+
const T1ll = u64.add5L(Hl, sigma1l, CHIl, SHA512_Kl[i], SHA512_W_L[i]);
|
| 245 |
+
const T1h = u64.add5H(T1ll, Hh, sigma1h, CHIh, SHA512_Kh[i], SHA512_W_H[i]);
|
| 246 |
+
const T1l = T1ll | 0;
|
| 247 |
+
// S0 := (a rightrotate 28) xor (a rightrotate 34) xor (a rightrotate 39)
|
| 248 |
+
const sigma0h = u64.rotrSH(Ah, Al, 28) ^ u64.rotrBH(Ah, Al, 34) ^ u64.rotrBH(Ah, Al, 39);
|
| 249 |
+
const sigma0l = u64.rotrSL(Ah, Al, 28) ^ u64.rotrBL(Ah, Al, 34) ^ u64.rotrBL(Ah, Al, 39);
|
| 250 |
+
const MAJh = (Ah & Bh) ^ (Ah & Ch) ^ (Bh & Ch);
|
| 251 |
+
const MAJl = (Al & Bl) ^ (Al & Cl) ^ (Bl & Cl);
|
| 252 |
+
Hh = Gh | 0;
|
| 253 |
+
Hl = Gl | 0;
|
| 254 |
+
Gh = Fh | 0;
|
| 255 |
+
Gl = Fl | 0;
|
| 256 |
+
Fh = Eh | 0;
|
| 257 |
+
Fl = El | 0;
|
| 258 |
+
({ h: Eh, l: El } = u64.add(Dh | 0, Dl | 0, T1h | 0, T1l | 0));
|
| 259 |
+
Dh = Ch | 0;
|
| 260 |
+
Dl = Cl | 0;
|
| 261 |
+
Ch = Bh | 0;
|
| 262 |
+
Cl = Bl | 0;
|
| 263 |
+
Bh = Ah | 0;
|
| 264 |
+
Bl = Al | 0;
|
| 265 |
+
const All = u64.add3L(T1l, sigma0l, MAJl);
|
| 266 |
+
Ah = u64.add3H(All, T1h, sigma0h, MAJh);
|
| 267 |
+
Al = All | 0;
|
| 268 |
+
}
|
| 269 |
+
// Add the compressed chunk to the current hash value
|
| 270 |
+
({ h: Ah, l: Al } = u64.add(this.Ah | 0, this.Al | 0, Ah | 0, Al | 0));
|
| 271 |
+
({ h: Bh, l: Bl } = u64.add(this.Bh | 0, this.Bl | 0, Bh | 0, Bl | 0));
|
| 272 |
+
({ h: Ch, l: Cl } = u64.add(this.Ch | 0, this.Cl | 0, Ch | 0, Cl | 0));
|
| 273 |
+
({ h: Dh, l: Dl } = u64.add(this.Dh | 0, this.Dl | 0, Dh | 0, Dl | 0));
|
| 274 |
+
({ h: Eh, l: El } = u64.add(this.Eh | 0, this.El | 0, Eh | 0, El | 0));
|
| 275 |
+
({ h: Fh, l: Fl } = u64.add(this.Fh | 0, this.Fl | 0, Fh | 0, Fl | 0));
|
| 276 |
+
({ h: Gh, l: Gl } = u64.add(this.Gh | 0, this.Gl | 0, Gh | 0, Gl | 0));
|
| 277 |
+
({ h: Hh, l: Hl } = u64.add(this.Hh | 0, this.Hl | 0, Hh | 0, Hl | 0));
|
| 278 |
+
this.set(Ah, Al, Bh, Bl, Ch, Cl, Dh, Dl, Eh, El, Fh, Fl, Gh, Gl, Hh, Hl);
|
| 279 |
+
}
|
| 280 |
+
protected roundClean(): void {
|
| 281 |
+
clean(SHA512_W_H, SHA512_W_L);
|
| 282 |
+
}
|
| 283 |
+
destroy(): void {
|
| 284 |
+
clean(this.buffer);
|
| 285 |
+
this.set(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
|
| 286 |
+
}
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
export class SHA384 extends SHA512 {
|
| 290 |
+
protected Ah: number = SHA384_IV[0] | 0;
|
| 291 |
+
protected Al: number = SHA384_IV[1] | 0;
|
| 292 |
+
protected Bh: number = SHA384_IV[2] | 0;
|
| 293 |
+
protected Bl: number = SHA384_IV[3] | 0;
|
| 294 |
+
protected Ch: number = SHA384_IV[4] | 0;
|
| 295 |
+
protected Cl: number = SHA384_IV[5] | 0;
|
| 296 |
+
protected Dh: number = SHA384_IV[6] | 0;
|
| 297 |
+
protected Dl: number = SHA384_IV[7] | 0;
|
| 298 |
+
protected Eh: number = SHA384_IV[8] | 0;
|
| 299 |
+
protected El: number = SHA384_IV[9] | 0;
|
| 300 |
+
protected Fh: number = SHA384_IV[10] | 0;
|
| 301 |
+
protected Fl: number = SHA384_IV[11] | 0;
|
| 302 |
+
protected Gh: number = SHA384_IV[12] | 0;
|
| 303 |
+
protected Gl: number = SHA384_IV[13] | 0;
|
| 304 |
+
protected Hh: number = SHA384_IV[14] | 0;
|
| 305 |
+
protected Hl: number = SHA384_IV[15] | 0;
|
| 306 |
+
|
| 307 |
+
constructor() {
|
| 308 |
+
super(48);
|
| 309 |
+
}
|
| 310 |
+
}
|
| 311 |
+
|
| 312 |
+
/**
|
| 313 |
+
* Truncated SHA512/256 and SHA512/224.
|
| 314 |
+
* SHA512_IV is XORed with 0xa5a5a5a5a5a5a5a5, then used as "intermediary" IV of SHA512/t.
|
| 315 |
+
* Then t hashes string to produce result IV.
|
| 316 |
+
* See `test/misc/sha2-gen-iv.js`.
|
| 317 |
+
*/
|
| 318 |
+
|
| 319 |
+
/** SHA512/224 IV */
|
| 320 |
+
const T224_IV = /* @__PURE__ */ Uint32Array.from([
|
| 321 |
+
0x8c3d37c8, 0x19544da2, 0x73e19966, 0x89dcd4d6, 0x1dfab7ae, 0x32ff9c82, 0x679dd514, 0x582f9fcf,
|
| 322 |
+
0x0f6d2b69, 0x7bd44da8, 0x77e36f73, 0x04c48942, 0x3f9d85a8, 0x6a1d36c8, 0x1112e6ad, 0x91d692a1,
|
| 323 |
+
]);
|
| 324 |
+
|
| 325 |
+
/** SHA512/256 IV */
|
| 326 |
+
const T256_IV = /* @__PURE__ */ Uint32Array.from([
|
| 327 |
+
0x22312194, 0xfc2bf72c, 0x9f555fa3, 0xc84c64c2, 0x2393b86b, 0x6f53b151, 0x96387719, 0x5940eabd,
|
| 328 |
+
0x96283ee2, 0xa88effe3, 0xbe5e1e25, 0x53863992, 0x2b0199fc, 0x2c85b8aa, 0x0eb72ddc, 0x81c52ca2,
|
| 329 |
+
]);
|
| 330 |
+
|
| 331 |
+
export class SHA512_224 extends SHA512 {
|
| 332 |
+
protected Ah: number = T224_IV[0] | 0;
|
| 333 |
+
protected Al: number = T224_IV[1] | 0;
|
| 334 |
+
protected Bh: number = T224_IV[2] | 0;
|
| 335 |
+
protected Bl: number = T224_IV[3] | 0;
|
| 336 |
+
protected Ch: number = T224_IV[4] | 0;
|
| 337 |
+
protected Cl: number = T224_IV[5] | 0;
|
| 338 |
+
protected Dh: number = T224_IV[6] | 0;
|
| 339 |
+
protected Dl: number = T224_IV[7] | 0;
|
| 340 |
+
protected Eh: number = T224_IV[8] | 0;
|
| 341 |
+
protected El: number = T224_IV[9] | 0;
|
| 342 |
+
protected Fh: number = T224_IV[10] | 0;
|
| 343 |
+
protected Fl: number = T224_IV[11] | 0;
|
| 344 |
+
protected Gh: number = T224_IV[12] | 0;
|
| 345 |
+
protected Gl: number = T224_IV[13] | 0;
|
| 346 |
+
protected Hh: number = T224_IV[14] | 0;
|
| 347 |
+
protected Hl: number = T224_IV[15] | 0;
|
| 348 |
+
|
| 349 |
+
constructor() {
|
| 350 |
+
super(28);
|
| 351 |
+
}
|
| 352 |
+
}
|
| 353 |
+
|
| 354 |
+
export class SHA512_256 extends SHA512 {
|
| 355 |
+
protected Ah: number = T256_IV[0] | 0;
|
| 356 |
+
protected Al: number = T256_IV[1] | 0;
|
| 357 |
+
protected Bh: number = T256_IV[2] | 0;
|
| 358 |
+
protected Bl: number = T256_IV[3] | 0;
|
| 359 |
+
protected Ch: number = T256_IV[4] | 0;
|
| 360 |
+
protected Cl: number = T256_IV[5] | 0;
|
| 361 |
+
protected Dh: number = T256_IV[6] | 0;
|
| 362 |
+
protected Dl: number = T256_IV[7] | 0;
|
| 363 |
+
protected Eh: number = T256_IV[8] | 0;
|
| 364 |
+
protected El: number = T256_IV[9] | 0;
|
| 365 |
+
protected Fh: number = T256_IV[10] | 0;
|
| 366 |
+
protected Fl: number = T256_IV[11] | 0;
|
| 367 |
+
protected Gh: number = T256_IV[12] | 0;
|
| 368 |
+
protected Gl: number = T256_IV[13] | 0;
|
| 369 |
+
protected Hh: number = T256_IV[14] | 0;
|
| 370 |
+
protected Hl: number = T256_IV[15] | 0;
|
| 371 |
+
|
| 372 |
+
constructor() {
|
| 373 |
+
super(32);
|
| 374 |
+
}
|
| 375 |
+
}
|
| 376 |
+
|
| 377 |
+
/**
|
| 378 |
+
* SHA2-256 hash function from RFC 4634.
|
| 379 |
+
*
|
| 380 |
+
* It is the fastest JS hash, even faster than Blake3.
|
| 381 |
+
* To break sha256 using birthday attack, attackers need to try 2^128 hashes.
|
| 382 |
+
* BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
|
| 383 |
+
*/
|
| 384 |
+
export const sha256: CHash = /* @__PURE__ */ createHasher(() => new SHA256());
|
| 385 |
+
/** SHA2-224 hash function from RFC 4634 */
|
| 386 |
+
export const sha224: CHash = /* @__PURE__ */ createHasher(() => new SHA224());
|
| 387 |
+
|
| 388 |
+
/** SHA2-512 hash function from RFC 4634. */
|
| 389 |
+
export const sha512: CHash = /* @__PURE__ */ createHasher(() => new SHA512());
|
| 390 |
+
/** SHA2-384 hash function from RFC 4634. */
|
| 391 |
+
export const sha384: CHash = /* @__PURE__ */ createHasher(() => new SHA384());
|
| 392 |
+
|
| 393 |
+
/**
|
| 394 |
+
* SHA2-512/256 "truncated" hash function, with improved resistance to length extension attacks.
|
| 395 |
+
* See the paper on [truncated SHA512](https://eprint.iacr.org/2010/548.pdf).
|
| 396 |
+
*/
|
| 397 |
+
export const sha512_256: CHash = /* @__PURE__ */ createHasher(() => new SHA512_256());
|
| 398 |
+
/**
|
| 399 |
+
* SHA2-512/224 "truncated" hash function, with improved resistance to length extension attacks.
|
| 400 |
+
* See the paper on [truncated SHA512](https://eprint.iacr.org/2010/548.pdf).
|
| 401 |
+
*/
|
| 402 |
+
export const sha512_224: CHash = /* @__PURE__ */ createHasher(() => new SHA512_224());
|
node_modules/@noble/hashes/src/sha256.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* SHA2-256 a.k.a. sha256. In JS, it is the fastest hash, even faster than Blake3.
|
| 3 |
+
*
|
| 4 |
+
* To break sha256 using birthday attack, attackers need to try 2^128 hashes.
|
| 5 |
+
* BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
|
| 6 |
+
*
|
| 7 |
+
* Check out [FIPS 180-4](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf).
|
| 8 |
+
* @module
|
| 9 |
+
* @deprecated
|
| 10 |
+
*/
|
| 11 |
+
import {
|
| 12 |
+
SHA224 as SHA224n,
|
| 13 |
+
sha224 as sha224n,
|
| 14 |
+
SHA256 as SHA256n,
|
| 15 |
+
sha256 as sha256n,
|
| 16 |
+
} from './sha2.ts';
|
| 17 |
+
/** @deprecated Use import from `noble/hashes/sha2` module */
|
| 18 |
+
export const SHA256: typeof SHA256n = SHA256n;
|
| 19 |
+
/** @deprecated Use import from `noble/hashes/sha2` module */
|
| 20 |
+
export const sha256: typeof sha256n = sha256n;
|
| 21 |
+
/** @deprecated Use import from `noble/hashes/sha2` module */
|
| 22 |
+
export const SHA224: typeof SHA224n = SHA224n;
|
| 23 |
+
/** @deprecated Use import from `noble/hashes/sha2` module */
|
| 24 |
+
export const sha224: typeof sha224n = sha224n;
|
node_modules/@noble/hashes/src/sha3-addons.ts
ADDED
|
@@ -0,0 +1,499 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* SHA3 (keccak) addons.
|
| 3 |
+
*
|
| 4 |
+
* * Full [NIST SP 800-185](https://nvlpubs.nist.gov/nistpubs/SpecialPublications/NIST.SP.800-185.pdf):
|
| 5 |
+
* cSHAKE, KMAC, TupleHash, ParallelHash + XOF variants
|
| 6 |
+
* * Reduced-round Keccak [(draft)](https://datatracker.ietf.org/doc/draft-irtf-cfrg-kangarootwelve/):
|
| 7 |
+
* * 🦘 K12 aka KangarooTwelve
|
| 8 |
+
* * M14 aka MarsupilamiFourteen
|
| 9 |
+
* * TurboSHAKE
|
| 10 |
+
* * KeccakPRG: Pseudo-random generator based on Keccak [(pdf)](https://keccak.team/files/CSF-0.1.pdf)
|
| 11 |
+
* @module
|
| 12 |
+
*/
|
| 13 |
+
import { Keccak, type ShakeOpts } from './sha3.ts';
|
| 14 |
+
import {
|
| 15 |
+
abytes,
|
| 16 |
+
anumber,
|
| 17 |
+
type CHashO,
|
| 18 |
+
type CHashXO,
|
| 19 |
+
createOptHasher,
|
| 20 |
+
createXOFer,
|
| 21 |
+
Hash,
|
| 22 |
+
type HashXOF,
|
| 23 |
+
type Input,
|
| 24 |
+
toBytes,
|
| 25 |
+
u32,
|
| 26 |
+
} from './utils.ts';
|
| 27 |
+
|
| 28 |
+
// cSHAKE && KMAC (NIST SP800-185)
|
| 29 |
+
const _8n = BigInt(8);
|
| 30 |
+
const _ffn = BigInt(0xff);
|
| 31 |
+
|
| 32 |
+
// NOTE: it is safe to use bigints here, since they used only for length encoding (not actual data).
|
| 33 |
+
// We use bigints in sha256 for lengths too.
|
| 34 |
+
function leftEncode(n: number | bigint): Uint8Array {
|
| 35 |
+
n = BigInt(n);
|
| 36 |
+
const res = [Number(n & _ffn)];
|
| 37 |
+
n >>= _8n;
|
| 38 |
+
for (; n > 0; n >>= _8n) res.unshift(Number(n & _ffn));
|
| 39 |
+
res.unshift(res.length);
|
| 40 |
+
return new Uint8Array(res);
|
| 41 |
+
}
|
| 42 |
+
|
| 43 |
+
function rightEncode(n: number | bigint): Uint8Array {
|
| 44 |
+
n = BigInt(n);
|
| 45 |
+
const res = [Number(n & _ffn)];
|
| 46 |
+
n >>= _8n;
|
| 47 |
+
for (; n > 0; n >>= _8n) res.unshift(Number(n & _ffn));
|
| 48 |
+
res.push(res.length);
|
| 49 |
+
return new Uint8Array(res);
|
| 50 |
+
}
|
| 51 |
+
|
| 52 |
+
function chooseLen(opts: ShakeOpts, outputLen: number): number {
|
| 53 |
+
return opts.dkLen === undefined ? outputLen : opts.dkLen;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
const abytesOrZero = (buf?: Input) => {
|
| 57 |
+
if (buf === undefined) return Uint8Array.of();
|
| 58 |
+
return toBytes(buf);
|
| 59 |
+
};
|
| 60 |
+
// NOTE: second modulo is necessary since we don't need to add padding if current element takes whole block
|
| 61 |
+
const getPadding = (len: number, block: number) => new Uint8Array((block - (len % block)) % block);
|
| 62 |
+
export type cShakeOpts = ShakeOpts & { personalization?: Input; NISTfn?: Input };
|
| 63 |
+
|
| 64 |
+
// Personalization
|
| 65 |
+
function cshakePers(hash: Keccak, opts: cShakeOpts = {}): Keccak {
|
| 66 |
+
if (!opts || (!opts.personalization && !opts.NISTfn)) return hash;
|
| 67 |
+
// Encode and pad inplace to avoid unneccesary memory copies/slices (so we don't need to zero them later)
|
| 68 |
+
// bytepad(encode_string(N) || encode_string(S), 168)
|
| 69 |
+
const blockLenBytes = leftEncode(hash.blockLen);
|
| 70 |
+
const fn = abytesOrZero(opts.NISTfn);
|
| 71 |
+
const fnLen = leftEncode(_8n * BigInt(fn.length)); // length in bits
|
| 72 |
+
const pers = abytesOrZero(opts.personalization);
|
| 73 |
+
const persLen = leftEncode(_8n * BigInt(pers.length)); // length in bits
|
| 74 |
+
if (!fn.length && !pers.length) return hash;
|
| 75 |
+
hash.suffix = 0x04;
|
| 76 |
+
hash.update(blockLenBytes).update(fnLen).update(fn).update(persLen).update(pers);
|
| 77 |
+
let totalLen = blockLenBytes.length + fnLen.length + fn.length + persLen.length + pers.length;
|
| 78 |
+
hash.update(getPadding(totalLen, hash.blockLen));
|
| 79 |
+
return hash;
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
const gencShake = (suffix: number, blockLen: number, outputLen: number) =>
|
| 83 |
+
createXOFer<Keccak, cShakeOpts>((opts: cShakeOpts = {}) =>
|
| 84 |
+
cshakePers(new Keccak(blockLen, suffix, chooseLen(opts, outputLen), true), opts)
|
| 85 |
+
);
|
| 86 |
+
|
| 87 |
+
// TODO: refactor
|
| 88 |
+
export type ICShake = {
|
| 89 |
+
(msg: Input, opts?: cShakeOpts): Uint8Array;
|
| 90 |
+
outputLen: number;
|
| 91 |
+
blockLen: number;
|
| 92 |
+
create(opts: cShakeOpts): HashXOF<Keccak>;
|
| 93 |
+
};
|
| 94 |
+
export type ITupleHash = {
|
| 95 |
+
(messages: Input[], opts?: cShakeOpts): Uint8Array;
|
| 96 |
+
create(opts?: cShakeOpts): TupleHash;
|
| 97 |
+
};
|
| 98 |
+
export type IParHash = {
|
| 99 |
+
(message: Input, opts?: ParallelOpts): Uint8Array;
|
| 100 |
+
create(opts?: ParallelOpts): ParallelHash;
|
| 101 |
+
};
|
| 102 |
+
export const cshake128: ICShake = /* @__PURE__ */ (() => gencShake(0x1f, 168, 128 / 8))();
|
| 103 |
+
export const cshake256: ICShake = /* @__PURE__ */ (() => gencShake(0x1f, 136, 256 / 8))();
|
| 104 |
+
|
| 105 |
+
export class KMAC extends Keccak implements HashXOF<KMAC> {
|
| 106 |
+
constructor(
|
| 107 |
+
blockLen: number,
|
| 108 |
+
outputLen: number,
|
| 109 |
+
enableXOF: boolean,
|
| 110 |
+
key: Input,
|
| 111 |
+
opts: cShakeOpts = {}
|
| 112 |
+
) {
|
| 113 |
+
super(blockLen, 0x1f, outputLen, enableXOF);
|
| 114 |
+
cshakePers(this, { NISTfn: 'KMAC', personalization: opts.personalization });
|
| 115 |
+
key = toBytes(key);
|
| 116 |
+
abytes(key);
|
| 117 |
+
// 1. newX = bytepad(encode_string(K), 168) || X || right_encode(L).
|
| 118 |
+
const blockLenBytes = leftEncode(this.blockLen);
|
| 119 |
+
const keyLen = leftEncode(_8n * BigInt(key.length));
|
| 120 |
+
this.update(blockLenBytes).update(keyLen).update(key);
|
| 121 |
+
const totalLen = blockLenBytes.length + keyLen.length + key.length;
|
| 122 |
+
this.update(getPadding(totalLen, this.blockLen));
|
| 123 |
+
}
|
| 124 |
+
protected finish(): void {
|
| 125 |
+
if (!this.finished) this.update(rightEncode(this.enableXOF ? 0 : _8n * BigInt(this.outputLen))); // outputLen in bits
|
| 126 |
+
super.finish();
|
| 127 |
+
}
|
| 128 |
+
_cloneInto(to?: KMAC): KMAC {
|
| 129 |
+
// Create new instance without calling constructor since key already in state and we don't know it.
|
| 130 |
+
// Force "to" to be instance of KMAC instead of Sha3.
|
| 131 |
+
if (!to) {
|
| 132 |
+
to = Object.create(Object.getPrototypeOf(this), {}) as KMAC;
|
| 133 |
+
to.state = this.state.slice();
|
| 134 |
+
to.blockLen = this.blockLen;
|
| 135 |
+
to.state32 = u32(to.state);
|
| 136 |
+
}
|
| 137 |
+
return super._cloneInto(to) as KMAC;
|
| 138 |
+
}
|
| 139 |
+
clone(): KMAC {
|
| 140 |
+
return this._cloneInto();
|
| 141 |
+
}
|
| 142 |
+
}
|
| 143 |
+
|
| 144 |
+
function genKmac(blockLen: number, outputLen: number, xof = false) {
|
| 145 |
+
const kmac = (key: Input, message: Input, opts?: cShakeOpts): Uint8Array =>
|
| 146 |
+
kmac.create(key, opts).update(message).digest();
|
| 147 |
+
kmac.create = (key: Input, opts: cShakeOpts = {}) =>
|
| 148 |
+
new KMAC(blockLen, chooseLen(opts, outputLen), xof, key, opts);
|
| 149 |
+
return kmac;
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
export const kmac128: {
|
| 153 |
+
(key: Input, message: Input, opts?: cShakeOpts): Uint8Array;
|
| 154 |
+
create(key: Input, opts?: cShakeOpts): KMAC;
|
| 155 |
+
} = /* @__PURE__ */ (() => genKmac(168, 128 / 8))();
|
| 156 |
+
export const kmac256: {
|
| 157 |
+
(key: Input, message: Input, opts?: cShakeOpts): Uint8Array;
|
| 158 |
+
create(key: Input, opts?: cShakeOpts): KMAC;
|
| 159 |
+
} = /* @__PURE__ */ (() => genKmac(136, 256 / 8))();
|
| 160 |
+
export const kmac128xof: {
|
| 161 |
+
(key: Input, message: Input, opts?: cShakeOpts): Uint8Array;
|
| 162 |
+
create(key: Input, opts?: cShakeOpts): KMAC;
|
| 163 |
+
} = /* @__PURE__ */ (() => genKmac(168, 128 / 8, true))();
|
| 164 |
+
export const kmac256xof: {
|
| 165 |
+
(key: Input, message: Input, opts?: cShakeOpts): Uint8Array;
|
| 166 |
+
create(key: Input, opts?: cShakeOpts): KMAC;
|
| 167 |
+
} = /* @__PURE__ */ (() => genKmac(136, 256 / 8, true))();
|
| 168 |
+
|
| 169 |
+
// TupleHash
|
| 170 |
+
// Usage: tuple(['ab', 'cd']) != tuple(['a', 'bcd'])
|
| 171 |
+
export class TupleHash extends Keccak implements HashXOF<TupleHash> {
|
| 172 |
+
constructor(blockLen: number, outputLen: number, enableXOF: boolean, opts: cShakeOpts = {}) {
|
| 173 |
+
super(blockLen, 0x1f, outputLen, enableXOF);
|
| 174 |
+
cshakePers(this, { NISTfn: 'TupleHash', personalization: opts.personalization });
|
| 175 |
+
// Change update after cshake processed
|
| 176 |
+
this.update = (data: Input) => {
|
| 177 |
+
data = toBytes(data);
|
| 178 |
+
abytes(data);
|
| 179 |
+
super.update(leftEncode(_8n * BigInt(data.length)));
|
| 180 |
+
super.update(data);
|
| 181 |
+
return this;
|
| 182 |
+
};
|
| 183 |
+
}
|
| 184 |
+
protected finish(): void {
|
| 185 |
+
if (!this.finished)
|
| 186 |
+
super.update(rightEncode(this.enableXOF ? 0 : _8n * BigInt(this.outputLen))); // outputLen in bits
|
| 187 |
+
super.finish();
|
| 188 |
+
}
|
| 189 |
+
_cloneInto(to?: TupleHash): TupleHash {
|
| 190 |
+
to ||= new TupleHash(this.blockLen, this.outputLen, this.enableXOF);
|
| 191 |
+
return super._cloneInto(to) as TupleHash;
|
| 192 |
+
}
|
| 193 |
+
clone(): TupleHash {
|
| 194 |
+
return this._cloneInto();
|
| 195 |
+
}
|
| 196 |
+
}
|
| 197 |
+
|
| 198 |
+
function genTuple(blockLen: number, outputLen: number, xof = false) {
|
| 199 |
+
const tuple = (messages: Input[], opts?: cShakeOpts): Uint8Array => {
|
| 200 |
+
const h = tuple.create(opts);
|
| 201 |
+
for (const msg of messages) h.update(msg);
|
| 202 |
+
return h.digest();
|
| 203 |
+
};
|
| 204 |
+
tuple.create = (opts: cShakeOpts = {}) =>
|
| 205 |
+
new TupleHash(blockLen, chooseLen(opts, outputLen), xof, opts);
|
| 206 |
+
return tuple;
|
| 207 |
+
}
|
| 208 |
+
|
| 209 |
+
/** 128-bit TupleHASH. */
|
| 210 |
+
export const tuplehash128: ITupleHash = /* @__PURE__ */ (() => genTuple(168, 128 / 8))();
|
| 211 |
+
/** 256-bit TupleHASH. */
|
| 212 |
+
export const tuplehash256: ITupleHash = /* @__PURE__ */ (() => genTuple(136, 256 / 8))();
|
| 213 |
+
/** 128-bit TupleHASH XOF. */
|
| 214 |
+
export const tuplehash128xof: ITupleHash = /* @__PURE__ */ (() => genTuple(168, 128 / 8, true))();
|
| 215 |
+
/** 256-bit TupleHASH XOF. */
|
| 216 |
+
export const tuplehash256xof: ITupleHash = /* @__PURE__ */ (() => genTuple(136, 256 / 8, true))();
|
| 217 |
+
|
| 218 |
+
// ParallelHash (same as K12/M14, but without speedup for inputs less 8kb, reduced number of rounds and more simple)
|
| 219 |
+
type ParallelOpts = cShakeOpts & { blockLen?: number };
|
| 220 |
+
|
| 221 |
+
export class ParallelHash extends Keccak implements HashXOF<ParallelHash> {
|
| 222 |
+
private leafHash?: Hash<Keccak>;
|
| 223 |
+
protected leafCons: () => Hash<Keccak>;
|
| 224 |
+
private chunkPos = 0; // Position of current block in chunk
|
| 225 |
+
private chunksDone = 0; // How many chunks we already have
|
| 226 |
+
private chunkLen: number;
|
| 227 |
+
constructor(
|
| 228 |
+
blockLen: number,
|
| 229 |
+
outputLen: number,
|
| 230 |
+
leafCons: () => Hash<Keccak>,
|
| 231 |
+
enableXOF: boolean,
|
| 232 |
+
opts: ParallelOpts = {}
|
| 233 |
+
) {
|
| 234 |
+
super(blockLen, 0x1f, outputLen, enableXOF);
|
| 235 |
+
cshakePers(this, { NISTfn: 'ParallelHash', personalization: opts.personalization });
|
| 236 |
+
this.leafCons = leafCons;
|
| 237 |
+
let { blockLen: B } = opts;
|
| 238 |
+
B ||= 8;
|
| 239 |
+
anumber(B);
|
| 240 |
+
this.chunkLen = B;
|
| 241 |
+
super.update(leftEncode(B));
|
| 242 |
+
// Change update after cshake processed
|
| 243 |
+
this.update = (data: Input) => {
|
| 244 |
+
data = toBytes(data);
|
| 245 |
+
abytes(data);
|
| 246 |
+
const { chunkLen, leafCons } = this;
|
| 247 |
+
for (let pos = 0, len = data.length; pos < len; ) {
|
| 248 |
+
if (this.chunkPos == chunkLen || !this.leafHash) {
|
| 249 |
+
if (this.leafHash) {
|
| 250 |
+
super.update(this.leafHash.digest());
|
| 251 |
+
this.chunksDone++;
|
| 252 |
+
}
|
| 253 |
+
this.leafHash = leafCons();
|
| 254 |
+
this.chunkPos = 0;
|
| 255 |
+
}
|
| 256 |
+
const take = Math.min(chunkLen - this.chunkPos, len - pos);
|
| 257 |
+
this.leafHash.update(data.subarray(pos, pos + take));
|
| 258 |
+
this.chunkPos += take;
|
| 259 |
+
pos += take;
|
| 260 |
+
}
|
| 261 |
+
return this;
|
| 262 |
+
};
|
| 263 |
+
}
|
| 264 |
+
protected finish(): void {
|
| 265 |
+
if (this.finished) return;
|
| 266 |
+
if (this.leafHash) {
|
| 267 |
+
super.update(this.leafHash.digest());
|
| 268 |
+
this.chunksDone++;
|
| 269 |
+
}
|
| 270 |
+
super.update(rightEncode(this.chunksDone));
|
| 271 |
+
super.update(rightEncode(this.enableXOF ? 0 : _8n * BigInt(this.outputLen))); // outputLen in bits
|
| 272 |
+
super.finish();
|
| 273 |
+
}
|
| 274 |
+
_cloneInto(to?: ParallelHash): ParallelHash {
|
| 275 |
+
to ||= new ParallelHash(this.blockLen, this.outputLen, this.leafCons, this.enableXOF);
|
| 276 |
+
if (this.leafHash) to.leafHash = this.leafHash._cloneInto(to.leafHash as Keccak);
|
| 277 |
+
to.chunkPos = this.chunkPos;
|
| 278 |
+
to.chunkLen = this.chunkLen;
|
| 279 |
+
to.chunksDone = this.chunksDone;
|
| 280 |
+
return super._cloneInto(to) as ParallelHash;
|
| 281 |
+
}
|
| 282 |
+
destroy(): void {
|
| 283 |
+
super.destroy.call(this);
|
| 284 |
+
if (this.leafHash) this.leafHash.destroy();
|
| 285 |
+
}
|
| 286 |
+
clone(): ParallelHash {
|
| 287 |
+
return this._cloneInto();
|
| 288 |
+
}
|
| 289 |
+
}
|
| 290 |
+
|
| 291 |
+
function genPrl(
|
| 292 |
+
blockLen: number,
|
| 293 |
+
outputLen: number,
|
| 294 |
+
leaf: ReturnType<typeof gencShake>,
|
| 295 |
+
xof = false
|
| 296 |
+
) {
|
| 297 |
+
const parallel = (message: Input, opts?: ParallelOpts): Uint8Array =>
|
| 298 |
+
parallel.create(opts).update(message).digest();
|
| 299 |
+
parallel.create = (opts: ParallelOpts = {}) =>
|
| 300 |
+
new ParallelHash(
|
| 301 |
+
blockLen,
|
| 302 |
+
chooseLen(opts, outputLen),
|
| 303 |
+
() => leaf.create({ dkLen: 2 * outputLen }),
|
| 304 |
+
xof,
|
| 305 |
+
opts
|
| 306 |
+
);
|
| 307 |
+
return parallel;
|
| 308 |
+
}
|
| 309 |
+
|
| 310 |
+
/** 128-bit ParallelHash. In JS, it is not parallel. */
|
| 311 |
+
export const parallelhash128: IParHash = /* @__PURE__ */ (() => genPrl(168, 128 / 8, cshake128))();
|
| 312 |
+
/** 256-bit ParallelHash. In JS, it is not parallel. */
|
| 313 |
+
export const parallelhash256: IParHash = /* @__PURE__ */ (() => genPrl(136, 256 / 8, cshake256))();
|
| 314 |
+
/** 128-bit ParallelHash XOF. In JS, it is not parallel. */
|
| 315 |
+
export const parallelhash128xof: IParHash = /* @__PURE__ */ (() =>
|
| 316 |
+
genPrl(168, 128 / 8, cshake128, true))();
|
| 317 |
+
/** 256-bit ParallelHash. In JS, it is not parallel. */
|
| 318 |
+
export const parallelhash256xof: IParHash = /* @__PURE__ */ (() =>
|
| 319 |
+
genPrl(136, 256 / 8, cshake256, true))();
|
| 320 |
+
|
| 321 |
+
// Should be simple 'shake with 12 rounds', but no, we got whole new spec about Turbo SHAKE Pro MAX.
|
| 322 |
+
export type TurboshakeOpts = ShakeOpts & {
|
| 323 |
+
D?: number; // Domain separation byte
|
| 324 |
+
};
|
| 325 |
+
|
| 326 |
+
const genTurboshake = (blockLen: number, outputLen: number) =>
|
| 327 |
+
createXOFer<HashXOF<Keccak>, TurboshakeOpts>((opts: TurboshakeOpts = {}) => {
|
| 328 |
+
const D = opts.D === undefined ? 0x1f : opts.D;
|
| 329 |
+
// Section 2.1 of https://datatracker.ietf.org/doc/draft-irtf-cfrg-kangarootwelve/
|
| 330 |
+
if (!Number.isSafeInteger(D) || D < 0x01 || D > 0x7f)
|
| 331 |
+
throw new Error('invalid domain separation byte must be 0x01..0x7f, got: ' + D);
|
| 332 |
+
return new Keccak(blockLen, D, opts.dkLen === undefined ? outputLen : opts.dkLen, true, 12);
|
| 333 |
+
});
|
| 334 |
+
|
| 335 |
+
/** TurboSHAKE 128-bit: reduced 12-round keccak. */
|
| 336 |
+
export const turboshake128: CHashXO = /* @__PURE__ */ genTurboshake(168, 256 / 8);
|
| 337 |
+
/** TurboSHAKE 256-bit: reduced 12-round keccak. */
|
| 338 |
+
export const turboshake256: CHashXO = /* @__PURE__ */ genTurboshake(136, 512 / 8);
|
| 339 |
+
|
| 340 |
+
// Kangaroo
|
| 341 |
+
// Same as NIST rightEncode, but returns [0] for zero string
|
| 342 |
+
function rightEncodeK12(n: number | bigint): Uint8Array {
|
| 343 |
+
n = BigInt(n);
|
| 344 |
+
const res: number[] = [];
|
| 345 |
+
for (; n > 0; n >>= _8n) res.unshift(Number(n & _ffn));
|
| 346 |
+
res.push(res.length);
|
| 347 |
+
return Uint8Array.from(res);
|
| 348 |
+
}
|
| 349 |
+
|
| 350 |
+
export type KangarooOpts = { dkLen?: number; personalization?: Input };
|
| 351 |
+
const EMPTY_BUFFER = /* @__PURE__ */ Uint8Array.of();
|
| 352 |
+
|
| 353 |
+
export class KangarooTwelve extends Keccak implements HashXOF<KangarooTwelve> {
|
| 354 |
+
readonly chunkLen = 8192;
|
| 355 |
+
private leafHash?: Keccak;
|
| 356 |
+
protected leafLen: number;
|
| 357 |
+
private personalization: Uint8Array;
|
| 358 |
+
private chunkPos = 0; // Position of current block in chunk
|
| 359 |
+
private chunksDone = 0; // How many chunks we already have
|
| 360 |
+
constructor(
|
| 361 |
+
blockLen: number,
|
| 362 |
+
leafLen: number,
|
| 363 |
+
outputLen: number,
|
| 364 |
+
rounds: number,
|
| 365 |
+
opts: KangarooOpts
|
| 366 |
+
) {
|
| 367 |
+
super(blockLen, 0x07, outputLen, true, rounds);
|
| 368 |
+
this.leafLen = leafLen;
|
| 369 |
+
this.personalization = abytesOrZero(opts.personalization);
|
| 370 |
+
}
|
| 371 |
+
update(data: Input): this {
|
| 372 |
+
data = toBytes(data);
|
| 373 |
+
abytes(data);
|
| 374 |
+
const { chunkLen, blockLen, leafLen, rounds } = this;
|
| 375 |
+
for (let pos = 0, len = data.length; pos < len; ) {
|
| 376 |
+
if (this.chunkPos == chunkLen) {
|
| 377 |
+
if (this.leafHash) super.update(this.leafHash.digest());
|
| 378 |
+
else {
|
| 379 |
+
this.suffix = 0x06; // Its safe to change suffix here since its used only in digest()
|
| 380 |
+
super.update(Uint8Array.from([3, 0, 0, 0, 0, 0, 0, 0]));
|
| 381 |
+
}
|
| 382 |
+
this.leafHash = new Keccak(blockLen, 0x0b, leafLen, false, rounds);
|
| 383 |
+
this.chunksDone++;
|
| 384 |
+
this.chunkPos = 0;
|
| 385 |
+
}
|
| 386 |
+
const take = Math.min(chunkLen - this.chunkPos, len - pos);
|
| 387 |
+
const chunk = data.subarray(pos, pos + take);
|
| 388 |
+
if (this.leafHash) this.leafHash.update(chunk);
|
| 389 |
+
else super.update(chunk);
|
| 390 |
+
this.chunkPos += take;
|
| 391 |
+
pos += take;
|
| 392 |
+
}
|
| 393 |
+
return this;
|
| 394 |
+
}
|
| 395 |
+
protected finish(): void {
|
| 396 |
+
if (this.finished) return;
|
| 397 |
+
const { personalization } = this;
|
| 398 |
+
this.update(personalization).update(rightEncodeK12(personalization.length));
|
| 399 |
+
// Leaf hash
|
| 400 |
+
if (this.leafHash) {
|
| 401 |
+
super.update(this.leafHash.digest());
|
| 402 |
+
super.update(rightEncodeK12(this.chunksDone));
|
| 403 |
+
super.update(Uint8Array.from([0xff, 0xff]));
|
| 404 |
+
}
|
| 405 |
+
super.finish.call(this);
|
| 406 |
+
}
|
| 407 |
+
destroy(): void {
|
| 408 |
+
super.destroy.call(this);
|
| 409 |
+
if (this.leafHash) this.leafHash.destroy();
|
| 410 |
+
// We cannot zero personalization buffer since it is user provided and we don't want to mutate user input
|
| 411 |
+
this.personalization = EMPTY_BUFFER;
|
| 412 |
+
}
|
| 413 |
+
_cloneInto(to?: KangarooTwelve): KangarooTwelve {
|
| 414 |
+
const { blockLen, leafLen, leafHash, outputLen, rounds } = this;
|
| 415 |
+
to ||= new KangarooTwelve(blockLen, leafLen, outputLen, rounds, {});
|
| 416 |
+
super._cloneInto(to);
|
| 417 |
+
if (leafHash) to.leafHash = leafHash._cloneInto(to.leafHash);
|
| 418 |
+
to.personalization.set(this.personalization);
|
| 419 |
+
to.leafLen = this.leafLen;
|
| 420 |
+
to.chunkPos = this.chunkPos;
|
| 421 |
+
to.chunksDone = this.chunksDone;
|
| 422 |
+
return to;
|
| 423 |
+
}
|
| 424 |
+
clone(): KangarooTwelve {
|
| 425 |
+
return this._cloneInto();
|
| 426 |
+
}
|
| 427 |
+
}
|
| 428 |
+
/** KangarooTwelve: reduced 12-round keccak. */
|
| 429 |
+
export const k12: CHashO = /* @__PURE__ */ (() =>
|
| 430 |
+
createOptHasher<KangarooTwelve, KangarooOpts>(
|
| 431 |
+
(opts: KangarooOpts = {}) => new KangarooTwelve(168, 32, chooseLen(opts, 32), 12, opts)
|
| 432 |
+
))();
|
| 433 |
+
/** MarsupilamiFourteen: reduced 14-round keccak. */
|
| 434 |
+
export const m14: CHashO = /* @__PURE__ */ (() =>
|
| 435 |
+
createOptHasher<KangarooTwelve, KangarooOpts>(
|
| 436 |
+
(opts: KangarooOpts = {}) => new KangarooTwelve(136, 64, chooseLen(opts, 64), 14, opts)
|
| 437 |
+
))();
|
| 438 |
+
|
| 439 |
+
/**
|
| 440 |
+
* More at https://github.com/XKCP/XKCP/tree/master/lib/high/Keccak/PRG.
|
| 441 |
+
*/
|
| 442 |
+
export class KeccakPRG extends Keccak {
|
| 443 |
+
protected rate: number;
|
| 444 |
+
constructor(capacity: number) {
|
| 445 |
+
anumber(capacity);
|
| 446 |
+
// Rho should be full bytes
|
| 447 |
+
if (capacity < 0 || capacity > 1600 - 10 || (1600 - capacity - 2) % 8)
|
| 448 |
+
throw new Error('invalid capacity');
|
| 449 |
+
// blockLen = rho in bytes
|
| 450 |
+
super((1600 - capacity - 2) / 8, 0, 0, true);
|
| 451 |
+
this.rate = 1600 - capacity;
|
| 452 |
+
this.posOut = Math.floor((this.rate + 7) / 8);
|
| 453 |
+
}
|
| 454 |
+
keccak(): void {
|
| 455 |
+
// Duplex padding
|
| 456 |
+
this.state[this.pos] ^= 0x01;
|
| 457 |
+
this.state[this.blockLen] ^= 0x02; // Rho is full bytes
|
| 458 |
+
super.keccak();
|
| 459 |
+
this.pos = 0;
|
| 460 |
+
this.posOut = 0;
|
| 461 |
+
}
|
| 462 |
+
update(data: Input): this {
|
| 463 |
+
super.update(data);
|
| 464 |
+
this.posOut = this.blockLen;
|
| 465 |
+
return this;
|
| 466 |
+
}
|
| 467 |
+
feed(data: Input): this {
|
| 468 |
+
return this.update(data);
|
| 469 |
+
}
|
| 470 |
+
protected finish(): void {}
|
| 471 |
+
digestInto(_out: Uint8Array): Uint8Array {
|
| 472 |
+
throw new Error('digest is not allowed, use .fetch instead');
|
| 473 |
+
}
|
| 474 |
+
fetch(bytes: number): Uint8Array {
|
| 475 |
+
return this.xof(bytes);
|
| 476 |
+
}
|
| 477 |
+
// Ensure irreversibility (even if state leaked previous outputs cannot be computed)
|
| 478 |
+
forget(): void {
|
| 479 |
+
if (this.rate < 1600 / 2 + 1) throw new Error('rate is too low to use .forget()');
|
| 480 |
+
this.keccak();
|
| 481 |
+
for (let i = 0; i < this.blockLen; i++) this.state[i] = 0;
|
| 482 |
+
this.pos = this.blockLen;
|
| 483 |
+
this.keccak();
|
| 484 |
+
this.posOut = this.blockLen;
|
| 485 |
+
}
|
| 486 |
+
_cloneInto(to?: KeccakPRG): KeccakPRG {
|
| 487 |
+
const { rate } = this;
|
| 488 |
+
to ||= new KeccakPRG(1600 - rate);
|
| 489 |
+
super._cloneInto(to);
|
| 490 |
+
to.rate = rate;
|
| 491 |
+
return to;
|
| 492 |
+
}
|
| 493 |
+
clone(): KeccakPRG {
|
| 494 |
+
return this._cloneInto();
|
| 495 |
+
}
|
| 496 |
+
}
|
| 497 |
+
|
| 498 |
+
/** KeccakPRG: Pseudo-random generator based on Keccak. https://keccak.team/files/CSF-0.1.pdf */
|
| 499 |
+
export const keccakprg = (capacity = 254): KeccakPRG => new KeccakPRG(capacity);
|
node_modules/@noble/hashes/src/sha3.ts
ADDED
|
@@ -0,0 +1,258 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* SHA3 (keccak) hash function, based on a new "Sponge function" design.
|
| 3 |
+
* Different from older hashes, the internal state is bigger than output size.
|
| 4 |
+
*
|
| 5 |
+
* Check out [FIPS-202](https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf),
|
| 6 |
+
* [Website](https://keccak.team/keccak.html),
|
| 7 |
+
* [the differences between SHA-3 and Keccak](https://crypto.stackexchange.com/questions/15727/what-are-the-key-differences-between-the-draft-sha-3-standard-and-the-keccak-sub).
|
| 8 |
+
*
|
| 9 |
+
* Check out `sha3-addons` module for cSHAKE, k12, and others.
|
| 10 |
+
* @module
|
| 11 |
+
*/
|
| 12 |
+
import { rotlBH, rotlBL, rotlSH, rotlSL, split } from './_u64.ts';
|
| 13 |
+
// prettier-ignore
|
| 14 |
+
import {
|
| 15 |
+
abytes, aexists, anumber, aoutput,
|
| 16 |
+
clean, createHasher, createXOFer, Hash,
|
| 17 |
+
swap32IfBE,
|
| 18 |
+
toBytes, u32,
|
| 19 |
+
type CHash, type CHashXO, type HashXOF, type Input
|
| 20 |
+
} from './utils.ts';
|
| 21 |
+
|
| 22 |
+
// No __PURE__ annotations in sha3 header:
|
| 23 |
+
// EVERYTHING is in fact used on every export.
|
| 24 |
+
// Various per round constants calculations
|
| 25 |
+
const _0n = BigInt(0);
|
| 26 |
+
const _1n = BigInt(1);
|
| 27 |
+
const _2n = BigInt(2);
|
| 28 |
+
const _7n = BigInt(7);
|
| 29 |
+
const _256n = BigInt(256);
|
| 30 |
+
const _0x71n = BigInt(0x71);
|
| 31 |
+
const SHA3_PI: number[] = [];
|
| 32 |
+
const SHA3_ROTL: number[] = [];
|
| 33 |
+
const _SHA3_IOTA: bigint[] = [];
|
| 34 |
+
for (let round = 0, R = _1n, x = 1, y = 0; round < 24; round++) {
|
| 35 |
+
// Pi
|
| 36 |
+
[x, y] = [y, (2 * x + 3 * y) % 5];
|
| 37 |
+
SHA3_PI.push(2 * (5 * y + x));
|
| 38 |
+
// Rotational
|
| 39 |
+
SHA3_ROTL.push((((round + 1) * (round + 2)) / 2) % 64);
|
| 40 |
+
// Iota
|
| 41 |
+
let t = _0n;
|
| 42 |
+
for (let j = 0; j < 7; j++) {
|
| 43 |
+
R = ((R << _1n) ^ ((R >> _7n) * _0x71n)) % _256n;
|
| 44 |
+
if (R & _2n) t ^= _1n << ((_1n << /* @__PURE__ */ BigInt(j)) - _1n);
|
| 45 |
+
}
|
| 46 |
+
_SHA3_IOTA.push(t);
|
| 47 |
+
}
|
| 48 |
+
const IOTAS = split(_SHA3_IOTA, true);
|
| 49 |
+
const SHA3_IOTA_H = IOTAS[0];
|
| 50 |
+
const SHA3_IOTA_L = IOTAS[1];
|
| 51 |
+
|
| 52 |
+
// Left rotation (without 0, 32, 64)
|
| 53 |
+
const rotlH = (h: number, l: number, s: number) => (s > 32 ? rotlBH(h, l, s) : rotlSH(h, l, s));
|
| 54 |
+
const rotlL = (h: number, l: number, s: number) => (s > 32 ? rotlBL(h, l, s) : rotlSL(h, l, s));
|
| 55 |
+
|
| 56 |
+
/** `keccakf1600` internal function, additionally allows to adjust round count. */
|
| 57 |
+
export function keccakP(s: Uint32Array, rounds: number = 24): void {
|
| 58 |
+
const B = new Uint32Array(5 * 2);
|
| 59 |
+
// NOTE: all indices are x2 since we store state as u32 instead of u64 (bigints to slow in js)
|
| 60 |
+
for (let round = 24 - rounds; round < 24; round++) {
|
| 61 |
+
// Theta θ
|
| 62 |
+
for (let x = 0; x < 10; x++) B[x] = s[x] ^ s[x + 10] ^ s[x + 20] ^ s[x + 30] ^ s[x + 40];
|
| 63 |
+
for (let x = 0; x < 10; x += 2) {
|
| 64 |
+
const idx1 = (x + 8) % 10;
|
| 65 |
+
const idx0 = (x + 2) % 10;
|
| 66 |
+
const B0 = B[idx0];
|
| 67 |
+
const B1 = B[idx0 + 1];
|
| 68 |
+
const Th = rotlH(B0, B1, 1) ^ B[idx1];
|
| 69 |
+
const Tl = rotlL(B0, B1, 1) ^ B[idx1 + 1];
|
| 70 |
+
for (let y = 0; y < 50; y += 10) {
|
| 71 |
+
s[x + y] ^= Th;
|
| 72 |
+
s[x + y + 1] ^= Tl;
|
| 73 |
+
}
|
| 74 |
+
}
|
| 75 |
+
// Rho (ρ) and Pi (π)
|
| 76 |
+
let curH = s[2];
|
| 77 |
+
let curL = s[3];
|
| 78 |
+
for (let t = 0; t < 24; t++) {
|
| 79 |
+
const shift = SHA3_ROTL[t];
|
| 80 |
+
const Th = rotlH(curH, curL, shift);
|
| 81 |
+
const Tl = rotlL(curH, curL, shift);
|
| 82 |
+
const PI = SHA3_PI[t];
|
| 83 |
+
curH = s[PI];
|
| 84 |
+
curL = s[PI + 1];
|
| 85 |
+
s[PI] = Th;
|
| 86 |
+
s[PI + 1] = Tl;
|
| 87 |
+
}
|
| 88 |
+
// Chi (χ)
|
| 89 |
+
for (let y = 0; y < 50; y += 10) {
|
| 90 |
+
for (let x = 0; x < 10; x++) B[x] = s[y + x];
|
| 91 |
+
for (let x = 0; x < 10; x++) s[y + x] ^= ~B[(x + 2) % 10] & B[(x + 4) % 10];
|
| 92 |
+
}
|
| 93 |
+
// Iota (ι)
|
| 94 |
+
s[0] ^= SHA3_IOTA_H[round];
|
| 95 |
+
s[1] ^= SHA3_IOTA_L[round];
|
| 96 |
+
}
|
| 97 |
+
clean(B);
|
| 98 |
+
}
|
| 99 |
+
|
| 100 |
+
/** Keccak sponge function. */
|
| 101 |
+
export class Keccak extends Hash<Keccak> implements HashXOF<Keccak> {
|
| 102 |
+
protected state: Uint8Array;
|
| 103 |
+
protected pos = 0;
|
| 104 |
+
protected posOut = 0;
|
| 105 |
+
protected finished = false;
|
| 106 |
+
protected state32: Uint32Array;
|
| 107 |
+
protected destroyed = false;
|
| 108 |
+
|
| 109 |
+
public blockLen: number;
|
| 110 |
+
public suffix: number;
|
| 111 |
+
public outputLen: number;
|
| 112 |
+
protected enableXOF = false;
|
| 113 |
+
protected rounds: number;
|
| 114 |
+
|
| 115 |
+
// NOTE: we accept arguments in bytes instead of bits here.
|
| 116 |
+
constructor(
|
| 117 |
+
blockLen: number,
|
| 118 |
+
suffix: number,
|
| 119 |
+
outputLen: number,
|
| 120 |
+
enableXOF = false,
|
| 121 |
+
rounds: number = 24
|
| 122 |
+
) {
|
| 123 |
+
super();
|
| 124 |
+
this.blockLen = blockLen;
|
| 125 |
+
this.suffix = suffix;
|
| 126 |
+
this.outputLen = outputLen;
|
| 127 |
+
this.enableXOF = enableXOF;
|
| 128 |
+
this.rounds = rounds;
|
| 129 |
+
// Can be passed from user as dkLen
|
| 130 |
+
anumber(outputLen);
|
| 131 |
+
// 1600 = 5x5 matrix of 64bit. 1600 bits === 200 bytes
|
| 132 |
+
// 0 < blockLen < 200
|
| 133 |
+
if (!(0 < blockLen && blockLen < 200))
|
| 134 |
+
throw new Error('only keccak-f1600 function is supported');
|
| 135 |
+
this.state = new Uint8Array(200);
|
| 136 |
+
this.state32 = u32(this.state);
|
| 137 |
+
}
|
| 138 |
+
clone(): Keccak {
|
| 139 |
+
return this._cloneInto();
|
| 140 |
+
}
|
| 141 |
+
protected keccak(): void {
|
| 142 |
+
swap32IfBE(this.state32);
|
| 143 |
+
keccakP(this.state32, this.rounds);
|
| 144 |
+
swap32IfBE(this.state32);
|
| 145 |
+
this.posOut = 0;
|
| 146 |
+
this.pos = 0;
|
| 147 |
+
}
|
| 148 |
+
update(data: Input): this {
|
| 149 |
+
aexists(this);
|
| 150 |
+
data = toBytes(data);
|
| 151 |
+
abytes(data);
|
| 152 |
+
const { blockLen, state } = this;
|
| 153 |
+
const len = data.length;
|
| 154 |
+
for (let pos = 0; pos < len; ) {
|
| 155 |
+
const take = Math.min(blockLen - this.pos, len - pos);
|
| 156 |
+
for (let i = 0; i < take; i++) state[this.pos++] ^= data[pos++];
|
| 157 |
+
if (this.pos === blockLen) this.keccak();
|
| 158 |
+
}
|
| 159 |
+
return this;
|
| 160 |
+
}
|
| 161 |
+
protected finish(): void {
|
| 162 |
+
if (this.finished) return;
|
| 163 |
+
this.finished = true;
|
| 164 |
+
const { state, suffix, pos, blockLen } = this;
|
| 165 |
+
// Do the padding
|
| 166 |
+
state[pos] ^= suffix;
|
| 167 |
+
if ((suffix & 0x80) !== 0 && pos === blockLen - 1) this.keccak();
|
| 168 |
+
state[blockLen - 1] ^= 0x80;
|
| 169 |
+
this.keccak();
|
| 170 |
+
}
|
| 171 |
+
protected writeInto(out: Uint8Array): Uint8Array {
|
| 172 |
+
aexists(this, false);
|
| 173 |
+
abytes(out);
|
| 174 |
+
this.finish();
|
| 175 |
+
const bufferOut = this.state;
|
| 176 |
+
const { blockLen } = this;
|
| 177 |
+
for (let pos = 0, len = out.length; pos < len; ) {
|
| 178 |
+
if (this.posOut >= blockLen) this.keccak();
|
| 179 |
+
const take = Math.min(blockLen - this.posOut, len - pos);
|
| 180 |
+
out.set(bufferOut.subarray(this.posOut, this.posOut + take), pos);
|
| 181 |
+
this.posOut += take;
|
| 182 |
+
pos += take;
|
| 183 |
+
}
|
| 184 |
+
return out;
|
| 185 |
+
}
|
| 186 |
+
xofInto(out: Uint8Array): Uint8Array {
|
| 187 |
+
// Sha3/Keccak usage with XOF is probably mistake, only SHAKE instances can do XOF
|
| 188 |
+
if (!this.enableXOF) throw new Error('XOF is not possible for this instance');
|
| 189 |
+
return this.writeInto(out);
|
| 190 |
+
}
|
| 191 |
+
xof(bytes: number): Uint8Array {
|
| 192 |
+
anumber(bytes);
|
| 193 |
+
return this.xofInto(new Uint8Array(bytes));
|
| 194 |
+
}
|
| 195 |
+
digestInto(out: Uint8Array): Uint8Array {
|
| 196 |
+
aoutput(out, this);
|
| 197 |
+
if (this.finished) throw new Error('digest() was already called');
|
| 198 |
+
this.writeInto(out);
|
| 199 |
+
this.destroy();
|
| 200 |
+
return out;
|
| 201 |
+
}
|
| 202 |
+
digest(): Uint8Array {
|
| 203 |
+
return this.digestInto(new Uint8Array(this.outputLen));
|
| 204 |
+
}
|
| 205 |
+
destroy(): void {
|
| 206 |
+
this.destroyed = true;
|
| 207 |
+
clean(this.state);
|
| 208 |
+
}
|
| 209 |
+
_cloneInto(to?: Keccak): Keccak {
|
| 210 |
+
const { blockLen, suffix, outputLen, rounds, enableXOF } = this;
|
| 211 |
+
to ||= new Keccak(blockLen, suffix, outputLen, enableXOF, rounds);
|
| 212 |
+
to.state32.set(this.state32);
|
| 213 |
+
to.pos = this.pos;
|
| 214 |
+
to.posOut = this.posOut;
|
| 215 |
+
to.finished = this.finished;
|
| 216 |
+
to.rounds = rounds;
|
| 217 |
+
// Suffix can change in cSHAKE
|
| 218 |
+
to.suffix = suffix;
|
| 219 |
+
to.outputLen = outputLen;
|
| 220 |
+
to.enableXOF = enableXOF;
|
| 221 |
+
to.destroyed = this.destroyed;
|
| 222 |
+
return to;
|
| 223 |
+
}
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
const gen = (suffix: number, blockLen: number, outputLen: number) =>
|
| 227 |
+
createHasher(() => new Keccak(blockLen, suffix, outputLen));
|
| 228 |
+
|
| 229 |
+
/** SHA3-224 hash function. */
|
| 230 |
+
export const sha3_224: CHash = /* @__PURE__ */ (() => gen(0x06, 144, 224 / 8))();
|
| 231 |
+
/** SHA3-256 hash function. Different from keccak-256. */
|
| 232 |
+
export const sha3_256: CHash = /* @__PURE__ */ (() => gen(0x06, 136, 256 / 8))();
|
| 233 |
+
/** SHA3-384 hash function. */
|
| 234 |
+
export const sha3_384: CHash = /* @__PURE__ */ (() => gen(0x06, 104, 384 / 8))();
|
| 235 |
+
/** SHA3-512 hash function. */
|
| 236 |
+
export const sha3_512: CHash = /* @__PURE__ */ (() => gen(0x06, 72, 512 / 8))();
|
| 237 |
+
|
| 238 |
+
/** keccak-224 hash function. */
|
| 239 |
+
export const keccak_224: CHash = /* @__PURE__ */ (() => gen(0x01, 144, 224 / 8))();
|
| 240 |
+
/** keccak-256 hash function. Different from SHA3-256. */
|
| 241 |
+
export const keccak_256: CHash = /* @__PURE__ */ (() => gen(0x01, 136, 256 / 8))();
|
| 242 |
+
/** keccak-384 hash function. */
|
| 243 |
+
export const keccak_384: CHash = /* @__PURE__ */ (() => gen(0x01, 104, 384 / 8))();
|
| 244 |
+
/** keccak-512 hash function. */
|
| 245 |
+
export const keccak_512: CHash = /* @__PURE__ */ (() => gen(0x01, 72, 512 / 8))();
|
| 246 |
+
|
| 247 |
+
export type ShakeOpts = { dkLen?: number };
|
| 248 |
+
|
| 249 |
+
const genShake = (suffix: number, blockLen: number, outputLen: number) =>
|
| 250 |
+
createXOFer<HashXOF<Keccak>, ShakeOpts>(
|
| 251 |
+
(opts: ShakeOpts = {}) =>
|
| 252 |
+
new Keccak(blockLen, suffix, opts.dkLen === undefined ? outputLen : opts.dkLen, true)
|
| 253 |
+
);
|
| 254 |
+
|
| 255 |
+
/** SHAKE128 XOF with 128-bit security. */
|
| 256 |
+
export const shake128: CHashXO = /* @__PURE__ */ (() => genShake(0x1f, 168, 128 / 8))();
|
| 257 |
+
/** SHAKE256 XOF with 256-bit security. */
|
| 258 |
+
export const shake256: CHashXO = /* @__PURE__ */ (() => genShake(0x1f, 136, 256 / 8))();
|
node_modules/@noble/hashes/src/sha512.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* SHA2-512 a.k.a. sha512 and sha384. It is slower than sha256 in js because u64 operations are slow.
|
| 3 |
+
*
|
| 4 |
+
* Check out [RFC 4634](https://datatracker.ietf.org/doc/html/rfc4634) and
|
| 5 |
+
* [the paper on truncated SHA512/256](https://eprint.iacr.org/2010/548.pdf).
|
| 6 |
+
* @module
|
| 7 |
+
* @deprecated
|
| 8 |
+
*/
|
| 9 |
+
import {
|
| 10 |
+
SHA384 as SHA384n,
|
| 11 |
+
sha384 as sha384n,
|
| 12 |
+
sha512_224 as sha512_224n,
|
| 13 |
+
SHA512_224 as SHA512_224n,
|
| 14 |
+
sha512_256 as sha512_256n,
|
| 15 |
+
SHA512_256 as SHA512_256n,
|
| 16 |
+
SHA512 as SHA512n,
|
| 17 |
+
sha512 as sha512n,
|
| 18 |
+
} from './sha2.ts';
|
| 19 |
+
/** @deprecated Use import from `noble/hashes/sha2` module */
|
| 20 |
+
export const SHA512: typeof SHA512n = SHA512n;
|
| 21 |
+
/** @deprecated Use import from `noble/hashes/sha2` module */
|
| 22 |
+
export const sha512: typeof sha512n = sha512n;
|
| 23 |
+
/** @deprecated Use import from `noble/hashes/sha2` module */
|
| 24 |
+
export const SHA384: typeof SHA384n = SHA384n;
|
| 25 |
+
/** @deprecated Use import from `noble/hashes/sha2` module */
|
| 26 |
+
export const sha384: typeof sha384n = sha384n;
|
| 27 |
+
/** @deprecated Use import from `noble/hashes/sha2` module */
|
| 28 |
+
export const SHA512_224: typeof SHA512_224n = SHA512_224n;
|
| 29 |
+
/** @deprecated Use import from `noble/hashes/sha2` module */
|
| 30 |
+
export const sha512_224: typeof sha512_224n = sha512_224n;
|
| 31 |
+
/** @deprecated Use import from `noble/hashes/sha2` module */
|
| 32 |
+
export const SHA512_256: typeof SHA512_256n = SHA512_256n;
|
| 33 |
+
/** @deprecated Use import from `noble/hashes/sha2` module */
|
| 34 |
+
export const sha512_256: typeof sha512_256n = sha512_256n;
|
node_modules/@noble/hashes/src/utils.ts
ADDED
|
@@ -0,0 +1,395 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Utilities for hex, bytes, CSPRNG.
|
| 3 |
+
* @module
|
| 4 |
+
*/
|
| 5 |
+
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
| 6 |
+
|
| 7 |
+
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
|
| 8 |
+
// node.js versions earlier than v19 don't declare it in global scope.
|
| 9 |
+
// For node.js, package.json#exports field mapping rewrites import
|
| 10 |
+
// from `crypto` to `cryptoNode`, which imports native module.
|
| 11 |
+
// Makes the utils un-importable in browsers without a bundler.
|
| 12 |
+
// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
|
| 13 |
+
import { crypto } from '@noble/hashes/crypto';
|
| 14 |
+
|
| 15 |
+
/** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
|
| 16 |
+
export function isBytes(a: unknown): a is Uint8Array {
|
| 17 |
+
return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
/** Asserts something is positive integer. */
|
| 21 |
+
export function anumber(n: number): void {
|
| 22 |
+
if (!Number.isSafeInteger(n) || n < 0) throw new Error('positive integer expected, got ' + n);
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
/** Asserts something is Uint8Array. */
|
| 26 |
+
export function abytes(b: Uint8Array | undefined, ...lengths: number[]): void {
|
| 27 |
+
if (!isBytes(b)) throw new Error('Uint8Array expected');
|
| 28 |
+
if (lengths.length > 0 && !lengths.includes(b.length))
|
| 29 |
+
throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length);
|
| 30 |
+
}
|
| 31 |
+
|
| 32 |
+
/** Asserts something is hash */
|
| 33 |
+
export function ahash(h: IHash): void {
|
| 34 |
+
if (typeof h !== 'function' || typeof h.create !== 'function')
|
| 35 |
+
throw new Error('Hash should be wrapped by utils.createHasher');
|
| 36 |
+
anumber(h.outputLen);
|
| 37 |
+
anumber(h.blockLen);
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
/** Asserts a hash instance has not been destroyed / finished */
|
| 41 |
+
export function aexists(instance: any, checkFinished = true): void {
|
| 42 |
+
if (instance.destroyed) throw new Error('Hash instance has been destroyed');
|
| 43 |
+
if (checkFinished && instance.finished) throw new Error('Hash#digest() has already been called');
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
/** Asserts output is properly-sized byte array */
|
| 47 |
+
export function aoutput(out: any, instance: any): void {
|
| 48 |
+
abytes(out);
|
| 49 |
+
const min = instance.outputLen;
|
| 50 |
+
if (out.length < min) {
|
| 51 |
+
throw new Error('digestInto() expects output buffer of length at least ' + min);
|
| 52 |
+
}
|
| 53 |
+
}
|
| 54 |
+
|
| 55 |
+
/** Generic type encompassing 8/16/32-byte arrays - but not 64-byte. */
|
| 56 |
+
// prettier-ignore
|
| 57 |
+
export type TypedArray = Int8Array | Uint8ClampedArray | Uint8Array |
|
| 58 |
+
Uint16Array | Int16Array | Uint32Array | Int32Array;
|
| 59 |
+
|
| 60 |
+
/** Cast u8 / u16 / u32 to u8. */
|
| 61 |
+
export function u8(arr: TypedArray): Uint8Array {
|
| 62 |
+
return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
/** Cast u8 / u16 / u32 to u32. */
|
| 66 |
+
export function u32(arr: TypedArray): Uint32Array {
|
| 67 |
+
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
/** Zeroize a byte array. Warning: JS provides no guarantees. */
|
| 71 |
+
export function clean(...arrays: TypedArray[]): void {
|
| 72 |
+
for (let i = 0; i < arrays.length; i++) {
|
| 73 |
+
arrays[i].fill(0);
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
|
| 77 |
+
/** Create DataView of an array for easy byte-level manipulation. */
|
| 78 |
+
export function createView(arr: TypedArray): DataView {
|
| 79 |
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
| 80 |
+
}
|
| 81 |
+
|
| 82 |
+
/** The rotate right (circular right shift) operation for uint32 */
|
| 83 |
+
export function rotr(word: number, shift: number): number {
|
| 84 |
+
return (word << (32 - shift)) | (word >>> shift);
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
/** The rotate left (circular left shift) operation for uint32 */
|
| 88 |
+
export function rotl(word: number, shift: number): number {
|
| 89 |
+
return (word << shift) | ((word >>> (32 - shift)) >>> 0);
|
| 90 |
+
}
|
| 91 |
+
|
| 92 |
+
/** Is current platform little-endian? Most are. Big-Endian platform: IBM */
|
| 93 |
+
export const isLE: boolean = /* @__PURE__ */ (() =>
|
| 94 |
+
new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();
|
| 95 |
+
|
| 96 |
+
/** The byte swap operation for uint32 */
|
| 97 |
+
export function byteSwap(word: number): number {
|
| 98 |
+
return (
|
| 99 |
+
((word << 24) & 0xff000000) |
|
| 100 |
+
((word << 8) & 0xff0000) |
|
| 101 |
+
((word >>> 8) & 0xff00) |
|
| 102 |
+
((word >>> 24) & 0xff)
|
| 103 |
+
);
|
| 104 |
+
}
|
| 105 |
+
/** Conditionally byte swap if on a big-endian platform */
|
| 106 |
+
export const swap8IfBE: (n: number) => number = isLE
|
| 107 |
+
? (n: number) => n
|
| 108 |
+
: (n: number) => byteSwap(n);
|
| 109 |
+
|
| 110 |
+
/** @deprecated */
|
| 111 |
+
export const byteSwapIfBE: typeof swap8IfBE = swap8IfBE;
|
| 112 |
+
/** In place byte swap for Uint32Array */
|
| 113 |
+
export function byteSwap32(arr: Uint32Array): Uint32Array {
|
| 114 |
+
for (let i = 0; i < arr.length; i++) {
|
| 115 |
+
arr[i] = byteSwap(arr[i]);
|
| 116 |
+
}
|
| 117 |
+
return arr;
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
export const swap32IfBE: (u: Uint32Array) => Uint32Array = isLE
|
| 121 |
+
? (u: Uint32Array) => u
|
| 122 |
+
: byteSwap32;
|
| 123 |
+
|
| 124 |
+
// Built-in hex conversion https://caniuse.com/mdn-javascript_builtins_uint8array_fromhex
|
| 125 |
+
const hasHexBuiltin: boolean = /* @__PURE__ */ (() =>
|
| 126 |
+
// @ts-ignore
|
| 127 |
+
typeof Uint8Array.from([]).toHex === 'function' && typeof Uint8Array.fromHex === 'function')();
|
| 128 |
+
|
| 129 |
+
// Array where index 0xf0 (240) is mapped to string 'f0'
|
| 130 |
+
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) =>
|
| 131 |
+
i.toString(16).padStart(2, '0')
|
| 132 |
+
);
|
| 133 |
+
|
| 134 |
+
/**
|
| 135 |
+
* Convert byte array to hex string. Uses built-in function, when available.
|
| 136 |
+
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
|
| 137 |
+
*/
|
| 138 |
+
export function bytesToHex(bytes: Uint8Array): string {
|
| 139 |
+
abytes(bytes);
|
| 140 |
+
// @ts-ignore
|
| 141 |
+
if (hasHexBuiltin) return bytes.toHex();
|
| 142 |
+
// pre-caching improves the speed 6x
|
| 143 |
+
let hex = '';
|
| 144 |
+
for (let i = 0; i < bytes.length; i++) {
|
| 145 |
+
hex += hexes[bytes[i]];
|
| 146 |
+
}
|
| 147 |
+
return hex;
|
| 148 |
+
}
|
| 149 |
+
|
| 150 |
+
// We use optimized technique to convert hex string to byte array
|
| 151 |
+
const asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 } as const;
|
| 152 |
+
function asciiToBase16(ch: number): number | undefined {
|
| 153 |
+
if (ch >= asciis._0 && ch <= asciis._9) return ch - asciis._0; // '2' => 50-48
|
| 154 |
+
if (ch >= asciis.A && ch <= asciis.F) return ch - (asciis.A - 10); // 'B' => 66-(65-10)
|
| 155 |
+
if (ch >= asciis.a && ch <= asciis.f) return ch - (asciis.a - 10); // 'b' => 98-(97-10)
|
| 156 |
+
return;
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
/**
|
| 160 |
+
* Convert hex string to byte array. Uses built-in function, when available.
|
| 161 |
+
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
|
| 162 |
+
*/
|
| 163 |
+
export function hexToBytes(hex: string): Uint8Array {
|
| 164 |
+
if (typeof hex !== 'string') throw new Error('hex string expected, got ' + typeof hex);
|
| 165 |
+
// @ts-ignore
|
| 166 |
+
if (hasHexBuiltin) return Uint8Array.fromHex(hex);
|
| 167 |
+
const hl = hex.length;
|
| 168 |
+
const al = hl / 2;
|
| 169 |
+
if (hl % 2) throw new Error('hex string expected, got unpadded hex of length ' + hl);
|
| 170 |
+
const array = new Uint8Array(al);
|
| 171 |
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
| 172 |
+
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
| 173 |
+
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
| 174 |
+
if (n1 === undefined || n2 === undefined) {
|
| 175 |
+
const char = hex[hi] + hex[hi + 1];
|
| 176 |
+
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
| 177 |
+
}
|
| 178 |
+
array[ai] = n1 * 16 + n2; // multiply first octet, e.g. 'a3' => 10*16+3 => 160 + 3 => 163
|
| 179 |
+
}
|
| 180 |
+
return array;
|
| 181 |
+
}
|
| 182 |
+
|
| 183 |
+
/**
|
| 184 |
+
* There is no setImmediate in browser and setTimeout is slow.
|
| 185 |
+
* Call of async fn will return Promise, which will be fullfiled only on
|
| 186 |
+
* next scheduler queue processing step and this is exactly what we need.
|
| 187 |
+
*/
|
| 188 |
+
export const nextTick = async (): Promise<void> => {};
|
| 189 |
+
|
| 190 |
+
/** Returns control to thread each 'tick' ms to avoid blocking. */
|
| 191 |
+
export async function asyncLoop(
|
| 192 |
+
iters: number,
|
| 193 |
+
tick: number,
|
| 194 |
+
cb: (i: number) => void
|
| 195 |
+
): Promise<void> {
|
| 196 |
+
let ts = Date.now();
|
| 197 |
+
for (let i = 0; i < iters; i++) {
|
| 198 |
+
cb(i);
|
| 199 |
+
// Date.now() is not monotonic, so in case if clock goes backwards we return return control too
|
| 200 |
+
const diff = Date.now() - ts;
|
| 201 |
+
if (diff >= 0 && diff < tick) continue;
|
| 202 |
+
await nextTick();
|
| 203 |
+
ts += diff;
|
| 204 |
+
}
|
| 205 |
+
}
|
| 206 |
+
|
| 207 |
+
// Global symbols, but ts doesn't see them: https://github.com/microsoft/TypeScript/issues/31535
|
| 208 |
+
declare const TextEncoder: any;
|
| 209 |
+
declare const TextDecoder: any;
|
| 210 |
+
|
| 211 |
+
/**
|
| 212 |
+
* Converts string to bytes using UTF8 encoding.
|
| 213 |
+
* @example utf8ToBytes('abc') // Uint8Array.from([97, 98, 99])
|
| 214 |
+
*/
|
| 215 |
+
export function utf8ToBytes(str: string): Uint8Array {
|
| 216 |
+
if (typeof str !== 'string') throw new Error('string expected');
|
| 217 |
+
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
/**
|
| 221 |
+
* Converts bytes to string using UTF8 encoding.
|
| 222 |
+
* @example bytesToUtf8(Uint8Array.from([97, 98, 99])) // 'abc'
|
| 223 |
+
*/
|
| 224 |
+
export function bytesToUtf8(bytes: Uint8Array): string {
|
| 225 |
+
return new TextDecoder().decode(bytes);
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
/** Accepted input of hash functions. Strings are converted to byte arrays. */
|
| 229 |
+
export type Input = string | Uint8Array;
|
| 230 |
+
/**
|
| 231 |
+
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
|
| 232 |
+
* Warning: when Uint8Array is passed, it would NOT get copied.
|
| 233 |
+
* Keep in mind for future mutable operations.
|
| 234 |
+
*/
|
| 235 |
+
export function toBytes(data: Input): Uint8Array {
|
| 236 |
+
if (typeof data === 'string') data = utf8ToBytes(data);
|
| 237 |
+
abytes(data);
|
| 238 |
+
return data;
|
| 239 |
+
}
|
| 240 |
+
|
| 241 |
+
/** KDFs can accept string or Uint8Array for user convenience. */
|
| 242 |
+
export type KDFInput = string | Uint8Array;
|
| 243 |
+
/**
|
| 244 |
+
* Helper for KDFs: consumes uint8array or string.
|
| 245 |
+
* When string is passed, does utf8 decoding, using TextDecoder.
|
| 246 |
+
*/
|
| 247 |
+
export function kdfInputToBytes(data: KDFInput): Uint8Array {
|
| 248 |
+
if (typeof data === 'string') data = utf8ToBytes(data);
|
| 249 |
+
abytes(data);
|
| 250 |
+
return data;
|
| 251 |
+
}
|
| 252 |
+
|
| 253 |
+
/** Copies several Uint8Arrays into one. */
|
| 254 |
+
export function concatBytes(...arrays: Uint8Array[]): Uint8Array {
|
| 255 |
+
let sum = 0;
|
| 256 |
+
for (let i = 0; i < arrays.length; i++) {
|
| 257 |
+
const a = arrays[i];
|
| 258 |
+
abytes(a);
|
| 259 |
+
sum += a.length;
|
| 260 |
+
}
|
| 261 |
+
const res = new Uint8Array(sum);
|
| 262 |
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
| 263 |
+
const a = arrays[i];
|
| 264 |
+
res.set(a, pad);
|
| 265 |
+
pad += a.length;
|
| 266 |
+
}
|
| 267 |
+
return res;
|
| 268 |
+
}
|
| 269 |
+
|
| 270 |
+
type EmptyObj = {};
|
| 271 |
+
export function checkOpts<T1 extends EmptyObj, T2 extends EmptyObj>(
|
| 272 |
+
defaults: T1,
|
| 273 |
+
opts?: T2
|
| 274 |
+
): T1 & T2 {
|
| 275 |
+
if (opts !== undefined && {}.toString.call(opts) !== '[object Object]')
|
| 276 |
+
throw new Error('options should be object or undefined');
|
| 277 |
+
const merged = Object.assign(defaults, opts);
|
| 278 |
+
return merged as T1 & T2;
|
| 279 |
+
}
|
| 280 |
+
|
| 281 |
+
/** Hash interface. */
|
| 282 |
+
export type IHash = {
|
| 283 |
+
(data: Uint8Array): Uint8Array;
|
| 284 |
+
blockLen: number;
|
| 285 |
+
outputLen: number;
|
| 286 |
+
create: any;
|
| 287 |
+
};
|
| 288 |
+
|
| 289 |
+
/** For runtime check if class implements interface */
|
| 290 |
+
export abstract class Hash<T extends Hash<T>> {
|
| 291 |
+
abstract blockLen: number; // Bytes per block
|
| 292 |
+
abstract outputLen: number; // Bytes in output
|
| 293 |
+
abstract update(buf: Input): this;
|
| 294 |
+
// Writes digest into buf
|
| 295 |
+
abstract digestInto(buf: Uint8Array): void;
|
| 296 |
+
abstract digest(): Uint8Array;
|
| 297 |
+
/**
|
| 298 |
+
* Resets internal state. Makes Hash instance unusable.
|
| 299 |
+
* Reset is impossible for keyed hashes if key is consumed into state. If digest is not consumed
|
| 300 |
+
* by user, they will need to manually call `destroy()` when zeroing is necessary.
|
| 301 |
+
*/
|
| 302 |
+
abstract destroy(): void;
|
| 303 |
+
/**
|
| 304 |
+
* Clones hash instance. Unsafe: doesn't check whether `to` is valid. Can be used as `clone()`
|
| 305 |
+
* when no options are passed.
|
| 306 |
+
* Reasons to use `_cloneInto` instead of clone: 1) performance 2) reuse instance => all internal
|
| 307 |
+
* buffers are overwritten => causes buffer overwrite which is used for digest in some cases.
|
| 308 |
+
* There are no guarantees for clean-up because it's impossible in JS.
|
| 309 |
+
*/
|
| 310 |
+
abstract _cloneInto(to?: T): T;
|
| 311 |
+
// Safe version that clones internal state
|
| 312 |
+
abstract clone(): T;
|
| 313 |
+
}
|
| 314 |
+
|
| 315 |
+
/**
|
| 316 |
+
* XOF: streaming API to read digest in chunks.
|
| 317 |
+
* Same as 'squeeze' in keccak/k12 and 'seek' in blake3, but more generic name.
|
| 318 |
+
* When hash used in XOF mode it is up to user to call '.destroy' afterwards, since we cannot
|
| 319 |
+
* destroy state, next call can require more bytes.
|
| 320 |
+
*/
|
| 321 |
+
export type HashXOF<T extends Hash<T>> = Hash<T> & {
|
| 322 |
+
xof(bytes: number): Uint8Array; // Read 'bytes' bytes from digest stream
|
| 323 |
+
xofInto(buf: Uint8Array): Uint8Array; // read buf.length bytes from digest stream into buf
|
| 324 |
+
};
|
| 325 |
+
|
| 326 |
+
/** Hash function */
|
| 327 |
+
export type CHash = ReturnType<typeof createHasher>;
|
| 328 |
+
/** Hash function with output */
|
| 329 |
+
export type CHashO = ReturnType<typeof createOptHasher>;
|
| 330 |
+
/** XOF with output */
|
| 331 |
+
export type CHashXO = ReturnType<typeof createXOFer>;
|
| 332 |
+
|
| 333 |
+
/** Wraps hash function, creating an interface on top of it */
|
| 334 |
+
export function createHasher<T extends Hash<T>>(
|
| 335 |
+
hashCons: () => Hash<T>
|
| 336 |
+
): {
|
| 337 |
+
(msg: Input): Uint8Array;
|
| 338 |
+
outputLen: number;
|
| 339 |
+
blockLen: number;
|
| 340 |
+
create(): Hash<T>;
|
| 341 |
+
} {
|
| 342 |
+
const hashC = (msg: Input): Uint8Array => hashCons().update(toBytes(msg)).digest();
|
| 343 |
+
const tmp = hashCons();
|
| 344 |
+
hashC.outputLen = tmp.outputLen;
|
| 345 |
+
hashC.blockLen = tmp.blockLen;
|
| 346 |
+
hashC.create = () => hashCons();
|
| 347 |
+
return hashC;
|
| 348 |
+
}
|
| 349 |
+
|
| 350 |
+
export function createOptHasher<H extends Hash<H>, T extends Object>(
|
| 351 |
+
hashCons: (opts?: T) => Hash<H>
|
| 352 |
+
): {
|
| 353 |
+
(msg: Input, opts?: T): Uint8Array;
|
| 354 |
+
outputLen: number;
|
| 355 |
+
blockLen: number;
|
| 356 |
+
create(opts?: T): Hash<H>;
|
| 357 |
+
} {
|
| 358 |
+
const hashC = (msg: Input, opts?: T): Uint8Array => hashCons(opts).update(toBytes(msg)).digest();
|
| 359 |
+
const tmp = hashCons({} as T);
|
| 360 |
+
hashC.outputLen = tmp.outputLen;
|
| 361 |
+
hashC.blockLen = tmp.blockLen;
|
| 362 |
+
hashC.create = (opts?: T) => hashCons(opts);
|
| 363 |
+
return hashC;
|
| 364 |
+
}
|
| 365 |
+
|
| 366 |
+
export function createXOFer<H extends HashXOF<H>, T extends Object>(
|
| 367 |
+
hashCons: (opts?: T) => HashXOF<H>
|
| 368 |
+
): {
|
| 369 |
+
(msg: Input, opts?: T): Uint8Array;
|
| 370 |
+
outputLen: number;
|
| 371 |
+
blockLen: number;
|
| 372 |
+
create(opts?: T): HashXOF<H>;
|
| 373 |
+
} {
|
| 374 |
+
const hashC = (msg: Input, opts?: T): Uint8Array => hashCons(opts).update(toBytes(msg)).digest();
|
| 375 |
+
const tmp = hashCons({} as T);
|
| 376 |
+
hashC.outputLen = tmp.outputLen;
|
| 377 |
+
hashC.blockLen = tmp.blockLen;
|
| 378 |
+
hashC.create = (opts?: T) => hashCons(opts);
|
| 379 |
+
return hashC;
|
| 380 |
+
}
|
| 381 |
+
export const wrapConstructor: typeof createHasher = createHasher;
|
| 382 |
+
export const wrapConstructorWithOpts: typeof createOptHasher = createOptHasher;
|
| 383 |
+
export const wrapXOFConstructorWithOpts: typeof createXOFer = createXOFer;
|
| 384 |
+
|
| 385 |
+
/** Cryptographically secure PRNG. Uses internal OS-level `crypto.getRandomValues`. */
|
| 386 |
+
export function randomBytes(bytesLength = 32): Uint8Array {
|
| 387 |
+
if (crypto && typeof crypto.getRandomValues === 'function') {
|
| 388 |
+
return crypto.getRandomValues(new Uint8Array(bytesLength));
|
| 389 |
+
}
|
| 390 |
+
// Legacy Node.js compatibility
|
| 391 |
+
if (crypto && typeof crypto.randomBytes === 'function') {
|
| 392 |
+
return Uint8Array.from(crypto.randomBytes(bytesLength));
|
| 393 |
+
}
|
| 394 |
+
throw new Error('crypto.getRandomValues must be defined');
|
| 395 |
+
}
|
node_modules/@noble/hashes/utils.d.ts
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* Utilities for hex, bytes, CSPRNG.
|
| 3 |
+
* @module
|
| 4 |
+
*/
|
| 5 |
+
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
| 6 |
+
/** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
|
| 7 |
+
export declare function isBytes(a: unknown): a is Uint8Array;
|
| 8 |
+
/** Asserts something is positive integer. */
|
| 9 |
+
export declare function anumber(n: number): void;
|
| 10 |
+
/** Asserts something is Uint8Array. */
|
| 11 |
+
export declare function abytes(b: Uint8Array | undefined, ...lengths: number[]): void;
|
| 12 |
+
/** Asserts something is hash */
|
| 13 |
+
export declare function ahash(h: IHash): void;
|
| 14 |
+
/** Asserts a hash instance has not been destroyed / finished */
|
| 15 |
+
export declare function aexists(instance: any, checkFinished?: boolean): void;
|
| 16 |
+
/** Asserts output is properly-sized byte array */
|
| 17 |
+
export declare function aoutput(out: any, instance: any): void;
|
| 18 |
+
/** Generic type encompassing 8/16/32-byte arrays - but not 64-byte. */
|
| 19 |
+
export type TypedArray = Int8Array | Uint8ClampedArray | Uint8Array | Uint16Array | Int16Array | Uint32Array | Int32Array;
|
| 20 |
+
/** Cast u8 / u16 / u32 to u8. */
|
| 21 |
+
export declare function u8(arr: TypedArray): Uint8Array;
|
| 22 |
+
/** Cast u8 / u16 / u32 to u32. */
|
| 23 |
+
export declare function u32(arr: TypedArray): Uint32Array;
|
| 24 |
+
/** Zeroize a byte array. Warning: JS provides no guarantees. */
|
| 25 |
+
export declare function clean(...arrays: TypedArray[]): void;
|
| 26 |
+
/** Create DataView of an array for easy byte-level manipulation. */
|
| 27 |
+
export declare function createView(arr: TypedArray): DataView;
|
| 28 |
+
/** The rotate right (circular right shift) operation for uint32 */
|
| 29 |
+
export declare function rotr(word: number, shift: number): number;
|
| 30 |
+
/** The rotate left (circular left shift) operation for uint32 */
|
| 31 |
+
export declare function rotl(word: number, shift: number): number;
|
| 32 |
+
/** Is current platform little-endian? Most are. Big-Endian platform: IBM */
|
| 33 |
+
export declare const isLE: boolean;
|
| 34 |
+
/** The byte swap operation for uint32 */
|
| 35 |
+
export declare function byteSwap(word: number): number;
|
| 36 |
+
/** Conditionally byte swap if on a big-endian platform */
|
| 37 |
+
export declare const swap8IfBE: (n: number) => number;
|
| 38 |
+
/** @deprecated */
|
| 39 |
+
export declare const byteSwapIfBE: typeof swap8IfBE;
|
| 40 |
+
/** In place byte swap for Uint32Array */
|
| 41 |
+
export declare function byteSwap32(arr: Uint32Array): Uint32Array;
|
| 42 |
+
export declare const swap32IfBE: (u: Uint32Array) => Uint32Array;
|
| 43 |
+
/**
|
| 44 |
+
* Convert byte array to hex string. Uses built-in function, when available.
|
| 45 |
+
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
|
| 46 |
+
*/
|
| 47 |
+
export declare function bytesToHex(bytes: Uint8Array): string;
|
| 48 |
+
/**
|
| 49 |
+
* Convert hex string to byte array. Uses built-in function, when available.
|
| 50 |
+
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
|
| 51 |
+
*/
|
| 52 |
+
export declare function hexToBytes(hex: string): Uint8Array;
|
| 53 |
+
/**
|
| 54 |
+
* There is no setImmediate in browser and setTimeout is slow.
|
| 55 |
+
* Call of async fn will return Promise, which will be fullfiled only on
|
| 56 |
+
* next scheduler queue processing step and this is exactly what we need.
|
| 57 |
+
*/
|
| 58 |
+
export declare const nextTick: () => Promise<void>;
|
| 59 |
+
/** Returns control to thread each 'tick' ms to avoid blocking. */
|
| 60 |
+
export declare function asyncLoop(iters: number, tick: number, cb: (i: number) => void): Promise<void>;
|
| 61 |
+
/**
|
| 62 |
+
* Converts string to bytes using UTF8 encoding.
|
| 63 |
+
* @example utf8ToBytes('abc') // Uint8Array.from([97, 98, 99])
|
| 64 |
+
*/
|
| 65 |
+
export declare function utf8ToBytes(str: string): Uint8Array;
|
| 66 |
+
/**
|
| 67 |
+
* Converts bytes to string using UTF8 encoding.
|
| 68 |
+
* @example bytesToUtf8(Uint8Array.from([97, 98, 99])) // 'abc'
|
| 69 |
+
*/
|
| 70 |
+
export declare function bytesToUtf8(bytes: Uint8Array): string;
|
| 71 |
+
/** Accepted input of hash functions. Strings are converted to byte arrays. */
|
| 72 |
+
export type Input = string | Uint8Array;
|
| 73 |
+
/**
|
| 74 |
+
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
|
| 75 |
+
* Warning: when Uint8Array is passed, it would NOT get copied.
|
| 76 |
+
* Keep in mind for future mutable operations.
|
| 77 |
+
*/
|
| 78 |
+
export declare function toBytes(data: Input): Uint8Array;
|
| 79 |
+
/** KDFs can accept string or Uint8Array for user convenience. */
|
| 80 |
+
export type KDFInput = string | Uint8Array;
|
| 81 |
+
/**
|
| 82 |
+
* Helper for KDFs: consumes uint8array or string.
|
| 83 |
+
* When string is passed, does utf8 decoding, using TextDecoder.
|
| 84 |
+
*/
|
| 85 |
+
export declare function kdfInputToBytes(data: KDFInput): Uint8Array;
|
| 86 |
+
/** Copies several Uint8Arrays into one. */
|
| 87 |
+
export declare function concatBytes(...arrays: Uint8Array[]): Uint8Array;
|
| 88 |
+
type EmptyObj = {};
|
| 89 |
+
export declare function checkOpts<T1 extends EmptyObj, T2 extends EmptyObj>(defaults: T1, opts?: T2): T1 & T2;
|
| 90 |
+
/** Hash interface. */
|
| 91 |
+
export type IHash = {
|
| 92 |
+
(data: Uint8Array): Uint8Array;
|
| 93 |
+
blockLen: number;
|
| 94 |
+
outputLen: number;
|
| 95 |
+
create: any;
|
| 96 |
+
};
|
| 97 |
+
/** For runtime check if class implements interface */
|
| 98 |
+
export declare abstract class Hash<T extends Hash<T>> {
|
| 99 |
+
abstract blockLen: number;
|
| 100 |
+
abstract outputLen: number;
|
| 101 |
+
abstract update(buf: Input): this;
|
| 102 |
+
abstract digestInto(buf: Uint8Array): void;
|
| 103 |
+
abstract digest(): Uint8Array;
|
| 104 |
+
/**
|
| 105 |
+
* Resets internal state. Makes Hash instance unusable.
|
| 106 |
+
* Reset is impossible for keyed hashes if key is consumed into state. If digest is not consumed
|
| 107 |
+
* by user, they will need to manually call `destroy()` when zeroing is necessary.
|
| 108 |
+
*/
|
| 109 |
+
abstract destroy(): void;
|
| 110 |
+
/**
|
| 111 |
+
* Clones hash instance. Unsafe: doesn't check whether `to` is valid. Can be used as `clone()`
|
| 112 |
+
* when no options are passed.
|
| 113 |
+
* Reasons to use `_cloneInto` instead of clone: 1) performance 2) reuse instance => all internal
|
| 114 |
+
* buffers are overwritten => causes buffer overwrite which is used for digest in some cases.
|
| 115 |
+
* There are no guarantees for clean-up because it's impossible in JS.
|
| 116 |
+
*/
|
| 117 |
+
abstract _cloneInto(to?: T): T;
|
| 118 |
+
abstract clone(): T;
|
| 119 |
+
}
|
| 120 |
+
/**
|
| 121 |
+
* XOF: streaming API to read digest in chunks.
|
| 122 |
+
* Same as 'squeeze' in keccak/k12 and 'seek' in blake3, but more generic name.
|
| 123 |
+
* When hash used in XOF mode it is up to user to call '.destroy' afterwards, since we cannot
|
| 124 |
+
* destroy state, next call can require more bytes.
|
| 125 |
+
*/
|
| 126 |
+
export type HashXOF<T extends Hash<T>> = Hash<T> & {
|
| 127 |
+
xof(bytes: number): Uint8Array;
|
| 128 |
+
xofInto(buf: Uint8Array): Uint8Array;
|
| 129 |
+
};
|
| 130 |
+
/** Hash function */
|
| 131 |
+
export type CHash = ReturnType<typeof createHasher>;
|
| 132 |
+
/** Hash function with output */
|
| 133 |
+
export type CHashO = ReturnType<typeof createOptHasher>;
|
| 134 |
+
/** XOF with output */
|
| 135 |
+
export type CHashXO = ReturnType<typeof createXOFer>;
|
| 136 |
+
/** Wraps hash function, creating an interface on top of it */
|
| 137 |
+
export declare function createHasher<T extends Hash<T>>(hashCons: () => Hash<T>): {
|
| 138 |
+
(msg: Input): Uint8Array;
|
| 139 |
+
outputLen: number;
|
| 140 |
+
blockLen: number;
|
| 141 |
+
create(): Hash<T>;
|
| 142 |
+
};
|
| 143 |
+
export declare function createOptHasher<H extends Hash<H>, T extends Object>(hashCons: (opts?: T) => Hash<H>): {
|
| 144 |
+
(msg: Input, opts?: T): Uint8Array;
|
| 145 |
+
outputLen: number;
|
| 146 |
+
blockLen: number;
|
| 147 |
+
create(opts?: T): Hash<H>;
|
| 148 |
+
};
|
| 149 |
+
export declare function createXOFer<H extends HashXOF<H>, T extends Object>(hashCons: (opts?: T) => HashXOF<H>): {
|
| 150 |
+
(msg: Input, opts?: T): Uint8Array;
|
| 151 |
+
outputLen: number;
|
| 152 |
+
blockLen: number;
|
| 153 |
+
create(opts?: T): HashXOF<H>;
|
| 154 |
+
};
|
| 155 |
+
export declare const wrapConstructor: typeof createHasher;
|
| 156 |
+
export declare const wrapConstructorWithOpts: typeof createOptHasher;
|
| 157 |
+
export declare const wrapXOFConstructorWithOpts: typeof createXOFer;
|
| 158 |
+
/** Cryptographically secure PRNG. Uses internal OS-level `crypto.getRandomValues`. */
|
| 159 |
+
export declare function randomBytes(bytesLength?: number): Uint8Array;
|
| 160 |
+
export {};
|
| 161 |
+
//# sourceMappingURL=utils.d.ts.map
|
node_modules/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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"use strict";
|
| 2 |
+
/**
|
| 3 |
+
* Utilities for hex, bytes, CSPRNG.
|
| 4 |
+
* @module
|
| 5 |
+
*/
|
| 6 |
+
/*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) */
|
| 7 |
+
Object.defineProperty(exports, "__esModule", { value: true });
|
| 8 |
+
exports.wrapXOFConstructorWithOpts = exports.wrapConstructorWithOpts = exports.wrapConstructor = exports.Hash = exports.nextTick = exports.swap32IfBE = exports.byteSwapIfBE = exports.swap8IfBE = exports.isLE = void 0;
|
| 9 |
+
exports.isBytes = isBytes;
|
| 10 |
+
exports.anumber = anumber;
|
| 11 |
+
exports.abytes = abytes;
|
| 12 |
+
exports.ahash = ahash;
|
| 13 |
+
exports.aexists = aexists;
|
| 14 |
+
exports.aoutput = aoutput;
|
| 15 |
+
exports.u8 = u8;
|
| 16 |
+
exports.u32 = u32;
|
| 17 |
+
exports.clean = clean;
|
| 18 |
+
exports.createView = createView;
|
| 19 |
+
exports.rotr = rotr;
|
| 20 |
+
exports.rotl = rotl;
|
| 21 |
+
exports.byteSwap = byteSwap;
|
| 22 |
+
exports.byteSwap32 = byteSwap32;
|
| 23 |
+
exports.bytesToHex = bytesToHex;
|
| 24 |
+
exports.hexToBytes = hexToBytes;
|
| 25 |
+
exports.asyncLoop = asyncLoop;
|
| 26 |
+
exports.utf8ToBytes = utf8ToBytes;
|
| 27 |
+
exports.bytesToUtf8 = bytesToUtf8;
|
| 28 |
+
exports.toBytes = toBytes;
|
| 29 |
+
exports.kdfInputToBytes = kdfInputToBytes;
|
| 30 |
+
exports.concatBytes = concatBytes;
|
| 31 |
+
exports.checkOpts = checkOpts;
|
| 32 |
+
exports.createHasher = createHasher;
|
| 33 |
+
exports.createOptHasher = createOptHasher;
|
| 34 |
+
exports.createXOFer = createXOFer;
|
| 35 |
+
exports.randomBytes = randomBytes;
|
| 36 |
+
// We use WebCrypto aka globalThis.crypto, which exists in browsers and node.js 16+.
|
| 37 |
+
// node.js versions earlier than v19 don't declare it in global scope.
|
| 38 |
+
// For node.js, package.json#exports field mapping rewrites import
|
| 39 |
+
// from `crypto` to `cryptoNode`, which imports native module.
|
| 40 |
+
// Makes the utils un-importable in browsers without a bundler.
|
| 41 |
+
// Once node.js 18 is deprecated (2025-04-30), we can just drop the import.
|
| 42 |
+
const crypto_1 = require("@noble/hashes/crypto");
|
| 43 |
+
/** Checks if something is Uint8Array. Be careful: nodejs Buffer will return true. */
|
| 44 |
+
function isBytes(a) {
|
| 45 |
+
return a instanceof Uint8Array || (ArrayBuffer.isView(a) && a.constructor.name === 'Uint8Array');
|
| 46 |
+
}
|
| 47 |
+
/** Asserts something is positive integer. */
|
| 48 |
+
function anumber(n) {
|
| 49 |
+
if (!Number.isSafeInteger(n) || n < 0)
|
| 50 |
+
throw new Error('positive integer expected, got ' + n);
|
| 51 |
+
}
|
| 52 |
+
/** Asserts something is Uint8Array. */
|
| 53 |
+
function abytes(b, ...lengths) {
|
| 54 |
+
if (!isBytes(b))
|
| 55 |
+
throw new Error('Uint8Array expected');
|
| 56 |
+
if (lengths.length > 0 && !lengths.includes(b.length))
|
| 57 |
+
throw new Error('Uint8Array expected of length ' + lengths + ', got length=' + b.length);
|
| 58 |
+
}
|
| 59 |
+
/** Asserts something is hash */
|
| 60 |
+
function ahash(h) {
|
| 61 |
+
if (typeof h !== 'function' || typeof h.create !== 'function')
|
| 62 |
+
throw new Error('Hash should be wrapped by utils.createHasher');
|
| 63 |
+
anumber(h.outputLen);
|
| 64 |
+
anumber(h.blockLen);
|
| 65 |
+
}
|
| 66 |
+
/** Asserts a hash instance has not been destroyed / finished */
|
| 67 |
+
function aexists(instance, checkFinished = true) {
|
| 68 |
+
if (instance.destroyed)
|
| 69 |
+
throw new Error('Hash instance has been destroyed');
|
| 70 |
+
if (checkFinished && instance.finished)
|
| 71 |
+
throw new Error('Hash#digest() has already been called');
|
| 72 |
+
}
|
| 73 |
+
/** Asserts output is properly-sized byte array */
|
| 74 |
+
function aoutput(out, instance) {
|
| 75 |
+
abytes(out);
|
| 76 |
+
const min = instance.outputLen;
|
| 77 |
+
if (out.length < min) {
|
| 78 |
+
throw new Error('digestInto() expects output buffer of length at least ' + min);
|
| 79 |
+
}
|
| 80 |
+
}
|
| 81 |
+
/** Cast u8 / u16 / u32 to u8. */
|
| 82 |
+
function u8(arr) {
|
| 83 |
+
return new Uint8Array(arr.buffer, arr.byteOffset, arr.byteLength);
|
| 84 |
+
}
|
| 85 |
+
/** Cast u8 / u16 / u32 to u32. */
|
| 86 |
+
function u32(arr) {
|
| 87 |
+
return new Uint32Array(arr.buffer, arr.byteOffset, Math.floor(arr.byteLength / 4));
|
| 88 |
+
}
|
| 89 |
+
/** Zeroize a byte array. Warning: JS provides no guarantees. */
|
| 90 |
+
function clean(...arrays) {
|
| 91 |
+
for (let i = 0; i < arrays.length; i++) {
|
| 92 |
+
arrays[i].fill(0);
|
| 93 |
+
}
|
| 94 |
+
}
|
| 95 |
+
/** Create DataView of an array for easy byte-level manipulation. */
|
| 96 |
+
function createView(arr) {
|
| 97 |
+
return new DataView(arr.buffer, arr.byteOffset, arr.byteLength);
|
| 98 |
+
}
|
| 99 |
+
/** The rotate right (circular right shift) operation for uint32 */
|
| 100 |
+
function rotr(word, shift) {
|
| 101 |
+
return (word << (32 - shift)) | (word >>> shift);
|
| 102 |
+
}
|
| 103 |
+
/** The rotate left (circular left shift) operation for uint32 */
|
| 104 |
+
function rotl(word, shift) {
|
| 105 |
+
return (word << shift) | ((word >>> (32 - shift)) >>> 0);
|
| 106 |
+
}
|
| 107 |
+
/** Is current platform little-endian? Most are. Big-Endian platform: IBM */
|
| 108 |
+
exports.isLE = (() => new Uint8Array(new Uint32Array([0x11223344]).buffer)[0] === 0x44)();
|
| 109 |
+
/** The byte swap operation for uint32 */
|
| 110 |
+
function byteSwap(word) {
|
| 111 |
+
return (((word << 24) & 0xff000000) |
|
| 112 |
+
((word << 8) & 0xff0000) |
|
| 113 |
+
((word >>> 8) & 0xff00) |
|
| 114 |
+
((word >>> 24) & 0xff));
|
| 115 |
+
}
|
| 116 |
+
/** Conditionally byte swap if on a big-endian platform */
|
| 117 |
+
exports.swap8IfBE = exports.isLE
|
| 118 |
+
? (n) => n
|
| 119 |
+
: (n) => byteSwap(n);
|
| 120 |
+
/** @deprecated */
|
| 121 |
+
exports.byteSwapIfBE = exports.swap8IfBE;
|
| 122 |
+
/** In place byte swap for Uint32Array */
|
| 123 |
+
function byteSwap32(arr) {
|
| 124 |
+
for (let i = 0; i < arr.length; i++) {
|
| 125 |
+
arr[i] = byteSwap(arr[i]);
|
| 126 |
+
}
|
| 127 |
+
return arr;
|
| 128 |
+
}
|
| 129 |
+
exports.swap32IfBE = exports.isLE
|
| 130 |
+
? (u) => u
|
| 131 |
+
: byteSwap32;
|
| 132 |
+
// Built-in hex conversion https://caniuse.com/mdn-javascript_builtins_uint8array_fromhex
|
| 133 |
+
const hasHexBuiltin = /* @__PURE__ */ (() =>
|
| 134 |
+
// @ts-ignore
|
| 135 |
+
typeof Uint8Array.from([]).toHex === 'function' && typeof Uint8Array.fromHex === 'function')();
|
| 136 |
+
// Array where index 0xf0 (240) is mapped to string 'f0'
|
| 137 |
+
const hexes = /* @__PURE__ */ Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, '0'));
|
| 138 |
+
/**
|
| 139 |
+
* Convert byte array to hex string. Uses built-in function, when available.
|
| 140 |
+
* @example bytesToHex(Uint8Array.from([0xca, 0xfe, 0x01, 0x23])) // 'cafe0123'
|
| 141 |
+
*/
|
| 142 |
+
function bytesToHex(bytes) {
|
| 143 |
+
abytes(bytes);
|
| 144 |
+
// @ts-ignore
|
| 145 |
+
if (hasHexBuiltin)
|
| 146 |
+
return bytes.toHex();
|
| 147 |
+
// pre-caching improves the speed 6x
|
| 148 |
+
let hex = '';
|
| 149 |
+
for (let i = 0; i < bytes.length; i++) {
|
| 150 |
+
hex += hexes[bytes[i]];
|
| 151 |
+
}
|
| 152 |
+
return hex;
|
| 153 |
+
}
|
| 154 |
+
// We use optimized technique to convert hex string to byte array
|
| 155 |
+
const asciis = { _0: 48, _9: 57, A: 65, F: 70, a: 97, f: 102 };
|
| 156 |
+
function asciiToBase16(ch) {
|
| 157 |
+
if (ch >= asciis._0 && ch <= asciis._9)
|
| 158 |
+
return ch - asciis._0; // '2' => 50-48
|
| 159 |
+
if (ch >= asciis.A && ch <= asciis.F)
|
| 160 |
+
return ch - (asciis.A - 10); // 'B' => 66-(65-10)
|
| 161 |
+
if (ch >= asciis.a && ch <= asciis.f)
|
| 162 |
+
return ch - (asciis.a - 10); // 'b' => 98-(97-10)
|
| 163 |
+
return;
|
| 164 |
+
}
|
| 165 |
+
/**
|
| 166 |
+
* Convert hex string to byte array. Uses built-in function, when available.
|
| 167 |
+
* @example hexToBytes('cafe0123') // Uint8Array.from([0xca, 0xfe, 0x01, 0x23])
|
| 168 |
+
*/
|
| 169 |
+
function hexToBytes(hex) {
|
| 170 |
+
if (typeof hex !== 'string')
|
| 171 |
+
throw new Error('hex string expected, got ' + typeof hex);
|
| 172 |
+
// @ts-ignore
|
| 173 |
+
if (hasHexBuiltin)
|
| 174 |
+
return Uint8Array.fromHex(hex);
|
| 175 |
+
const hl = hex.length;
|
| 176 |
+
const al = hl / 2;
|
| 177 |
+
if (hl % 2)
|
| 178 |
+
throw new Error('hex string expected, got unpadded hex of length ' + hl);
|
| 179 |
+
const array = new Uint8Array(al);
|
| 180 |
+
for (let ai = 0, hi = 0; ai < al; ai++, hi += 2) {
|
| 181 |
+
const n1 = asciiToBase16(hex.charCodeAt(hi));
|
| 182 |
+
const n2 = asciiToBase16(hex.charCodeAt(hi + 1));
|
| 183 |
+
if (n1 === undefined || n2 === undefined) {
|
| 184 |
+
const char = hex[hi] + hex[hi + 1];
|
| 185 |
+
throw new Error('hex string expected, got non-hex character "' + char + '" at index ' + hi);
|
| 186 |
+
}
|
| 187 |
+
array[ai] = n1 * 16 + n2; // multiply first octet, e.g. 'a3' => 10*16+3 => 160 + 3 => 163
|
| 188 |
+
}
|
| 189 |
+
return array;
|
| 190 |
+
}
|
| 191 |
+
/**
|
| 192 |
+
* There is no setImmediate in browser and setTimeout is slow.
|
| 193 |
+
* Call of async fn will return Promise, which will be fullfiled only on
|
| 194 |
+
* next scheduler queue processing step and this is exactly what we need.
|
| 195 |
+
*/
|
| 196 |
+
const nextTick = async () => { };
|
| 197 |
+
exports.nextTick = nextTick;
|
| 198 |
+
/** Returns control to thread each 'tick' ms to avoid blocking. */
|
| 199 |
+
async function asyncLoop(iters, tick, cb) {
|
| 200 |
+
let ts = Date.now();
|
| 201 |
+
for (let i = 0; i < iters; i++) {
|
| 202 |
+
cb(i);
|
| 203 |
+
// Date.now() is not monotonic, so in case if clock goes backwards we return return control too
|
| 204 |
+
const diff = Date.now() - ts;
|
| 205 |
+
if (diff >= 0 && diff < tick)
|
| 206 |
+
continue;
|
| 207 |
+
await (0, exports.nextTick)();
|
| 208 |
+
ts += diff;
|
| 209 |
+
}
|
| 210 |
+
}
|
| 211 |
+
/**
|
| 212 |
+
* Converts string to bytes using UTF8 encoding.
|
| 213 |
+
* @example utf8ToBytes('abc') // Uint8Array.from([97, 98, 99])
|
| 214 |
+
*/
|
| 215 |
+
function utf8ToBytes(str) {
|
| 216 |
+
if (typeof str !== 'string')
|
| 217 |
+
throw new Error('string expected');
|
| 218 |
+
return new Uint8Array(new TextEncoder().encode(str)); // https://bugzil.la/1681809
|
| 219 |
+
}
|
| 220 |
+
/**
|
| 221 |
+
* Converts bytes to string using UTF8 encoding.
|
| 222 |
+
* @example bytesToUtf8(Uint8Array.from([97, 98, 99])) // 'abc'
|
| 223 |
+
*/
|
| 224 |
+
function bytesToUtf8(bytes) {
|
| 225 |
+
return new TextDecoder().decode(bytes);
|
| 226 |
+
}
|
| 227 |
+
/**
|
| 228 |
+
* Normalizes (non-hex) string or Uint8Array to Uint8Array.
|
| 229 |
+
* Warning: when Uint8Array is passed, it would NOT get copied.
|
| 230 |
+
* Keep in mind for future mutable operations.
|
| 231 |
+
*/
|
| 232 |
+
function toBytes(data) {
|
| 233 |
+
if (typeof data === 'string')
|
| 234 |
+
data = utf8ToBytes(data);
|
| 235 |
+
abytes(data);
|
| 236 |
+
return data;
|
| 237 |
+
}
|
| 238 |
+
/**
|
| 239 |
+
* Helper for KDFs: consumes uint8array or string.
|
| 240 |
+
* When string is passed, does utf8 decoding, using TextDecoder.
|
| 241 |
+
*/
|
| 242 |
+
function kdfInputToBytes(data) {
|
| 243 |
+
if (typeof data === 'string')
|
| 244 |
+
data = utf8ToBytes(data);
|
| 245 |
+
abytes(data);
|
| 246 |
+
return data;
|
| 247 |
+
}
|
| 248 |
+
/** Copies several Uint8Arrays into one. */
|
| 249 |
+
function concatBytes(...arrays) {
|
| 250 |
+
let sum = 0;
|
| 251 |
+
for (let i = 0; i < arrays.length; i++) {
|
| 252 |
+
const a = arrays[i];
|
| 253 |
+
abytes(a);
|
| 254 |
+
sum += a.length;
|
| 255 |
+
}
|
| 256 |
+
const res = new Uint8Array(sum);
|
| 257 |
+
for (let i = 0, pad = 0; i < arrays.length; i++) {
|
| 258 |
+
const a = arrays[i];
|
| 259 |
+
res.set(a, pad);
|
| 260 |
+
pad += a.length;
|
| 261 |
+
}
|
| 262 |
+
return res;
|
| 263 |
+
}
|
| 264 |
+
function checkOpts(defaults, opts) {
|
| 265 |
+
if (opts !== undefined && {}.toString.call(opts) !== '[object Object]')
|
| 266 |
+
throw new Error('options should be object or undefined');
|
| 267 |
+
const merged = Object.assign(defaults, opts);
|
| 268 |
+
return merged;
|
| 269 |
+
}
|
| 270 |
+
/** For runtime check if class implements interface */
|
| 271 |
+
class Hash {
|
| 272 |
+
}
|
| 273 |
+
exports.Hash = Hash;
|
| 274 |
+
/** Wraps hash function, creating an interface on top of it */
|
| 275 |
+
function createHasher(hashCons) {
|
| 276 |
+
const hashC = (msg) => hashCons().update(toBytes(msg)).digest();
|
| 277 |
+
const tmp = hashCons();
|
| 278 |
+
hashC.outputLen = tmp.outputLen;
|
| 279 |
+
hashC.blockLen = tmp.blockLen;
|
| 280 |
+
hashC.create = () => hashCons();
|
| 281 |
+
return hashC;
|
| 282 |
+
}
|
| 283 |
+
function createOptHasher(hashCons) {
|
| 284 |
+
const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest();
|
| 285 |
+
const tmp = hashCons({});
|
| 286 |
+
hashC.outputLen = tmp.outputLen;
|
| 287 |
+
hashC.blockLen = tmp.blockLen;
|
| 288 |
+
hashC.create = (opts) => hashCons(opts);
|
| 289 |
+
return hashC;
|
| 290 |
+
}
|
| 291 |
+
function createXOFer(hashCons) {
|
| 292 |
+
const hashC = (msg, opts) => hashCons(opts).update(toBytes(msg)).digest();
|
| 293 |
+
const tmp = hashCons({});
|
| 294 |
+
hashC.outputLen = tmp.outputLen;
|
| 295 |
+
hashC.blockLen = tmp.blockLen;
|
| 296 |
+
hashC.create = (opts) => hashCons(opts);
|
| 297 |
+
return hashC;
|
| 298 |
+
}
|
| 299 |
+
exports.wrapConstructor = createHasher;
|
| 300 |
+
exports.wrapConstructorWithOpts = createOptHasher;
|
| 301 |
+
exports.wrapXOFConstructorWithOpts = createXOFer;
|
| 302 |
+
/** Cryptographically secure PRNG. Uses internal OS-level `crypto.getRandomValues`. */
|
| 303 |
+
function randomBytes(bytesLength = 32) {
|
| 304 |
+
if (crypto_1.crypto && typeof crypto_1.crypto.getRandomValues === 'function') {
|
| 305 |
+
return crypto_1.crypto.getRandomValues(new Uint8Array(bytesLength));
|
| 306 |
+
}
|
| 307 |
+
// Legacy Node.js compatibility
|
| 308 |
+
if (crypto_1.crypto && typeof crypto_1.crypto.randomBytes === 'function') {
|
| 309 |
+
return Uint8Array.from(crypto_1.crypto.randomBytes(bytesLength));
|
| 310 |
+
}
|
| 311 |
+
throw new Error('crypto.getRandomValues must be defined');
|
| 312 |
+
}
|
| 313 |
+
//# sourceMappingURL=utils.js.map
|
node_modules/@noble/hashes/utils.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["src/utils.ts"],"names":[],"mappings":";AAAA;;;GAGG;AACH,sEAAsE;;;AAWtE,0BAEC;AAGD,0BAEC;AAGD,wBAIC;AAGD,sBAKC;AAGD,0BAGC;AAGD,0BAMC;AAQD,gBAEC;AAGD,kBAEC;AAGD,sBAIC;AAGD,gCAEC;AAGD,oBAEC;AAGD,oBAEC;AAOD,4BAOC;AASD,gCAKC;AAoBD,gCAUC;AAeD,gCAkBC;AAUD,8BAcC;AAUD,kCAGC;AAMD,kCAEC;AASD,0BAIC;AAQD,0CAIC;AAGD,kCAcC;AAGD,8BAQC;AAuDD,oCAcC;AAED,0CAcC;AAED,kCAcC;AAMD,kCASC;AApYD,oFAAoF;AACpF,sEAAsE;AACtE,kEAAkE;AAClE,8DAA8D;AAC9D,+DAA+D;AAC/D,2EAA2E;AAC3E,iDAA8C;AAE9C,qFAAqF;AACrF,SAAgB,OAAO,CAAC,CAAU;IAChC,OAAO,CAAC,YAAY,UAAU,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,WAAW,CAAC,IAAI,KAAK,YAAY,CAAC,CAAC;AACnG,CAAC;AAED,6CAA6C;AAC7C,SAAgB,OAAO,CAAC,CAAS;IAC/B,IAAI,CAAC,MAAM,C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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# @nodable/entities
|
| 2 |
+
|
| 3 |
+
Fast, zero-dependency XML/HTML entity encoder and decoder for Node.js.
|
| 4 |
+
|
| 5 |
+
## Install
|
| 6 |
+
|
| 7 |
+
```bash
|
| 8 |
+
npm install @nodable/entities
|
| 9 |
+
```
|
| 10 |
+
|
| 11 |
+
## Quick start
|
| 12 |
+
|
| 13 |
+
```js
|
| 14 |
+
import { EntityEncoder, EntityDecoder, ALL_ENTITIES } from '@nodable/entities';
|
| 15 |
+
|
| 16 |
+
// Encode: plain text → entity references
|
| 17 |
+
const enc = new EntityEncoder();
|
| 18 |
+
enc.encode('Hello © 2024 & <stuff>');
|
| 19 |
+
// → 'Hello © 2024 & <stuff>'
|
| 20 |
+
|
| 21 |
+
// Decode: entity references → plain text
|
| 22 |
+
const dec = new EntityDecoder({ namedEntities: ALL_ENTITIES });
|
| 23 |
+
dec.decode('Hello © 2024 & <stuff>');
|
| 24 |
+
// → 'Hello © 2024 & <stuff>'
|
| 25 |
+
```
|
| 26 |
+
|
| 27 |
+
## Performance
|
| 28 |
+
|
| 29 |
+
| | encode | decode |
|
| 30 |
+
|---|---|---|
|
| 31 |
+
| `entities` (npm) | 3.65 M req/s | 1.76 M req/s |
|
| 32 |
+
| `@nodable/entities` | 3.33 M req/s | **5.19 M req/s** |
|
| 33 |
+
|
| 34 |
+
## Documentation
|
| 35 |
+
|
| 36 |
+
- [EntityEncoder](docs/EntityEncoder.md) — options, API, recipes
|
| 37 |
+
- [EntityDecoder](docs/EntityDecoder.md) — options, API, security limits, entity sets
|
| 38 |
+
|
| 39 |
+
## License
|
| 40 |
+
|
| 41 |
+
MIT
|
node_modules/@nodable/entities/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@nodable/entities",
|
| 3 |
+
"version": "2.2.0",
|
| 4 |
+
"description": "Entity parser for XML, HTML, External entites with security and NCR control",
|
| 5 |
+
"main": "./src/index.js",
|
| 6 |
+
"type": "module",
|
| 7 |
+
"sideEffects": false,
|
| 8 |
+
"types": "./src/index.d.ts",
|
| 9 |
+
"scripts": {
|
| 10 |
+
"test": "node --experimental-vm-modules node_modules/.bin/jest",
|
| 11 |
+
"test:watch": "node --experimental-vm-modules node_modules/.bin/jest --watch",
|
| 12 |
+
"test:coverage": "node --experimental-vm-modules node_modules/.bin/jest --coverage",
|
| 13 |
+
"lint": "eslint src/ test/"
|
| 14 |
+
},
|
| 15 |
+
"files": [
|
| 16 |
+
"src",
|
| 17 |
+
"README.md"
|
| 18 |
+
],
|
| 19 |
+
"repository": {
|
| 20 |
+
"type": "git",
|
| 21 |
+
"url": "git+https://github.com/nodable/val-parsers.git"
|
| 22 |
+
},
|
| 23 |
+
"keywords": [
|
| 24 |
+
"fast",
|
| 25 |
+
"xml",
|
| 26 |
+
"html",
|
| 27 |
+
"entity",
|
| 28 |
+
"entities",
|
| 29 |
+
"stringify",
|
| 30 |
+
"encode",
|
| 31 |
+
"decode",
|
| 32 |
+
"ncr",
|
| 33 |
+
"security",
|
| 34 |
+
"safe",
|
| 35 |
+
"performance"
|
| 36 |
+
],
|
| 37 |
+
"author": "Amit Gupta (https://solothought.com)",
|
| 38 |
+
"license": "MIT",
|
| 39 |
+
"publishConfig": {
|
| 40 |
+
"access": "public"
|
| 41 |
+
},
|
| 42 |
+
"devDependencies": {
|
| 43 |
+
"jest": "^29.7.0"
|
| 44 |
+
},
|
| 45 |
+
"funding": [
|
| 46 |
+
{
|
| 47 |
+
"type": "github",
|
| 48 |
+
"url": "https://github.com/sponsors/nodable"
|
| 49 |
+
}
|
| 50 |
+
],
|
| 51 |
+
"jest": {
|
| 52 |
+
"testMatch": [
|
| 53 |
+
"**/?(*.)+(spec|test).[jt]s?(x)",
|
| 54 |
+
"**/*_spec.[jt]s?(x)"
|
| 55 |
+
]
|
| 56 |
+
}
|
| 57 |
+
}
|
node_modules/@nodable/entities/src/EntityDecoder.js
ADDED
|
@@ -0,0 +1,644 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// ---------------------------------------------------------------------------
|
| 2 |
+
// Built-in named entity map (name → replacement string)
|
| 3 |
+
// No regex, no {regex,val} objects — just flat key/value pairs.
|
| 4 |
+
// ---------------------------------------------------------------------------
|
| 5 |
+
|
| 6 |
+
import { XML as DEFAULT_XML_ENTITIES } from "./entities.js"
|
| 7 |
+
|
| 8 |
+
// ---------------------------------------------------------------------------
|
| 9 |
+
// Entity hook action constants
|
| 10 |
+
// ---------------------------------------------------------------------------
|
| 11 |
+
|
| 12 |
+
/**
|
| 13 |
+
* Action constants for `onExternalEntity` and `onInputEntity` hooks.
|
| 14 |
+
*
|
| 15 |
+
* Use these instead of raw strings to avoid typos:
|
| 16 |
+
*
|
| 17 |
+
* @example
|
| 18 |
+
* import EntityDecoder, { ENTITY_ACTION } from './EntityDecoder.js';
|
| 19 |
+
* const dec = new EntityDecoder({
|
| 20 |
+
* onInputEntity: (name, value) => ENTITY_ACTION.BLOCK,
|
| 21 |
+
* });
|
| 22 |
+
*/
|
| 23 |
+
export const ENTITY_ACTION = Object.freeze({
|
| 24 |
+
/** Resolve and expand the entity normally. */
|
| 25 |
+
ALLOW: 'allow',
|
| 26 |
+
/** Silently skip this entity — it will not be registered. */
|
| 27 |
+
BLOCK: 'block',
|
| 28 |
+
/** Throw an error, aborting entity registration entirely. */
|
| 29 |
+
THROW: 'throw',
|
| 30 |
+
});
|
| 31 |
+
|
| 32 |
+
// ---------------------------------------------------------------------------
|
| 33 |
+
// Helpers
|
| 34 |
+
// ---------------------------------------------------------------------------
|
| 35 |
+
|
| 36 |
+
const SPECIAL_CHARS = new Set('!?\\\\/[]$%{}^&*()<>|+');
|
| 37 |
+
|
| 38 |
+
/**
|
| 39 |
+
* Validate that an entity name contains no dangerous characters.
|
| 40 |
+
* @param {string} name
|
| 41 |
+
* @returns {string} the name, unchanged
|
| 42 |
+
* @throws {Error} on invalid characters
|
| 43 |
+
*/
|
| 44 |
+
function validateEntityName(name) {
|
| 45 |
+
if (name[0] === '#') {
|
| 46 |
+
throw new Error(`[EntityReplacer] Invalid character '#' in entity name: "${name}"`);
|
| 47 |
+
}
|
| 48 |
+
for (const ch of name) {
|
| 49 |
+
if (SPECIAL_CHARS.has(ch)) {
|
| 50 |
+
throw new Error(`[EntityReplacer] Invalid character '${ch}' in entity name: "${name}"`);
|
| 51 |
+
}
|
| 52 |
+
}
|
| 53 |
+
return name;
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
/**
|
| 57 |
+
* Merge one or more entity maps into a flat name→string map.
|
| 58 |
+
* Accepts either:
|
| 59 |
+
* - plain string values: { amp: '&' }
|
| 60 |
+
* - legacy {regex,val} / {regx,val}: { lt: { regex: /.../, val: '<' } }
|
| 61 |
+
*
|
| 62 |
+
* Values containing '&' are skipped (recursive expansion risk).
|
| 63 |
+
*
|
| 64 |
+
* @param {...object} maps
|
| 65 |
+
* @returns {Record<string, string>}
|
| 66 |
+
*/
|
| 67 |
+
function mergeEntityMaps(...maps) {
|
| 68 |
+
const out = Object.create(null);
|
| 69 |
+
for (const map of maps) {
|
| 70 |
+
if (!map) continue;
|
| 71 |
+
for (const key of Object.keys(map)) {
|
| 72 |
+
const raw = map[key];
|
| 73 |
+
if (typeof raw === 'string') {
|
| 74 |
+
out[key] = raw;
|
| 75 |
+
} else if (raw && typeof raw === 'object' && raw.val !== undefined) {
|
| 76 |
+
// Legacy {regex,val} or {regx,val} — extract the string val only
|
| 77 |
+
const val = raw.val;
|
| 78 |
+
if (typeof val === 'string') {
|
| 79 |
+
out[key] = val;
|
| 80 |
+
}
|
| 81 |
+
// function vals are not supported in the scanner — skip
|
| 82 |
+
}
|
| 83 |
+
}
|
| 84 |
+
}
|
| 85 |
+
return out;
|
| 86 |
+
}
|
| 87 |
+
|
| 88 |
+
// ---------------------------------------------------------------------------
|
| 89 |
+
// applyLimitsTo helpers
|
| 90 |
+
// ---------------------------------------------------------------------------
|
| 91 |
+
|
| 92 |
+
const LIMIT_TIER_EXTERNAL = 'external'; // input/runtime + persistent external maps
|
| 93 |
+
const LIMIT_TIER_BASE = 'base'; // DEFAULT_XML_ENTITIES + namedEntities (system) maps
|
| 94 |
+
const LIMIT_TIER_ALL = 'all'; // every entity regardless of tier
|
| 95 |
+
|
| 96 |
+
/**
|
| 97 |
+
* Resolve `applyLimitsTo` option into a normalised Set of tier strings.
|
| 98 |
+
* Accepted values: 'external' | 'base' | 'all' | string[]
|
| 99 |
+
* Default: 'external' (only untrusted injected entities are counted).
|
| 100 |
+
* @param {string|string[]|undefined} raw
|
| 101 |
+
* @returns {Set<string>}
|
| 102 |
+
*/
|
| 103 |
+
function parseLimitTiers(raw) {
|
| 104 |
+
if (!raw || raw === LIMIT_TIER_EXTERNAL) return new Set([LIMIT_TIER_EXTERNAL]);
|
| 105 |
+
if (raw === LIMIT_TIER_ALL) return new Set([LIMIT_TIER_ALL]);
|
| 106 |
+
if (raw === LIMIT_TIER_BASE) return new Set([LIMIT_TIER_BASE]);
|
| 107 |
+
if (Array.isArray(raw)) return new Set(raw);
|
| 108 |
+
return new Set([LIMIT_TIER_EXTERNAL]); // safe default for unrecognised values
|
| 109 |
+
}
|
| 110 |
+
|
| 111 |
+
// ---------------------------------------------------------------------------
|
| 112 |
+
// NCR (Numeric Character Reference) classification
|
| 113 |
+
// ---------------------------------------------------------------------------
|
| 114 |
+
|
| 115 |
+
// Severity order — higher number = stricter action.
|
| 116 |
+
// Used to enforce minimum action levels for specific codepoint ranges.
|
| 117 |
+
const NCR_LEVEL = Object.freeze({ allow: 0, leave: 1, remove: 2, throw: 3 });
|
| 118 |
+
|
| 119 |
+
// XML 1.0 §2.2: allowed chars are #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
|
| 120 |
+
// Restricted C0: U+0001–U+001F excluding U+0009, U+000A, U+000D
|
| 121 |
+
const XML10_ALLOWED_C0 = new Set([0x09, 0x0A, 0x0D]);
|
| 122 |
+
|
| 123 |
+
/**
|
| 124 |
+
* Parse the `ncr` constructor option into flat, hot-path-friendly fields.
|
| 125 |
+
* @param {object|undefined} ncr
|
| 126 |
+
* @returns {{ xmlVersion: number, onLevel: number, nullLevel: number }}
|
| 127 |
+
*/
|
| 128 |
+
function parseNCRConfig(ncr) {
|
| 129 |
+
if (!ncr) {
|
| 130 |
+
return { xmlVersion: 1.0, onLevel: NCR_LEVEL.allow, nullLevel: NCR_LEVEL.remove };
|
| 131 |
+
}
|
| 132 |
+
const xmlVersion = ncr.xmlVersion === 1.1 ? 1.1 : 1.0;
|
| 133 |
+
const onLevel = NCR_LEVEL[ncr.onNCR] ?? NCR_LEVEL.allow;
|
| 134 |
+
const nullLevel = NCR_LEVEL[ncr.nullNCR] ?? NCR_LEVEL.remove;
|
| 135 |
+
// 'allow' is not meaningful for null — clamp to at least 'remove'
|
| 136 |
+
const clampedNull = Math.max(nullLevel, NCR_LEVEL.remove);
|
| 137 |
+
return { xmlVersion, onLevel, nullLevel: clampedNull };
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
// ---------------------------------------------------------------------------
|
| 141 |
+
// EntityReplacer
|
| 142 |
+
// ---------------------------------------------------------------------------
|
| 143 |
+
|
| 144 |
+
/**
|
| 145 |
+
* Single-pass, zero-regex entity replacer for XML/HTML content.
|
| 146 |
+
*
|
| 147 |
+
* Algorithm: scan the string once for '&', read to ';', resolve via map
|
| 148 |
+
* or direct codepoint conversion, build output chunks, join once at the end.
|
| 149 |
+
*
|
| 150 |
+
* Entity lookup priority (highest → lowest):
|
| 151 |
+
* 1. input / runtime (DOCTYPE entities for current document)
|
| 152 |
+
* 2. persistent external (survive across documents)
|
| 153 |
+
* 3. base named map (DEFAULT_XML_ENTITIES + user-supplied namedEntities)
|
| 154 |
+
*
|
| 155 |
+
* Both input and external resolve as the 'external' tier for limit purposes.
|
| 156 |
+
* Base map entities resolve as the 'base' tier.
|
| 157 |
+
*
|
| 158 |
+
* Numeric / hex references (&#NNN; / &#xHH;) are resolved directly via
|
| 159 |
+
* String.fromCodePoint() — no map needed. They count as 'base' tier.
|
| 160 |
+
*
|
| 161 |
+
* @example
|
| 162 |
+
* const replacer = new EntityReplacer({ namedEntities: COMMON_HTML });
|
| 163 |
+
* replacer.setExternalEntities({ brand: 'Acme' });
|
| 164 |
+
*
|
| 165 |
+
* const instance = replacer.reset();
|
| 166 |
+
* instance.addInputEntities({ version: '1.0' });
|
| 167 |
+
* instance.encode('&brand; v&version; <'); // 'Acme v1.0 <'
|
| 168 |
+
*/
|
| 169 |
+
export default class EntityDecoder {
|
| 170 |
+
/**
|
| 171 |
+
* @param {object} [options]
|
| 172 |
+
* @param {object|null} [options.namedEntities] — extra named entities merged into base map
|
| 173 |
+
* @param {object} [options.limit] — security limits
|
| 174 |
+
* @param {number} [options.limit.maxTotalExpansions=0] — 0 = unlimited
|
| 175 |
+
* @param {number} [options.limit.maxExpandedLength=0] — 0 = unlimited
|
| 176 |
+
* @param {'external'|'base'|'all'|string[]} [options.limit.applyLimitsTo='external']
|
| 177 |
+
* Which entity tiers count against the security limits:
|
| 178 |
+
* - 'external' (default) — only input/runtime + persistent external entities
|
| 179 |
+
* - 'base' — only DEFAULT_XML_ENTITIES + namedEntities
|
| 180 |
+
* - 'all' — every entity regardless of tier
|
| 181 |
+
* - string[] — explicit combination, e.g. ['external', 'base']
|
| 182 |
+
* @param {((resolved: string, original: string) => string)|null} [options.postCheck=null]
|
| 183 |
+
* @param {string[]} [options.remove=[]] — entity names (e.g. ['nbsp', '#13']) to delete (replace with empty string)
|
| 184 |
+
* @param {string[]} [options.leave=[]] — entity names to keep as literal (unchanged in output)
|
| 185 |
+
* @param {object} [options.ncr] — Numeric Character Reference controls
|
| 186 |
+
* @param {1.0|1.1} [options.ncr.xmlVersion=1.0]
|
| 187 |
+
* XML version governing which codepoint ranges are restricted:
|
| 188 |
+
* - 1.0 — C0 controls U+0001–U+001F (except U+0009/000A/000D) are prohibited
|
| 189 |
+
* - 1.1 — C0 controls are allowed when written as NCRs; C1 (U+007F–U+009F) decoded as-is
|
| 190 |
+
* @param {'allow'|'leave'|'remove'|'throw'} [options.ncr.onNCR='allow']
|
| 191 |
+
* Base action for numeric references. Severity order: allow < leave < remove < throw.
|
| 192 |
+
* For codepoint ranges that carry a minimum level (surrogates → remove, XML 1.0 C0 → remove),
|
| 193 |
+
* the effective action is max(onNCR, rangeMinimum).
|
| 194 |
+
* @param {'remove'|'throw'} [options.ncr.nullNCR='remove']
|
| 195 |
+
* Action for U+0000 (null). 'allow' and 'leave' are clamped to 'remove' since null is never safe.
|
| 196 |
+
* @param {((name: string, value: string) => 'allow'|'block'|'throw')|null} [options.onExternalEntity=null]
|
| 197 |
+
* Hook called when an external entity is registered via `setExternalEntities()` or
|
| 198 |
+
* `addExternalEntity()`. Return `ENTITY_ACTION.ALLOW` to accept the entity,
|
| 199 |
+
* `ENTITY_ACTION.BLOCK` to silently skip it, or `ENTITY_ACTION.THROW` to abort with an error.
|
| 200 |
+
* @param {((name: string, value: string) => 'allow'|'block'|'throw')|null} [options.onInputEntity=null]
|
| 201 |
+
* Hook called when an input entity is registered via `addInputEntities()`. Return
|
| 202 |
+
* `ENTITY_ACTION.ALLOW` to accept, `ENTITY_ACTION.BLOCK` to silently skip, or
|
| 203 |
+
* `ENTITY_ACTION.THROW` to abort with an error.
|
| 204 |
+
*/
|
| 205 |
+
constructor(options = {}) {
|
| 206 |
+
this._limit = options.limit || {};
|
| 207 |
+
this._maxTotalExpansions = this._limit.maxTotalExpansions || 0;
|
| 208 |
+
this._maxExpandedLength = this._limit.maxExpandedLength || 0;
|
| 209 |
+
this._postCheck = typeof options.postCheck === 'function' ? options.postCheck : r => r;
|
| 210 |
+
this._limitTiers = parseLimitTiers(this._limit.applyLimitsTo ?? LIMIT_TIER_EXTERNAL);
|
| 211 |
+
this._numericAllowed = options.numericAllowed ?? true;
|
| 212 |
+
// Base map: DEFAULT_XML_ENTITIES + user-supplied extras. Immutable after construction.
|
| 213 |
+
this._baseMap = mergeEntityMaps(DEFAULT_XML_ENTITIES, options.namedEntities || null);
|
| 214 |
+
|
| 215 |
+
// Persistent external entities — survive across documents.
|
| 216 |
+
// Stored as a separate map so reset() never touches them.
|
| 217 |
+
/** @type {Record<string, string>} */
|
| 218 |
+
this._externalMap = Object.create(null);
|
| 219 |
+
|
| 220 |
+
// Input / runtime entities — current document only, wiped on reset().
|
| 221 |
+
/** @type {Record<string, string>} */
|
| 222 |
+
this._inputMap = Object.create(null);
|
| 223 |
+
|
| 224 |
+
// Per-document counters
|
| 225 |
+
this._totalExpansions = 0;
|
| 226 |
+
this._expandedLength = 0;
|
| 227 |
+
|
| 228 |
+
// --- New: remove / leave sets ---
|
| 229 |
+
/** @type {Set<string>} */
|
| 230 |
+
this._removeSet = new Set(options.remove && Array.isArray(options.remove) ? options.remove : []);
|
| 231 |
+
/** @type {Set<string>} */
|
| 232 |
+
this._leaveSet = new Set(options.leave && Array.isArray(options.leave) ? options.leave : []);
|
| 233 |
+
|
| 234 |
+
// --- NCR config (parsed into flat fields for hot-path speed) ---
|
| 235 |
+
const ncrCfg = parseNCRConfig(options.ncr);
|
| 236 |
+
this._ncrXmlVersion = ncrCfg.xmlVersion;
|
| 237 |
+
this._ncrOnLevel = ncrCfg.onLevel;
|
| 238 |
+
this._ncrNullLevel = ncrCfg.nullLevel;
|
| 239 |
+
|
| 240 |
+
// --- Registration hooks ---
|
| 241 |
+
/** @type {((name: string, value: string) => 'allow'|'block'|'throw')|null} */
|
| 242 |
+
this._onExternalEntity = typeof options.onExternalEntity === 'function'
|
| 243 |
+
? options.onExternalEntity
|
| 244 |
+
: null;
|
| 245 |
+
/** @type {((name: string, value: string) => 'allow'|'block'|'throw')|null} */
|
| 246 |
+
this._onInputEntity = typeof options.onInputEntity === 'function'
|
| 247 |
+
? options.onInputEntity
|
| 248 |
+
: null;
|
| 249 |
+
}
|
| 250 |
+
|
| 251 |
+
// -------------------------------------------------------------------------
|
| 252 |
+
// Private: registration hook dispatch
|
| 253 |
+
// -------------------------------------------------------------------------
|
| 254 |
+
|
| 255 |
+
/**
|
| 256 |
+
* Invoke a registration hook for a single entity name/value pair.
|
| 257 |
+
* Returns true when the entity should be accepted, false when it should be
|
| 258 |
+
* silently skipped (BLOCK), and throws when the hook returns THROW.
|
| 259 |
+
*
|
| 260 |
+
* @param {((name: string, value: string) => 'allow'|'block'|'throw')|null} hook
|
| 261 |
+
* @param {string} name
|
| 262 |
+
* @param {string} value
|
| 263 |
+
* @param {string} context — used in error messages ('external' | 'input')
|
| 264 |
+
* @returns {boolean} true = accept, false = skip
|
| 265 |
+
*/
|
| 266 |
+
_applyRegistrationHook(hook, name, value, context) {
|
| 267 |
+
if (!hook) return true; // no hook → always accept
|
| 268 |
+
const action = hook(name, value);
|
| 269 |
+
if (action === ENTITY_ACTION.BLOCK) return false;
|
| 270 |
+
if (action === ENTITY_ACTION.THROW) {
|
| 271 |
+
throw new Error(
|
| 272 |
+
`[EntityDecoder] Registration of ${context} entity "&${name};" was rejected by hook`
|
| 273 |
+
);
|
| 274 |
+
}
|
| 275 |
+
return true; // ALLOW or any unknown return value → accept
|
| 276 |
+
}
|
| 277 |
+
|
| 278 |
+
// -------------------------------------------------------------------------
|
| 279 |
+
// Persistent external entity registration
|
| 280 |
+
// -------------------------------------------------------------------------
|
| 281 |
+
|
| 282 |
+
/**
|
| 283 |
+
* Replace the full set of persistent external entities.
|
| 284 |
+
* All keys are validated — throws on invalid characters.
|
| 285 |
+
* If `onExternalEntity` is set, it is called once per entry; entries that
|
| 286 |
+
* return `ENTITY_ACTION.BLOCK` are silently omitted, `ENTITY_ACTION.THROW`
|
| 287 |
+
* aborts the whole call.
|
| 288 |
+
* @param {Record<string, string | { regex?: RegExp, val: string }>} map
|
| 289 |
+
*/
|
| 290 |
+
setExternalEntities(map) {
|
| 291 |
+
if (map) {
|
| 292 |
+
for (const key of Object.keys(map)) {
|
| 293 |
+
validateEntityName(key);
|
| 294 |
+
}
|
| 295 |
+
}
|
| 296 |
+
if (!this._onExternalEntity) {
|
| 297 |
+
this._externalMap = mergeEntityMaps(map);
|
| 298 |
+
return;
|
| 299 |
+
}
|
| 300 |
+
// Hook present — resolve values first, then filter
|
| 301 |
+
const flat = mergeEntityMaps(map);
|
| 302 |
+
const filtered = Object.create(null);
|
| 303 |
+
for (const [name, value] of Object.entries(flat)) {
|
| 304 |
+
if (this._applyRegistrationHook(this._onExternalEntity, name, value, 'external')) {
|
| 305 |
+
filtered[name] = value;
|
| 306 |
+
}
|
| 307 |
+
}
|
| 308 |
+
this._externalMap = filtered;
|
| 309 |
+
}
|
| 310 |
+
|
| 311 |
+
/**
|
| 312 |
+
* Add a single persistent external entity.
|
| 313 |
+
* If `onExternalEntity` is set it is called before the entity is stored;
|
| 314 |
+
* `ENTITY_ACTION.BLOCK` silently skips storage, `ENTITY_ACTION.THROW` raises.
|
| 315 |
+
* @param {string} key
|
| 316 |
+
* @param {string} value
|
| 317 |
+
*/
|
| 318 |
+
addExternalEntity(key, value) {
|
| 319 |
+
validateEntityName(key);
|
| 320 |
+
if (typeof value === 'string' && value.indexOf('&') === -1) {
|
| 321 |
+
if (this._applyRegistrationHook(this._onExternalEntity, key, value, 'external')) {
|
| 322 |
+
this._externalMap[key] = value;
|
| 323 |
+
}
|
| 324 |
+
}
|
| 325 |
+
}
|
| 326 |
+
|
| 327 |
+
// -------------------------------------------------------------------------
|
| 328 |
+
// Input / runtime entity registration (per document)
|
| 329 |
+
// -------------------------------------------------------------------------
|
| 330 |
+
|
| 331 |
+
/**
|
| 332 |
+
* Inject DOCTYPE entities for the current document.
|
| 333 |
+
* Also resets per-document expansion counters.
|
| 334 |
+
* If `onInputEntity` is set it is called once per entry; entries returning
|
| 335 |
+
* `ENTITY_ACTION.BLOCK` are silently omitted, `ENTITY_ACTION.THROW` aborts.
|
| 336 |
+
* @param {Record<string, string | { regx?: RegExp, regex?: RegExp, val: string }>} map
|
| 337 |
+
*/
|
| 338 |
+
addInputEntities(map) {
|
| 339 |
+
this._totalExpansions = 0;
|
| 340 |
+
this._expandedLength = 0;
|
| 341 |
+
if (!this._onInputEntity) {
|
| 342 |
+
this._inputMap = mergeEntityMaps(map);
|
| 343 |
+
return;
|
| 344 |
+
}
|
| 345 |
+
const flat = mergeEntityMaps(map);
|
| 346 |
+
const filtered = Object.create(null);
|
| 347 |
+
for (const [name, value] of Object.entries(flat)) {
|
| 348 |
+
if (this._applyRegistrationHook(this._onInputEntity, name, value, 'input')) {
|
| 349 |
+
filtered[name] = value;
|
| 350 |
+
}
|
| 351 |
+
}
|
| 352 |
+
this._inputMap = filtered;
|
| 353 |
+
}
|
| 354 |
+
|
| 355 |
+
// -------------------------------------------------------------------------
|
| 356 |
+
// Per-document reset
|
| 357 |
+
// -------------------------------------------------------------------------
|
| 358 |
+
|
| 359 |
+
/**
|
| 360 |
+
* Wipe input/runtime entities and reset counters.
|
| 361 |
+
* Call this before processing each new document.
|
| 362 |
+
* @returns {this}
|
| 363 |
+
*/
|
| 364 |
+
reset() {
|
| 365 |
+
this._inputMap = Object.create(null);
|
| 366 |
+
this._totalExpansions = 0;
|
| 367 |
+
this._expandedLength = 0;
|
| 368 |
+
return this;
|
| 369 |
+
}
|
| 370 |
+
|
| 371 |
+
// -------------------------------------------------------------------------
|
| 372 |
+
// XML version (can be set after construction, e.g. once parser reads <?xml?>)
|
| 373 |
+
// -------------------------------------------------------------------------
|
| 374 |
+
|
| 375 |
+
/**
|
| 376 |
+
* Update the XML version used for NCR classification.
|
| 377 |
+
* Call this as soon as the document's `<?xml version="...">` declaration is parsed.
|
| 378 |
+
* @param {1.0|1.1|number} version
|
| 379 |
+
*/
|
| 380 |
+
setXmlVersion(version) {
|
| 381 |
+
this._ncrXmlVersion = version === 1.1 ? 1.1 : 1.0;
|
| 382 |
+
}
|
| 383 |
+
|
| 384 |
+
// -------------------------------------------------------------------------
|
| 385 |
+
// Primary API
|
| 386 |
+
// -------------------------------------------------------------------------
|
| 387 |
+
|
| 388 |
+
/**
|
| 389 |
+
* Replace all entity references in `str` in a single pass.
|
| 390 |
+
*
|
| 391 |
+
* @param {string} str
|
| 392 |
+
* @returns {string}
|
| 393 |
+
*/
|
| 394 |
+
decode(str) {
|
| 395 |
+
if (typeof str !== 'string' || str.length === 0) return str;
|
| 396 |
+
//TODO: check if needed
|
| 397 |
+
if (str.indexOf('&') === -1) return str; // fast path — no entities at all
|
| 398 |
+
|
| 399 |
+
const original = str;
|
| 400 |
+
const chunks = [];
|
| 401 |
+
const len = str.length;
|
| 402 |
+
let last = 0; // start of next unprocessed literal chunk
|
| 403 |
+
let i = 0;
|
| 404 |
+
|
| 405 |
+
const limitExpansions = this._maxTotalExpansions > 0;
|
| 406 |
+
const limitLength = this._maxExpandedLength > 0;
|
| 407 |
+
const checkLimits = limitExpansions || limitLength;
|
| 408 |
+
|
| 409 |
+
while (i < len) {
|
| 410 |
+
// Scan forward to next '&'
|
| 411 |
+
if (str.charCodeAt(i) !== 38 /* '&' */) { i++; continue; }
|
| 412 |
+
|
| 413 |
+
// --- Found '&' at position i ---
|
| 414 |
+
|
| 415 |
+
// Scan forward to ';'
|
| 416 |
+
let j = i + 1;
|
| 417 |
+
while (j < len && str.charCodeAt(j) !== 59 /* ';' */ && (j - i) <= 32) j++;
|
| 418 |
+
|
| 419 |
+
if (j >= len || str.charCodeAt(j) !== 59) {
|
| 420 |
+
// No closing ';' within window — treat '&' as literal
|
| 421 |
+
i++;
|
| 422 |
+
continue;
|
| 423 |
+
}
|
| 424 |
+
|
| 425 |
+
// Raw token between '&' and ';' (exclusive)
|
| 426 |
+
const token = str.slice(i + 1, j);
|
| 427 |
+
if (token.length === 0) { i++; continue; }
|
| 428 |
+
|
| 429 |
+
let replacement;
|
| 430 |
+
let tier; // which limit tier this entity belongs to
|
| 431 |
+
|
| 432 |
+
if (this._removeSet.has(token)) {
|
| 433 |
+
// Remove entity: replace with empty string
|
| 434 |
+
replacement = '';
|
| 435 |
+
// If entity was unknown (replacement undefined), we still need a tier for limits.
|
| 436 |
+
// Treat as external tier because it's user-directed removal of an unknown reference.
|
| 437 |
+
if (tier === undefined) {
|
| 438 |
+
tier = LIMIT_TIER_EXTERNAL;
|
| 439 |
+
}
|
| 440 |
+
} else if (this._leaveSet.has(token)) {
|
| 441 |
+
// Do not replace — keep original &token; as literal
|
| 442 |
+
i++;
|
| 443 |
+
continue;
|
| 444 |
+
} else if (token.charCodeAt(0) === 35 /* '#' */) {
|
| 445 |
+
// ---- Numeric / NCR reference ----
|
| 446 |
+
// NCR classification always runs first — prohibited codepoints must be
|
| 447 |
+
// caught regardless of numericAllowed.
|
| 448 |
+
const ncrResult = this._resolveNCR(token);
|
| 449 |
+
if (ncrResult === undefined) {
|
| 450 |
+
// 'leave' action — keep original &token; as-is
|
| 451 |
+
i++;
|
| 452 |
+
continue;
|
| 453 |
+
}
|
| 454 |
+
replacement = ncrResult; // '' for remove, char string for allow
|
| 455 |
+
tier = LIMIT_TIER_BASE;
|
| 456 |
+
} else {
|
| 457 |
+
// ---- Named reference ----
|
| 458 |
+
const resolved = this._resolveName(token);
|
| 459 |
+
replacement = resolved?.value;
|
| 460 |
+
tier = resolved?.tier;
|
| 461 |
+
}
|
| 462 |
+
|
| 463 |
+
if (replacement === undefined) {
|
| 464 |
+
// Unknown entity — leave as-is, advance past '&' only
|
| 465 |
+
i++;
|
| 466 |
+
continue;
|
| 467 |
+
}
|
| 468 |
+
|
| 469 |
+
// Flush literal chunk before this entity
|
| 470 |
+
if (i > last) chunks.push(str.slice(last, i));
|
| 471 |
+
chunks.push(replacement);
|
| 472 |
+
last = j + 1; // skip past ';'
|
| 473 |
+
i = last;
|
| 474 |
+
|
| 475 |
+
// Apply expansion limits only if this tier is being tracked
|
| 476 |
+
if (checkLimits && this._tierCounts(tier)) {
|
| 477 |
+
if (limitExpansions) {
|
| 478 |
+
this._totalExpansions++;
|
| 479 |
+
if (this._totalExpansions > this._maxTotalExpansions) {
|
| 480 |
+
throw new Error(
|
| 481 |
+
`[EntityReplacer] Entity expansion count limit exceeded: ` +
|
| 482 |
+
`${this._totalExpansions} > ${this._maxTotalExpansions}`
|
| 483 |
+
);
|
| 484 |
+
}
|
| 485 |
+
}
|
| 486 |
+
if (limitLength) {
|
| 487 |
+
// delta: replacement.length minus the raw &token; length (token.length + 2 for '&' and ';')
|
| 488 |
+
const delta = replacement.length - (token.length + 2);
|
| 489 |
+
if (delta > 0) {
|
| 490 |
+
this._expandedLength += delta;
|
| 491 |
+
if (this._expandedLength > this._maxExpandedLength) {
|
| 492 |
+
throw new Error(
|
| 493 |
+
`[EntityReplacer] Expanded content length limit exceeded: ` +
|
| 494 |
+
`${this._expandedLength} > ${this._maxExpandedLength}`
|
| 495 |
+
);
|
| 496 |
+
}
|
| 497 |
+
}
|
| 498 |
+
}
|
| 499 |
+
}
|
| 500 |
+
}
|
| 501 |
+
|
| 502 |
+
// Flush trailing literal
|
| 503 |
+
if (last < len) chunks.push(str.slice(last));
|
| 504 |
+
|
| 505 |
+
// If nothing was replaced, chunks is empty — return original
|
| 506 |
+
const result = chunks.length === 0 ? str : chunks.join('');
|
| 507 |
+
|
| 508 |
+
return this._postCheck(result, original);
|
| 509 |
+
}
|
| 510 |
+
|
| 511 |
+
// -------------------------------------------------------------------------
|
| 512 |
+
// Private: limit tier check
|
| 513 |
+
// -------------------------------------------------------------------------
|
| 514 |
+
|
| 515 |
+
/**
|
| 516 |
+
* Returns true if a resolved entity of the given tier should count
|
| 517 |
+
* against the expansion/length limits.
|
| 518 |
+
* @param {string} tier — LIMIT_TIER_EXTERNAL | LIMIT_TIER_BASE
|
| 519 |
+
* @returns {boolean}
|
| 520 |
+
*/
|
| 521 |
+
_tierCounts(tier) {
|
| 522 |
+
if (this._limitTiers.has(LIMIT_TIER_ALL)) return true;
|
| 523 |
+
return this._limitTiers.has(tier);
|
| 524 |
+
}
|
| 525 |
+
|
| 526 |
+
// -------------------------------------------------------------------------
|
| 527 |
+
// Private: entity resolution
|
| 528 |
+
// -------------------------------------------------------------------------
|
| 529 |
+
|
| 530 |
+
/**
|
| 531 |
+
* Resolve a named entity token (without & and ;).
|
| 532 |
+
* Priority: inputMap > externalMap > baseMap
|
| 533 |
+
* Returns the resolved value tagged with its limit tier.
|
| 534 |
+
*
|
| 535 |
+
* @param {string} name
|
| 536 |
+
* @returns {{ value: string, tier: string }|undefined}
|
| 537 |
+
*/
|
| 538 |
+
_resolveName(name) {
|
| 539 |
+
// input and external both count as 'external' tier for limit purposes —
|
| 540 |
+
// they are injected at runtime and are the untrusted surface.
|
| 541 |
+
if (name in this._inputMap) return { value: this._inputMap[name], tier: LIMIT_TIER_EXTERNAL };
|
| 542 |
+
if (name in this._externalMap) return { value: this._externalMap[name], tier: LIMIT_TIER_EXTERNAL };
|
| 543 |
+
if (name in this._baseMap) return { value: this._baseMap[name], tier: LIMIT_TIER_BASE };
|
| 544 |
+
return undefined;
|
| 545 |
+
}
|
| 546 |
+
|
| 547 |
+
/**
|
| 548 |
+
* Classify a codepoint and return the minimum action level that must be applied.
|
| 549 |
+
* Returns -1 when no minimum is imposed (normal allow path).
|
| 550 |
+
*
|
| 551 |
+
* Ranges checked (in priority order):
|
| 552 |
+
* 1. U+0000 — null, governed by nullNCR (always ≥ remove)
|
| 553 |
+
* 2. U+D800–U+DFFF — surrogates, always prohibited (min: remove)
|
| 554 |
+
* 3. U+0001–U+001F \ {0x09,0x0A,0x0D} — XML 1.0 restricted C0 (min: remove)
|
| 555 |
+
* (skipped in XML 1.1 — C0 controls are allowed when written as NCRs)
|
| 556 |
+
*
|
| 557 |
+
* @param {number} cp — codepoint
|
| 558 |
+
* @returns {number} — minimum NCR_LEVEL value, or -1 for no restriction
|
| 559 |
+
*/
|
| 560 |
+
_classifyNCR(cp) {
|
| 561 |
+
// 1. Null
|
| 562 |
+
if (cp === 0) return this._ncrNullLevel;
|
| 563 |
+
|
| 564 |
+
// 2. Surrogates — always prohibited, minimum 'remove'
|
| 565 |
+
if (cp >= 0xD800 && cp <= 0xDFFF) return NCR_LEVEL.remove;
|
| 566 |
+
|
| 567 |
+
// 3. XML 1.0 restricted C0 controls
|
| 568 |
+
if (this._ncrXmlVersion === 1.0) {
|
| 569 |
+
if (cp >= 0x01 && cp <= 0x1F && !XML10_ALLOWED_C0.has(cp)) return NCR_LEVEL.remove;
|
| 570 |
+
}
|
| 571 |
+
|
| 572 |
+
return -1; // no restriction
|
| 573 |
+
}
|
| 574 |
+
|
| 575 |
+
/**
|
| 576 |
+
* Execute a resolved NCR action.
|
| 577 |
+
*
|
| 578 |
+
* @param {number} action — NCR_LEVEL value
|
| 579 |
+
* @param {string} token — raw token (e.g. '#38') for error messages
|
| 580 |
+
* @param {number} cp — codepoint, used only for error messages
|
| 581 |
+
* @returns {string|undefined}
|
| 582 |
+
* - decoded character string → 'allow'
|
| 583 |
+
* - '' → 'remove'
|
| 584 |
+
* - undefined → 'leave' (caller must skip past '&' only)
|
| 585 |
+
* - throws Error → 'throw'
|
| 586 |
+
*/
|
| 587 |
+
_applyNCRAction(action, token, cp) {
|
| 588 |
+
switch (action) {
|
| 589 |
+
case NCR_LEVEL.allow: return String.fromCodePoint(cp);
|
| 590 |
+
case NCR_LEVEL.remove: return '';
|
| 591 |
+
case NCR_LEVEL.leave: return undefined; // signal: keep literal
|
| 592 |
+
case NCR_LEVEL.throw:
|
| 593 |
+
throw new Error(
|
| 594 |
+
`[EntityDecoder] Prohibited numeric character reference ` +
|
| 595 |
+
`&${token}; (U+${cp.toString(16).toUpperCase().padStart(4, '0')})`
|
| 596 |
+
);
|
| 597 |
+
default: return String.fromCodePoint(cp);
|
| 598 |
+
}
|
| 599 |
+
}
|
| 600 |
+
|
| 601 |
+
/**
|
| 602 |
+
* Full NCR resolution pipeline for a numeric token.
|
| 603 |
+
*
|
| 604 |
+
* Steps:
|
| 605 |
+
* 1. Parse the codepoint (decimal or hex).
|
| 606 |
+
* 2. Validate the raw codepoint range (NaN, <0, >0x10FFFF).
|
| 607 |
+
* 3. If numericAllowed is false and no minimum restriction applies → leave as-is.
|
| 608 |
+
* 4. Classify the codepoint to find the minimum required action level.
|
| 609 |
+
* 5. Resolve effective action = max(onNCR, minimum).
|
| 610 |
+
* 6. Apply and return.
|
| 611 |
+
*
|
| 612 |
+
* @param {string} token — e.g. '#38', '#x26', '#X26'
|
| 613 |
+
* @returns {string|undefined}
|
| 614 |
+
* - string (incl. '') — replacement ('' = remove)
|
| 615 |
+
* - undefined — leave original &token; as-is
|
| 616 |
+
*/
|
| 617 |
+
_resolveNCR(token) {
|
| 618 |
+
// Step 1: parse codepoint
|
| 619 |
+
const second = token.charCodeAt(1);
|
| 620 |
+
let cp;
|
| 621 |
+
if (second === 120 /* x */ || second === 88 /* X */) {
|
| 622 |
+
cp = parseInt(token.slice(2), 16);
|
| 623 |
+
} else {
|
| 624 |
+
cp = parseInt(token.slice(1), 10);
|
| 625 |
+
}
|
| 626 |
+
|
| 627 |
+
// Step 2: out-of-range → leave as-is unconditionally
|
| 628 |
+
if (Number.isNaN(cp) || cp < 0 || cp > 0x10FFFF) return undefined;
|
| 629 |
+
|
| 630 |
+
// Step 3: classify to get minimum action level
|
| 631 |
+
const minimum = this._classifyNCR(cp);
|
| 632 |
+
|
| 633 |
+
// Step 4: if numericAllowed is false and no hard minimum → leave
|
| 634 |
+
if (!this._numericAllowed && minimum < NCR_LEVEL.remove) return undefined;
|
| 635 |
+
|
| 636 |
+
// Step 5: effective action = max(configured onNCR, range minimum)
|
| 637 |
+
const effective = minimum === -1
|
| 638 |
+
? this._ncrOnLevel
|
| 639 |
+
: Math.max(this._ncrOnLevel, minimum);
|
| 640 |
+
|
| 641 |
+
//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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// EntityDecoder.js
|
| 2 |
+
import { trie1, trie2, trie3 } from './entityTries.js';
|
| 3 |
+
|
| 4 |
+
// Replacement strings indexed by char code — direct array access, no hashing
|
| 5 |
+
const XML_UNSAFE_REPLACEMENT = new Array(128);
|
| 6 |
+
XML_UNSAFE_REPLACEMENT[38] = '&'; // &
|
| 7 |
+
XML_UNSAFE_REPLACEMENT[60] = '<'; // <
|
| 8 |
+
XML_UNSAFE_REPLACEMENT[62] = '>'; // >
|
| 9 |
+
XML_UNSAFE_REPLACEMENT[34] = '"'; // "
|
| 10 |
+
XML_UNSAFE_REPLACEMENT[39] = '''; // '
|
| 11 |
+
|
| 12 |
+
// Typed bitmask for O(1) "is this ASCII code XML-unsafe?" check
|
| 13 |
+
const IS_XML_UNSAFE = new Uint8Array(128);
|
| 14 |
+
IS_XML_UNSAFE[38] = 1;
|
| 15 |
+
IS_XML_UNSAFE[60] = 1;
|
| 16 |
+
IS_XML_UNSAFE[62] = 1;
|
| 17 |
+
IS_XML_UNSAFE[34] = 1;
|
| 18 |
+
IS_XML_UNSAFE[39] = 1;
|
| 19 |
+
|
| 20 |
+
// Fast pre-scan: bail out immediately if nothing needs encoding
|
| 21 |
+
const NEEDS_PROCESSING = /[&<>"'\u0080-\uFFFF]/;
|
| 22 |
+
|
| 23 |
+
export default class EntityEncoder {
|
| 24 |
+
constructor(options = {}) {
|
| 25 |
+
this.encodeXmlSafe = options.encodeXmlSafe !== false;
|
| 26 |
+
this.encodeAllNamed = options.encodeAllNamed !== false;
|
| 27 |
+
this.maxReplacements = options.maxReplacements || 0;
|
| 28 |
+
this.replacementsCount = 0;
|
| 29 |
+
}
|
| 30 |
+
|
| 31 |
+
encode(str) {
|
| 32 |
+
if (typeof str !== 'string' || str.length === 0) return str;
|
| 33 |
+
if (!NEEDS_PROCESSING.test(str)) return str;
|
| 34 |
+
|
| 35 |
+
const maxRep = this.maxReplacements;
|
| 36 |
+
if (maxRep > 0 && this.replacementsCount >= maxRep) return str;
|
| 37 |
+
|
| 38 |
+
// Hoist to locals — avoids `this` property lookup inside the hot loop
|
| 39 |
+
const encodeXmlSafe = this.encodeXmlSafe;
|
| 40 |
+
const encodeAllNamed = this.encodeAllNamed;
|
| 41 |
+
|
| 42 |
+
const len = str.length;
|
| 43 |
+
|
| 44 |
+
let result = '';
|
| 45 |
+
let last = 0;
|
| 46 |
+
let i = 0;
|
| 47 |
+
let limitReached = false;
|
| 48 |
+
|
| 49 |
+
// ── Main loop: runs to len-2 so trie3 never needs a bounds check ────────
|
| 50 |
+
// The last 2 characters are handled by the tail block below.
|
| 51 |
+
const mainEnd = len - 2; // i <= mainEnd guarantees i+1 and i+2 are valid
|
| 52 |
+
|
| 53 |
+
while (i <= mainEnd && !limitReached) {
|
| 54 |
+
const c0 = str.charCodeAt(i);
|
| 55 |
+
|
| 56 |
+
// ── ASCII branch ───────────────────────────────────────────────────
|
| 57 |
+
if (c0 < 128) {
|
| 58 |
+
if (encodeXmlSafe && IS_XML_UNSAFE[c0] === 1) {
|
| 59 |
+
result += str.substring(last, i) + XML_UNSAFE_REPLACEMENT[c0];
|
| 60 |
+
last = ++i;
|
| 61 |
+
if (maxRep > 0) {
|
| 62 |
+
this.replacementsCount++;
|
| 63 |
+
if (this.replacementsCount >= maxRep) {
|
| 64 |
+
limitReached = true;
|
| 65 |
+
break;
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
} else {
|
| 69 |
+
// Bulk-skip: advance to the next interesting position without
|
| 70 |
+
// touching the outer loop overhead on every safe character
|
| 71 |
+
i++;
|
| 72 |
+
while (i <= mainEnd && !limitReached) {
|
| 73 |
+
const c = str.charCodeAt(i);
|
| 74 |
+
if (c >= 128 || (encodeXmlSafe && IS_XML_UNSAFE[c] === 1)) break;
|
| 75 |
+
i++;
|
| 76 |
+
}
|
| 77 |
+
}
|
| 78 |
+
continue;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
// ── Non-ASCII: integer-keyed trie lookup ───────────────────────────
|
| 82 |
+
// No bounds checks needed for c1/c2 because i <= mainEnd guarantees
|
| 83 |
+
// i+1 and i+2 are both within the string.
|
| 84 |
+
let matchedEntity = null;
|
| 85 |
+
let advance = 1;
|
| 86 |
+
|
| 87 |
+
// Try 3-char match first (longest wins)
|
| 88 |
+
const mid3 = trie3.get(c0);
|
| 89 |
+
if (mid3 !== undefined) {
|
| 90 |
+
const c1 = str.charCodeAt(i + 1);
|
| 91 |
+
const inner3 = mid3.get(c1);
|
| 92 |
+
if (inner3 !== undefined) {
|
| 93 |
+
const c2 = str.charCodeAt(i + 2);
|
| 94 |
+
const candidate = inner3.get(c2);
|
| 95 |
+
if (candidate !== undefined) { matchedEntity = candidate; advance = 3; }
|
| 96 |
+
}
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
// Try 2-char match
|
| 100 |
+
if (matchedEntity === null) {
|
| 101 |
+
const inner2 = trie2.get(c0);
|
| 102 |
+
if (inner2 !== undefined) {
|
| 103 |
+
const c1 = str.charCodeAt(i + 1);
|
| 104 |
+
const candidate = inner2.get(c1);
|
| 105 |
+
if (candidate !== undefined) { matchedEntity = candidate; advance = 2; }
|
| 106 |
+
}
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
// Try 1-char match
|
| 110 |
+
if (matchedEntity === null && encodeAllNamed) {
|
| 111 |
+
const candidate = trie1.get(c0);
|
| 112 |
+
if (candidate !== undefined) { matchedEntity = candidate; }
|
| 113 |
+
}
|
| 114 |
+
|
| 115 |
+
if (matchedEntity !== null) {
|
| 116 |
+
result += str.substring(last, i) + matchedEntity;
|
| 117 |
+
i += advance;
|
| 118 |
+
last = i;
|
| 119 |
+
if (maxRep > 0) {
|
| 120 |
+
this.replacementsCount++;
|
| 121 |
+
if (this.replacementsCount >= maxRep) {
|
| 122 |
+
limitReached = true;
|
| 123 |
+
break;
|
| 124 |
+
}
|
| 125 |
+
}
|
| 126 |
+
} else {
|
| 127 |
+
i++;
|
| 128 |
+
}
|
| 129 |
+
}
|
| 130 |
+
|
| 131 |
+
// ── Tail: handle the last 1-2 characters (no 3-char match possible) ────
|
| 132 |
+
while (i < len && !limitReached) {
|
| 133 |
+
const c0 = str.charCodeAt(i);
|
| 134 |
+
|
| 135 |
+
if (c0 < 128) {
|
| 136 |
+
if (encodeXmlSafe && IS_XML_UNSAFE[c0] === 1) {
|
| 137 |
+
result += str.substring(last, i) + XML_UNSAFE_REPLACEMENT[c0];
|
| 138 |
+
last = ++i;
|
| 139 |
+
if (maxRep > 0) {
|
| 140 |
+
this.replacementsCount++;
|
| 141 |
+
if (this.replacementsCount >= maxRep) {
|
| 142 |
+
limitReached = true;
|
| 143 |
+
break;
|
| 144 |
+
}
|
| 145 |
+
}
|
| 146 |
+
} else {
|
| 147 |
+
i++;
|
| 148 |
+
}
|
| 149 |
+
continue;
|
| 150 |
+
}
|
| 151 |
+
|
| 152 |
+
// Non-ASCII tail — only 2-char and 1-char matches are possible here
|
| 153 |
+
let matchedEntity = null;
|
| 154 |
+
let advance = 1;
|
| 155 |
+
|
| 156 |
+
if (i + 1 < len) {
|
| 157 |
+
const inner2 = trie2.get(c0);
|
| 158 |
+
if (inner2 !== undefined) {
|
| 159 |
+
const c1 = str.charCodeAt(i + 1);
|
| 160 |
+
const candidate = inner2.get(c1);
|
| 161 |
+
if (candidate !== undefined) { matchedEntity = candidate; advance = 2; }
|
| 162 |
+
}
|
| 163 |
+
}
|
| 164 |
+
|
| 165 |
+
if (matchedEntity === null && encodeAllNamed) {
|
| 166 |
+
const candidate = trie1.get(c0);
|
| 167 |
+
if (candidate !== undefined) { matchedEntity = candidate; }
|
| 168 |
+
}
|
| 169 |
+
|
| 170 |
+
if (matchedEntity !== null) {
|
| 171 |
+
result += str.substring(last, i) + matchedEntity;
|
| 172 |
+
i += advance;
|
| 173 |
+
last = i;
|
| 174 |
+
if (maxRep > 0) {
|
| 175 |
+
this.replacementsCount++;
|
| 176 |
+
if (this.replacementsCount >= maxRep) {
|
| 177 |
+
limitReached = true;
|
| 178 |
+
break;
|
| 179 |
+
}
|
| 180 |
+
}
|
| 181 |
+
} else {
|
| 182 |
+
i++;
|
| 183 |
+
}
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
// ── Flush any remaining literal suffix ────────────────────────────────
|
| 187 |
+
if (last < len) result += str.substring(last);
|
| 188 |
+
return result;
|
| 189 |
+
}
|
| 190 |
+
|
| 191 |
+
reset() {
|
| 192 |
+
this.replacementsCount = 0;
|
| 193 |
+
}
|
| 194 |
+
}
|
node_modules/@nodable/entities/src/entities.js
ADDED
|
@@ -0,0 +1,1159 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// ---------------------------------------------------------------------------
|
| 2 |
+
// Complete HTML5 named entity reference
|
| 3 |
+
// Organized by logical categories for easy maintenance and selective importing
|
| 4 |
+
// ---------------------------------------------------------------------------
|
| 5 |
+
|
| 6 |
+
/**
|
| 7 |
+
* Basic Latin & Special Characters
|
| 8 |
+
* @type {Record<string, string>}
|
| 9 |
+
*/
|
| 10 |
+
export const BASIC_LATIN = {
|
| 11 |
+
amp: '&',
|
| 12 |
+
AMP: '&',
|
| 13 |
+
lt: '<',
|
| 14 |
+
LT: '<',
|
| 15 |
+
gt: '>',
|
| 16 |
+
GT: '>',
|
| 17 |
+
quot: '"',
|
| 18 |
+
QUOT: '"',
|
| 19 |
+
apos: "'",
|
| 20 |
+
lsquo: '‘',
|
| 21 |
+
rsquo: '’',
|
| 22 |
+
ldquo: '“',
|
| 23 |
+
rdquo: '”',
|
| 24 |
+
lsquor: '‚',
|
| 25 |
+
rsquor: '’',
|
| 26 |
+
ldquor: '„',
|
| 27 |
+
bdquo: '„',
|
| 28 |
+
comma: ',',
|
| 29 |
+
period: '.',
|
| 30 |
+
colon: ':',
|
| 31 |
+
semi: ';',
|
| 32 |
+
excl: '!',
|
| 33 |
+
quest: '?',
|
| 34 |
+
num: '#',
|
| 35 |
+
dollar: '$',
|
| 36 |
+
percent: '%',
|
| 37 |
+
ast: '*',
|
| 38 |
+
commat: '@',
|
| 39 |
+
lowbar: '_',
|
| 40 |
+
verbar: '|',
|
| 41 |
+
vert: '|',
|
| 42 |
+
sol: '/',
|
| 43 |
+
bsol: '\\',
|
| 44 |
+
lbrace: '{',
|
| 45 |
+
rbrace: '}',
|
| 46 |
+
lbrack: '[',
|
| 47 |
+
rbrack: ']',
|
| 48 |
+
lpar: '(',
|
| 49 |
+
rpar: ')',
|
| 50 |
+
nbsp: '\u00a0',
|
| 51 |
+
iexcl: '¡',
|
| 52 |
+
cent: '¢',
|
| 53 |
+
pound: '£',
|
| 54 |
+
curren: '¤',
|
| 55 |
+
yen: '¥',
|
| 56 |
+
brvbar: '¦',
|
| 57 |
+
sect: '§',
|
| 58 |
+
uml: '¨',
|
| 59 |
+
copy: '©',
|
| 60 |
+
COPY: '©',
|
| 61 |
+
ordf: 'ª',
|
| 62 |
+
laquo: '«',
|
| 63 |
+
not: '¬',
|
| 64 |
+
shy: '\u00ad',
|
| 65 |
+
reg: '®',
|
| 66 |
+
REG: '®',
|
| 67 |
+
macr: '¯',
|
| 68 |
+
deg: '°',
|
| 69 |
+
plusmn: '±',
|
| 70 |
+
sup2: '²',
|
| 71 |
+
sup3: '³',
|
| 72 |
+
acute: '´',
|
| 73 |
+
micro: 'µ',
|
| 74 |
+
para: '¶',
|
| 75 |
+
middot: '·',
|
| 76 |
+
cedil: '¸',
|
| 77 |
+
sup1: '¹',
|
| 78 |
+
ordm: 'º',
|
| 79 |
+
raquo: '»',
|
| 80 |
+
frac14: '¼',
|
| 81 |
+
frac12: '½',
|
| 82 |
+
half: '½',
|
| 83 |
+
frac34: '¾',
|
| 84 |
+
iquest: '¿',
|
| 85 |
+
times: '×',
|
| 86 |
+
div: '÷',
|
| 87 |
+
divide: '÷',
|
| 88 |
+
};
|
| 89 |
+
|
| 90 |
+
/**
|
| 91 |
+
* Latin Extended & Accented Letters (A-Z)
|
| 92 |
+
* @type {Record<string, string>}
|
| 93 |
+
*/
|
| 94 |
+
export const LATIN_ACCENTS = {
|
| 95 |
+
Agrave: 'À',
|
| 96 |
+
agrave: 'à',
|
| 97 |
+
Aacute: 'Á',
|
| 98 |
+
aacute: 'á',
|
| 99 |
+
Acirc: 'Â',
|
| 100 |
+
acirc: 'â',
|
| 101 |
+
Atilde: 'Ã',
|
| 102 |
+
atilde: 'ã',
|
| 103 |
+
Auml: 'Ä',
|
| 104 |
+
auml: 'ä',
|
| 105 |
+
Aring: 'Å',
|
| 106 |
+
aring: 'å',
|
| 107 |
+
AElig: 'Æ',
|
| 108 |
+
aelig: 'æ',
|
| 109 |
+
Ccedil: 'Ç',
|
| 110 |
+
ccedil: 'ç',
|
| 111 |
+
Egrave: 'È',
|
| 112 |
+
egrave: 'è',
|
| 113 |
+
Eacute: 'É',
|
| 114 |
+
eacute: 'é',
|
| 115 |
+
Ecirc: 'Ê',
|
| 116 |
+
ecirc: 'ê',
|
| 117 |
+
Euml: 'Ë',
|
| 118 |
+
euml: 'ë',
|
| 119 |
+
Igrave: 'Ì',
|
| 120 |
+
igrave: 'ì',
|
| 121 |
+
Iacute: 'Í',
|
| 122 |
+
iacute: 'í',
|
| 123 |
+
Icirc: 'Î',
|
| 124 |
+
icirc: 'î',
|
| 125 |
+
Iuml: 'Ï',
|
| 126 |
+
iuml: 'ï',
|
| 127 |
+
ETH: 'Ð',
|
| 128 |
+
eth: 'ð',
|
| 129 |
+
Ntilde: 'Ñ',
|
| 130 |
+
ntilde: 'ñ',
|
| 131 |
+
Ograve: 'Ò',
|
| 132 |
+
ograve: 'ò',
|
| 133 |
+
Oacute: 'Ó',
|
| 134 |
+
oacute: 'ó',
|
| 135 |
+
Ocirc: 'Ô',
|
| 136 |
+
ocirc: 'ô',
|
| 137 |
+
Otilde: 'Õ',
|
| 138 |
+
otilde: 'õ',
|
| 139 |
+
Ouml: 'Ö',
|
| 140 |
+
ouml: 'ö',
|
| 141 |
+
Oslash: 'Ø',
|
| 142 |
+
oslash: 'ø',
|
| 143 |
+
Ugrave: 'Ù',
|
| 144 |
+
ugrave: 'ù',
|
| 145 |
+
Uacute: 'Ú',
|
| 146 |
+
uacute: 'ú',
|
| 147 |
+
Ucirc: 'Û',
|
| 148 |
+
ucirc: 'û',
|
| 149 |
+
Uuml: 'Ü',
|
| 150 |
+
uuml: 'ü',
|
| 151 |
+
Yacute: 'Ý',
|
| 152 |
+
yacute: 'ý',
|
| 153 |
+
THORN: 'Þ',
|
| 154 |
+
thorn: 'þ',
|
| 155 |
+
szlig: 'ß',
|
| 156 |
+
yuml: 'ÿ',
|
| 157 |
+
Yuml: 'Ÿ',
|
| 158 |
+
};
|
| 159 |
+
|
| 160 |
+
/**
|
| 161 |
+
* Latin Extended (Letters with diacritics)
|
| 162 |
+
* @type {Record<string, string>}
|
| 163 |
+
*/
|
| 164 |
+
export const LATIN_EXTENDED = {
|
| 165 |
+
Amacr: 'Ā',
|
| 166 |
+
amacr: 'ā',
|
| 167 |
+
Abreve: 'Ă',
|
| 168 |
+
abreve: 'ă',
|
| 169 |
+
Aogon: 'Ą',
|
| 170 |
+
aogon: 'ą',
|
| 171 |
+
Cacute: 'Ć',
|
| 172 |
+
cacute: 'ć',
|
| 173 |
+
Ccirc: 'Ĉ',
|
| 174 |
+
ccirc: 'ĉ',
|
| 175 |
+
Cdot: 'Ċ',
|
| 176 |
+
cdot: 'ċ',
|
| 177 |
+
Ccaron: 'Č',
|
| 178 |
+
ccaron: 'č',
|
| 179 |
+
Dcaron: 'Ď',
|
| 180 |
+
dcaron: 'ď',
|
| 181 |
+
Dstrok: 'Đ',
|
| 182 |
+
dstrok: 'đ',
|
| 183 |
+
Emacr: 'Ē',
|
| 184 |
+
emacr: 'ē',
|
| 185 |
+
Ecaron: 'Ě',
|
| 186 |
+
ecaron: 'ě',
|
| 187 |
+
Edot: 'Ė',
|
| 188 |
+
edot: 'ė',
|
| 189 |
+
Eogon: 'Ę',
|
| 190 |
+
eogon: 'ę',
|
| 191 |
+
Gcirc: 'Ĝ',
|
| 192 |
+
gcirc: 'ĝ',
|
| 193 |
+
Gbreve: 'Ğ',
|
| 194 |
+
gbreve: 'ğ',
|
| 195 |
+
Gdot: 'Ġ',
|
| 196 |
+
gdot: 'ġ',
|
| 197 |
+
Gcedil: 'Ģ',
|
| 198 |
+
Hcirc: 'Ĥ',
|
| 199 |
+
hcirc: 'ĥ',
|
| 200 |
+
Hstrok: 'Ħ',
|
| 201 |
+
hstrok: 'ħ',
|
| 202 |
+
Itilde: 'Ĩ',
|
| 203 |
+
itilde: 'ĩ',
|
| 204 |
+
Imacr: 'Ī',
|
| 205 |
+
imacr: 'ī',
|
| 206 |
+
Iogon: 'Į',
|
| 207 |
+
iogon: 'į',
|
| 208 |
+
Idot: 'İ',
|
| 209 |
+
IJlig: 'IJ',
|
| 210 |
+
ijlig: 'ij',
|
| 211 |
+
Jcirc: 'Ĵ',
|
| 212 |
+
jcirc: 'ĵ',
|
| 213 |
+
Kcedil: 'Ķ',
|
| 214 |
+
kcedil: 'ķ',
|
| 215 |
+
kgreen: 'ĸ',
|
| 216 |
+
Lacute: 'Ĺ',
|
| 217 |
+
lacute: 'ĺ',
|
| 218 |
+
Lcedil: 'Ļ',
|
| 219 |
+
lcedil: 'ļ',
|
| 220 |
+
Lcaron: 'Ľ',
|
| 221 |
+
lcaron: 'ľ',
|
| 222 |
+
Lmidot: 'Ŀ',
|
| 223 |
+
lmidot: 'ŀ',
|
| 224 |
+
Lstrok: 'Ł',
|
| 225 |
+
lstrok: 'ł',
|
| 226 |
+
Nacute: 'Ń',
|
| 227 |
+
nacute: 'ń',
|
| 228 |
+
Ncaron: 'Ň',
|
| 229 |
+
ncaron: 'ň',
|
| 230 |
+
Ncedil: 'Ņ',
|
| 231 |
+
ncedil: 'ņ',
|
| 232 |
+
ENG: 'Ŋ',
|
| 233 |
+
eng: 'ŋ',
|
| 234 |
+
Omacr: 'Ō',
|
| 235 |
+
omacr: 'ō',
|
| 236 |
+
Odblac: 'Ő',
|
| 237 |
+
odblac: 'ő',
|
| 238 |
+
OElig: 'Œ',
|
| 239 |
+
oelig: 'œ',
|
| 240 |
+
Racute: 'Ŕ',
|
| 241 |
+
racute: 'ŕ',
|
| 242 |
+
Rcaron: 'Ř',
|
| 243 |
+
rcaron: 'ř',
|
| 244 |
+
Rcedil: 'Ŗ',
|
| 245 |
+
rcedil: 'ŗ',
|
| 246 |
+
Sacute: 'Ś',
|
| 247 |
+
sacute: 'ś',
|
| 248 |
+
Scirc: 'Ŝ',
|
| 249 |
+
scirc: 'ŝ',
|
| 250 |
+
Scedil: 'Ş',
|
| 251 |
+
scedil: 'ş',
|
| 252 |
+
Scaron: 'Š',
|
| 253 |
+
scaron: 'š',
|
| 254 |
+
Tcedil: 'Ţ',
|
| 255 |
+
tcedil: 'ţ',
|
| 256 |
+
Tcaron: 'Ť',
|
| 257 |
+
tcaron: 'ť',
|
| 258 |
+
Tstrok: 'Ŧ',
|
| 259 |
+
tstrok: 'ŧ',
|
| 260 |
+
Utilde: 'Ũ',
|
| 261 |
+
utilde: 'ũ',
|
| 262 |
+
Umacr: 'Ū',
|
| 263 |
+
umacr: 'ū',
|
| 264 |
+
Ubreve: 'Ŭ',
|
| 265 |
+
ubreve: 'ŭ',
|
| 266 |
+
Uring: 'Ů',
|
| 267 |
+
uring: 'ů',
|
| 268 |
+
Udblac: 'Ű',
|
| 269 |
+
udblac: 'ű',
|
| 270 |
+
Uogon: 'Ų',
|
| 271 |
+
uogon: 'ų',
|
| 272 |
+
Wcirc: 'Ŵ',
|
| 273 |
+
wcirc: 'ŵ',
|
| 274 |
+
Ycirc: 'Ŷ',
|
| 275 |
+
ycirc: 'ŷ',
|
| 276 |
+
Zacute: 'Ź',
|
| 277 |
+
zacute: 'ź',
|
| 278 |
+
Zdot: 'Ż',
|
| 279 |
+
zdot: 'ż',
|
| 280 |
+
Zcaron: 'Ž',
|
| 281 |
+
zcaron: 'ž',
|
| 282 |
+
};
|
| 283 |
+
|
| 284 |
+
/**
|
| 285 |
+
* Greek Letters
|
| 286 |
+
* @type {Record<string, string>}
|
| 287 |
+
*/
|
| 288 |
+
export const GREEK = {
|
| 289 |
+
Alpha: 'Α',
|
| 290 |
+
alpha: 'α',
|
| 291 |
+
Beta: 'Β',
|
| 292 |
+
beta: 'β',
|
| 293 |
+
Gamma: 'Γ',
|
| 294 |
+
gamma: 'γ',
|
| 295 |
+
Delta: 'Δ',
|
| 296 |
+
delta: 'δ',
|
| 297 |
+
Epsilon: 'Ε',
|
| 298 |
+
epsilon: 'ε',
|
| 299 |
+
epsiv: 'ϵ',
|
| 300 |
+
varepsilon: 'ϵ',
|
| 301 |
+
Zeta: 'Ζ',
|
| 302 |
+
zeta: 'ζ',
|
| 303 |
+
Eta: 'Η',
|
| 304 |
+
eta: 'η',
|
| 305 |
+
Theta: 'Θ',
|
| 306 |
+
theta: 'θ',
|
| 307 |
+
thetasym: 'ϑ',
|
| 308 |
+
vartheta: 'ϑ',
|
| 309 |
+
Iota: 'Ι',
|
| 310 |
+
iota: 'ι',
|
| 311 |
+
Kappa: 'Κ',
|
| 312 |
+
kappa: 'κ',
|
| 313 |
+
kappav: 'ϰ',
|
| 314 |
+
varkappa: 'ϰ',
|
| 315 |
+
Lambda: 'Λ',
|
| 316 |
+
lambda: 'λ',
|
| 317 |
+
Mu: 'Μ',
|
| 318 |
+
mu: 'μ',
|
| 319 |
+
Nu: 'Ν',
|
| 320 |
+
nu: 'ν',
|
| 321 |
+
Xi: 'Ξ',
|
| 322 |
+
xi: 'ξ',
|
| 323 |
+
Omicron: 'Ο',
|
| 324 |
+
omicron: 'ο',
|
| 325 |
+
Pi: 'Π',
|
| 326 |
+
pi: 'π',
|
| 327 |
+
piv: 'ϖ',
|
| 328 |
+
varpi: 'ϖ',
|
| 329 |
+
Rho: 'Ρ',
|
| 330 |
+
rho: 'ρ',
|
| 331 |
+
rhov: 'ϱ',
|
| 332 |
+
varrho: 'ϱ',
|
| 333 |
+
Sigma: 'Σ',
|
| 334 |
+
sigma: 'σ',
|
| 335 |
+
sigmaf: 'ς',
|
| 336 |
+
sigmav: 'ς',
|
| 337 |
+
varsigma: 'ς',
|
| 338 |
+
Tau: 'Τ',
|
| 339 |
+
tau: 'τ',
|
| 340 |
+
Upsilon: 'Υ',
|
| 341 |
+
upsilon: 'υ',
|
| 342 |
+
upsi: 'υ',
|
| 343 |
+
Upsi: 'ϒ',
|
| 344 |
+
upsih: 'ϒ',
|
| 345 |
+
Phi: 'Φ',
|
| 346 |
+
phi: 'φ',
|
| 347 |
+
phiv: 'ϕ',
|
| 348 |
+
varphi: 'ϕ',
|
| 349 |
+
Chi: 'Χ',
|
| 350 |
+
chi: 'χ',
|
| 351 |
+
Psi: 'Ψ',
|
| 352 |
+
psi: 'ψ',
|
| 353 |
+
Omega: 'Ω',
|
| 354 |
+
omega: 'ω',
|
| 355 |
+
ohm: 'Ω',
|
| 356 |
+
Gammad: 'Ϝ',
|
| 357 |
+
gammad: 'ϝ',
|
| 358 |
+
digamma: 'ϝ',
|
| 359 |
+
};
|
| 360 |
+
|
| 361 |
+
/**
|
| 362 |
+
* Cyrillic Letters
|
| 363 |
+
* @type {Record<string, string>}
|
| 364 |
+
*/
|
| 365 |
+
export const CYRILLIC = {
|
| 366 |
+
Afr: '𝔄',
|
| 367 |
+
afr: '𝔞',
|
| 368 |
+
Acy: 'А',
|
| 369 |
+
acy: 'а',
|
| 370 |
+
Bcy: 'Б',
|
| 371 |
+
bcy: 'б',
|
| 372 |
+
Vcy: 'В',
|
| 373 |
+
vcy: 'в',
|
| 374 |
+
Gcy: 'Г',
|
| 375 |
+
gcy: 'г',
|
| 376 |
+
Dcy: 'Д',
|
| 377 |
+
dcy: 'д',
|
| 378 |
+
IEcy: 'Е',
|
| 379 |
+
iecy: 'е',
|
| 380 |
+
IOcy: 'Ё',
|
| 381 |
+
iocy: 'ё',
|
| 382 |
+
ZHcy: 'Ж',
|
| 383 |
+
zhcy: 'ж',
|
| 384 |
+
Zcy: 'З',
|
| 385 |
+
zcy: 'з',
|
| 386 |
+
Icy: 'И',
|
| 387 |
+
icy: 'и',
|
| 388 |
+
Jcy: 'Й',
|
| 389 |
+
jcy: 'й',
|
| 390 |
+
Kcy: 'К',
|
| 391 |
+
kcy: 'к',
|
| 392 |
+
Lcy: 'Л',
|
| 393 |
+
lcy: 'л',
|
| 394 |
+
Mcy: 'М',
|
| 395 |
+
mcy: 'м',
|
| 396 |
+
Ncy: 'Н',
|
| 397 |
+
ncy: 'н',
|
| 398 |
+
Ocy: 'О',
|
| 399 |
+
ocy: 'о',
|
| 400 |
+
Pcy: 'П',
|
| 401 |
+
pcy: 'п',
|
| 402 |
+
Rcy: 'Р',
|
| 403 |
+
rcy: 'р',
|
| 404 |
+
Scy: 'С',
|
| 405 |
+
scy: 'с',
|
| 406 |
+
Tcy: 'Т',
|
| 407 |
+
tcy: 'т',
|
| 408 |
+
Ucy: 'У',
|
| 409 |
+
ucy: 'у',
|
| 410 |
+
Fcy: 'Ф',
|
| 411 |
+
fcy: 'ф',
|
| 412 |
+
KHcy: 'Х',
|
| 413 |
+
khcy: 'х',
|
| 414 |
+
TScy: 'Ц',
|
| 415 |
+
tscy: 'ц',
|
| 416 |
+
CHcy: 'Ч',
|
| 417 |
+
chcy: 'ч',
|
| 418 |
+
SHcy: 'Ш',
|
| 419 |
+
shcy: 'ш',
|
| 420 |
+
SHCHcy: 'Щ',
|
| 421 |
+
shchcy: 'щ',
|
| 422 |
+
HARDcy: 'Ъ',
|
| 423 |
+
hardcy: 'ъ',
|
| 424 |
+
Ycy: 'Ы',
|
| 425 |
+
ycy: 'ы',
|
| 426 |
+
SOFTcy: 'Ь',
|
| 427 |
+
softcy: 'ь',
|
| 428 |
+
Ecy: 'Э',
|
| 429 |
+
ecy: 'э',
|
| 430 |
+
YUcy: 'Ю',
|
| 431 |
+
yucy: 'ю',
|
| 432 |
+
YAcy: 'Я',
|
| 433 |
+
yacy: 'я',
|
| 434 |
+
DJcy: 'Ђ',
|
| 435 |
+
djcy: 'ђ',
|
| 436 |
+
GJcy: 'Ѓ',
|
| 437 |
+
gjcy: 'ѓ',
|
| 438 |
+
Jukcy: 'Є',
|
| 439 |
+
jukcy: 'є',
|
| 440 |
+
DScy: 'Ѕ',
|
| 441 |
+
dscy: 'ѕ',
|
| 442 |
+
Iukcy: 'І',
|
| 443 |
+
iukcy: 'і',
|
| 444 |
+
YIcy: 'Ї',
|
| 445 |
+
yicy: 'ї',
|
| 446 |
+
Jsercy: 'Ј',
|
| 447 |
+
jsercy: 'ј',
|
| 448 |
+
LJcy: 'Љ',
|
| 449 |
+
ljcy: 'љ',
|
| 450 |
+
NJcy: 'Њ',
|
| 451 |
+
njcy: 'њ',
|
| 452 |
+
TSHcy: 'Ћ',
|
| 453 |
+
tshcy: 'ћ',
|
| 454 |
+
KJcy: 'Ќ',
|
| 455 |
+
kjcy: 'ќ',
|
| 456 |
+
Ubrcy: 'Ў',
|
| 457 |
+
ubrcy: 'ў',
|
| 458 |
+
DZcy: 'Џ',
|
| 459 |
+
dzcy: 'џ',
|
| 460 |
+
};
|
| 461 |
+
|
| 462 |
+
/**
|
| 463 |
+
* Mathematical Operators & Relations
|
| 464 |
+
* @type {Record<string, string>}
|
| 465 |
+
*/
|
| 466 |
+
export const MATH = {
|
| 467 |
+
plus: '+',
|
| 468 |
+
pm: '±',
|
| 469 |
+
times: '×',
|
| 470 |
+
div: '÷',
|
| 471 |
+
divide: '÷',
|
| 472 |
+
sdot: '⋅',
|
| 473 |
+
star: '☆',
|
| 474 |
+
starf: '★',
|
| 475 |
+
bigstar: '★',
|
| 476 |
+
lowast: '∗',
|
| 477 |
+
ast: '*',
|
| 478 |
+
midast: '*',
|
| 479 |
+
compfn: '∘',
|
| 480 |
+
smallcircle: '∘',
|
| 481 |
+
bullet: '•',
|
| 482 |
+
bull: '•',
|
| 483 |
+
nbsp: '\u00a0',
|
| 484 |
+
hellip: '…',
|
| 485 |
+
mldr: '…',
|
| 486 |
+
prime: '′',
|
| 487 |
+
Prime: '″',
|
| 488 |
+
tprime: '‴',
|
| 489 |
+
bprime: '‵',
|
| 490 |
+
backprime: '‵',
|
| 491 |
+
minus: '−',
|
| 492 |
+
minusd: '∸',
|
| 493 |
+
dotminus: '∸',
|
| 494 |
+
plusdo: '∔',
|
| 495 |
+
dotplus: '∔',
|
| 496 |
+
plusmn: '±',
|
| 497 |
+
minusplus: '∓',
|
| 498 |
+
mnplus: '∓',
|
| 499 |
+
mp: '∓',
|
| 500 |
+
setminus: '∖',
|
| 501 |
+
smallsetminus: '∖',
|
| 502 |
+
Backslash: '∖',
|
| 503 |
+
setmn: '∖',
|
| 504 |
+
ssetmn: '∖',
|
| 505 |
+
lowbar: '_',
|
| 506 |
+
verbar: '|',
|
| 507 |
+
vert: '|',
|
| 508 |
+
VerticalLine: '|',
|
| 509 |
+
colon: ':',
|
| 510 |
+
Colon: '∷',
|
| 511 |
+
Proportion: '∷',
|
| 512 |
+
ratio: '∶',
|
| 513 |
+
equals: '=',
|
| 514 |
+
ne: '≠',
|
| 515 |
+
nequiv: '≢',
|
| 516 |
+
equiv: '≡',
|
| 517 |
+
Congruent: '≡',
|
| 518 |
+
sim: '∼',
|
| 519 |
+
thicksim: '∼',
|
| 520 |
+
thksim: '∼',
|
| 521 |
+
sime: '≃',
|
| 522 |
+
simeq: '≃',
|
| 523 |
+
TildeEqual: '≃',
|
| 524 |
+
asymp: '≈',
|
| 525 |
+
approx: '≈',
|
| 526 |
+
thickapprox: '≈',
|
| 527 |
+
thkap: '≈',
|
| 528 |
+
TildeTilde: '≈',
|
| 529 |
+
ncong: '≇',
|
| 530 |
+
cong: '≅',
|
| 531 |
+
TildeFullEqual: '≅',
|
| 532 |
+
asympeq: '≍',
|
| 533 |
+
CupCap: '≍',
|
| 534 |
+
bump: '≎',
|
| 535 |
+
Bumpeq: '≎',
|
| 536 |
+
HumpDownHump: '≎',
|
| 537 |
+
bumpe: '≏',
|
| 538 |
+
bumpeq: '≏',
|
| 539 |
+
HumpEqual: '≏',
|
| 540 |
+
le: '≤',
|
| 541 |
+
LessEqual: '≤',
|
| 542 |
+
ge: '≥',
|
| 543 |
+
GreaterEqual: '≥',
|
| 544 |
+
lesseqgtr: '⋚',
|
| 545 |
+
lesseqqgtr: '⪋',
|
| 546 |
+
greater: '>',
|
| 547 |
+
less: '<',
|
| 548 |
+
};
|
| 549 |
+
|
| 550 |
+
/**
|
| 551 |
+
* Mathematical Operators (Advanced)
|
| 552 |
+
* @type {Record<string, string>}
|
| 553 |
+
*/
|
| 554 |
+
export const MATH_ADVANCED = {
|
| 555 |
+
alefsym: 'ℵ',
|
| 556 |
+
aleph: 'ℵ',
|
| 557 |
+
beth: 'ℶ',
|
| 558 |
+
gimel: 'ℷ',
|
| 559 |
+
daleth: 'ℸ',
|
| 560 |
+
forall: '∀',
|
| 561 |
+
ForAll: '∀',
|
| 562 |
+
part: '∂',
|
| 563 |
+
PartialD: '∂',
|
| 564 |
+
exist: '∃',
|
| 565 |
+
Exists: '∃',
|
| 566 |
+
nexist: '∄',
|
| 567 |
+
nexists: '∄',
|
| 568 |
+
empty: '∅',
|
| 569 |
+
emptyset: '∅',
|
| 570 |
+
emptyv: '∅',
|
| 571 |
+
varnothing: '∅',
|
| 572 |
+
nabla: '∇',
|
| 573 |
+
Del: '∇',
|
| 574 |
+
isin: '∈',
|
| 575 |
+
isinv: '∈',
|
| 576 |
+
in: '∈',
|
| 577 |
+
Element: '∈',
|
| 578 |
+
notin: '∉',
|
| 579 |
+
notinva: '∉',
|
| 580 |
+
ni: '∋',
|
| 581 |
+
niv: '∋',
|
| 582 |
+
SuchThat: '∋',
|
| 583 |
+
ReverseElement: '∋',
|
| 584 |
+
notni: '∌',
|
| 585 |
+
notniva: '∌',
|
| 586 |
+
prod: '∏',
|
| 587 |
+
Product: '∏',
|
| 588 |
+
coprod: '∐',
|
| 589 |
+
Coproduct: '∐',
|
| 590 |
+
sum: '∑',
|
| 591 |
+
Sum: '∑',
|
| 592 |
+
minus: '−',
|
| 593 |
+
mp: '∓',
|
| 594 |
+
plusdo: '∔',
|
| 595 |
+
dotplus: '∔',
|
| 596 |
+
setminus: '∖',
|
| 597 |
+
lowast: '∗',
|
| 598 |
+
radic: '√',
|
| 599 |
+
Sqrt: '√',
|
| 600 |
+
prop: '∝',
|
| 601 |
+
propto: '∝',
|
| 602 |
+
Proportional: '∝',
|
| 603 |
+
varpropto: '∝',
|
| 604 |
+
infin: '∞',
|
| 605 |
+
infintie: '⧝',
|
| 606 |
+
ang: '∠',
|
| 607 |
+
angle: '∠',
|
| 608 |
+
angmsd: '∡',
|
| 609 |
+
measuredangle: '∡',
|
| 610 |
+
angsph: '∢',
|
| 611 |
+
mid: '∣',
|
| 612 |
+
VerticalBar: '∣',
|
| 613 |
+
nmid: '∤',
|
| 614 |
+
nsmid: '∤',
|
| 615 |
+
npar: '∦',
|
| 616 |
+
parallel: '∥',
|
| 617 |
+
spar: '∥',
|
| 618 |
+
nparallel: '∦',
|
| 619 |
+
nspar: '∦',
|
| 620 |
+
and: '∧',
|
| 621 |
+
wedge: '∧',
|
| 622 |
+
or: '∨',
|
| 623 |
+
vee: '∨',
|
| 624 |
+
cap: '∩',
|
| 625 |
+
cup: '∪',
|
| 626 |
+
int: '∫',
|
| 627 |
+
Integral: '∫',
|
| 628 |
+
conint: '∮',
|
| 629 |
+
ContourIntegral: '∮',
|
| 630 |
+
Conint: '∯',
|
| 631 |
+
DoubleContourIntegral: '∯',
|
| 632 |
+
Cconint: '∰',
|
| 633 |
+
there4: '∴',
|
| 634 |
+
therefore: '∴',
|
| 635 |
+
Therefore: '∴',
|
| 636 |
+
becaus: '∵',
|
| 637 |
+
because: '∵',
|
| 638 |
+
Because: '∵',
|
| 639 |
+
ratio: '∶',
|
| 640 |
+
Proportion: '∷',
|
| 641 |
+
minusd: '∸',
|
| 642 |
+
dotminus: '∸',
|
| 643 |
+
mDDot: '∺',
|
| 644 |
+
homtht: '∻',
|
| 645 |
+
sim: '∼',
|
| 646 |
+
bsimg: '∽',
|
| 647 |
+
backsim: '∽',
|
| 648 |
+
ac: '∾',
|
| 649 |
+
mstpos: '∾',
|
| 650 |
+
acd: '∿',
|
| 651 |
+
VerticalTilde: '≀',
|
| 652 |
+
wr: '≀',
|
| 653 |
+
wreath: '≀',
|
| 654 |
+
nsime: '≄',
|
| 655 |
+
nsimeq: '≄',
|
| 656 |
+
ncong: '≇',
|
| 657 |
+
simne: '≆',
|
| 658 |
+
ncongdot: '⩭̸',
|
| 659 |
+
ngsim: '≵',
|
| 660 |
+
nsim: '≁',
|
| 661 |
+
napprox: '≉',
|
| 662 |
+
nap: '≉',
|
| 663 |
+
ngeq: '≱',
|
| 664 |
+
nge: '≱',
|
| 665 |
+
nleq: '≰',
|
| 666 |
+
nle: '≰',
|
| 667 |
+
ngtr: '≯',
|
| 668 |
+
ngt: '≯',
|
| 669 |
+
nless: '≮',
|
| 670 |
+
nlt: '≮',
|
| 671 |
+
nprec: '⊀',
|
| 672 |
+
npr: '⊀',
|
| 673 |
+
nsucc: '⊁',
|
| 674 |
+
nsc: '⊁',
|
| 675 |
+
};
|
| 676 |
+
|
| 677 |
+
/**
|
| 678 |
+
* Arrows
|
| 679 |
+
* @type {Record<string, string>}
|
| 680 |
+
*/
|
| 681 |
+
export const ARROWS = {
|
| 682 |
+
larr: '←',
|
| 683 |
+
leftarrow: '←',
|
| 684 |
+
LeftArrow: '←',
|
| 685 |
+
uarr: '↑',
|
| 686 |
+
uparrow: '↑',
|
| 687 |
+
UpArrow: '↑',
|
| 688 |
+
rarr: '→',
|
| 689 |
+
rightarrow: '→',
|
| 690 |
+
RightArrow: '→',
|
| 691 |
+
darr: '↓',
|
| 692 |
+
downarrow: '↓',
|
| 693 |
+
DownArrow: '↓',
|
| 694 |
+
harr: '↔',
|
| 695 |
+
leftrightarrow: '↔',
|
| 696 |
+
LeftRightArrow: '↔',
|
| 697 |
+
varr: '↕',
|
| 698 |
+
updownarrow: '↕',
|
| 699 |
+
UpDownArrow: '↕',
|
| 700 |
+
nwarr: '↖',
|
| 701 |
+
nwarrow: '↖',
|
| 702 |
+
UpperLeftArrow: '↖',
|
| 703 |
+
nearr: '↗',
|
| 704 |
+
nearrow: '↗',
|
| 705 |
+
UpperRightArrow: '↗',
|
| 706 |
+
searr: '↘',
|
| 707 |
+
searrow: '↘',
|
| 708 |
+
LowerRightArrow: '↘',
|
| 709 |
+
swarr: '↙',
|
| 710 |
+
swarrow: '↙',
|
| 711 |
+
LowerLeftArrow: '↙',
|
| 712 |
+
lArr: '⇐',
|
| 713 |
+
Leftarrow: '⇐',
|
| 714 |
+
uArr: '⇑',
|
| 715 |
+
Uparrow: '⇑',
|
| 716 |
+
rArr: '⇒',
|
| 717 |
+
Rightarrow: '⇒',
|
| 718 |
+
dArr: '⇓',
|
| 719 |
+
Downarrow: '⇓',
|
| 720 |
+
hArr: '⇔',
|
| 721 |
+
Leftrightarrow: '⇔',
|
| 722 |
+
iff: '⇔',
|
| 723 |
+
vArr: '⇕',
|
| 724 |
+
Updownarrow: '⇕',
|
| 725 |
+
lAarr: '⇚',
|
| 726 |
+
Lleftarrow: '⇚',
|
| 727 |
+
rAarr: '⇛',
|
| 728 |
+
Rrightarrow: '⇛',
|
| 729 |
+
lrarr: '⇆',
|
| 730 |
+
leftrightarrows: '⇆',
|
| 731 |
+
rlarr: '⇄',
|
| 732 |
+
rightleftarrows: '⇄',
|
| 733 |
+
lrhar: '⇋',
|
| 734 |
+
leftrightharpoons: '⇋',
|
| 735 |
+
ReverseEquilibrium: '⇋',
|
| 736 |
+
rlhar: '⇌',
|
| 737 |
+
rightleftharpoons: '⇌',
|
| 738 |
+
Equilibrium: '⇌',
|
| 739 |
+
udarr: '⇅',
|
| 740 |
+
UpArrowDownArrow: '⇅',
|
| 741 |
+
duarr: '⇵',
|
| 742 |
+
DownArrowUpArrow: '⇵',
|
| 743 |
+
llarr: '⇇',
|
| 744 |
+
leftleftarrows: '⇇',
|
| 745 |
+
rrarr: '⇉',
|
| 746 |
+
rightrightarrows: '⇉',
|
| 747 |
+
ddarr: '⇊',
|
| 748 |
+
downdownarrows: '⇊',
|
| 749 |
+
har: '↽',
|
| 750 |
+
lhard: '↽',
|
| 751 |
+
leftharpoondown: '↽',
|
| 752 |
+
lharu: '↼',
|
| 753 |
+
leftharpoonup: '↼',
|
| 754 |
+
rhard: '⇁',
|
| 755 |
+
rightharpoondown: '⇁',
|
| 756 |
+
rharu: '⇀',
|
| 757 |
+
rightharpoonup: '⇀',
|
| 758 |
+
lsh: '↰',
|
| 759 |
+
Lsh: '↰',
|
| 760 |
+
rsh: '↱',
|
| 761 |
+
Rsh: '↱',
|
| 762 |
+
ldsh: '↲',
|
| 763 |
+
rdsh: '↳',
|
| 764 |
+
hookleftarrow: '↩',
|
| 765 |
+
hookrightarrow: '↪',
|
| 766 |
+
mapstoleft: '↤',
|
| 767 |
+
mapstoup: '↥',
|
| 768 |
+
map: '↦',
|
| 769 |
+
mapsto: '↦',
|
| 770 |
+
mapstodown: '↧',
|
| 771 |
+
crarr: '↵',
|
| 772 |
+
nleftarrow: '↚',
|
| 773 |
+
nleftrightarrow: '↮',
|
| 774 |
+
nrightarrow: '↛',
|
| 775 |
+
nrarr: '↛',
|
| 776 |
+
larrtl: '↢',
|
| 777 |
+
rarrtl: '↣',
|
| 778 |
+
leftarrowtail: '↢',
|
| 779 |
+
rightarrowtail: '↣',
|
| 780 |
+
twoheadleftarrow: '↞',
|
| 781 |
+
twoheadrightarrow: '↠',
|
| 782 |
+
Larr: '↞',
|
| 783 |
+
Rarr: '↠',
|
| 784 |
+
larrhk: '↩',
|
| 785 |
+
rarrhk: '↪',
|
| 786 |
+
larrlp: '↫',
|
| 787 |
+
looparrowleft: '↫',
|
| 788 |
+
rarrlp: '↬',
|
| 789 |
+
looparrowright: '↬',
|
| 790 |
+
harrw: '↭',
|
| 791 |
+
leftrightsquigarrow: '↭',
|
| 792 |
+
nrarrw: '↝̸',
|
| 793 |
+
rarrw: '↝',
|
| 794 |
+
rightsquigarrow: '↝',
|
| 795 |
+
larrbfs: '⤟',
|
| 796 |
+
rarrbfs: '⤠',
|
| 797 |
+
nvHarr: '⤄',
|
| 798 |
+
nvlArr: '⤂',
|
| 799 |
+
nvrArr: '⤃',
|
| 800 |
+
larrfs: '⤝',
|
| 801 |
+
rarrfs: '⤞',
|
| 802 |
+
Map: '⤅',
|
| 803 |
+
larrsim: '⥳',
|
| 804 |
+
rarrsim: '⥴',
|
| 805 |
+
harrcir: '⥈',
|
| 806 |
+
Uarrocir: '⥉',
|
| 807 |
+
lurdshar: '⥊',
|
| 808 |
+
ldrdhar: '⥧',
|
| 809 |
+
ldrushar: '⥋',
|
| 810 |
+
rdldhar: '⥩',
|
| 811 |
+
lrhard: '⥭',
|
| 812 |
+
uharr: '↾',
|
| 813 |
+
uharl: '↿',
|
| 814 |
+
dharr: '⇂',
|
| 815 |
+
dharl: '⇃',
|
| 816 |
+
Uarr: '↟',
|
| 817 |
+
Darr: '↡',
|
| 818 |
+
zigrarr: '⇝',
|
| 819 |
+
nwArr: '⇖',
|
| 820 |
+
neArr: '⇗',
|
| 821 |
+
seArr: '⇘',
|
| 822 |
+
swArr: '⇙',
|
| 823 |
+
nharr: '↮',
|
| 824 |
+
nhArr: '⇎',
|
| 825 |
+
nlarr: '↚',
|
| 826 |
+
nlArr: '⇍',
|
| 827 |
+
nrArr: '⇏',
|
| 828 |
+
larrb: '⇤',
|
| 829 |
+
LeftArrowBar: '⇤',
|
| 830 |
+
rarrb: '⇥',
|
| 831 |
+
RightArrowBar: '⇥',
|
| 832 |
+
};
|
| 833 |
+
|
| 834 |
+
/**
|
| 835 |
+
* Geometric Shapes
|
| 836 |
+
* @type {Record<string, string>}
|
| 837 |
+
*/
|
| 838 |
+
export const SHAPES = {
|
| 839 |
+
square: '□',
|
| 840 |
+
Square: '□',
|
| 841 |
+
squ: '□',
|
| 842 |
+
squf: '▪',
|
| 843 |
+
squarf: '▪',
|
| 844 |
+
blacksquar: '▪',
|
| 845 |
+
blacksquare: '▪',
|
| 846 |
+
FilledVerySmallSquare: '▪',
|
| 847 |
+
blk34: '▓',
|
| 848 |
+
blk12: '▒',
|
| 849 |
+
blk14: '░',
|
| 850 |
+
block: '█',
|
| 851 |
+
srect: '▭',
|
| 852 |
+
rect: '▭',
|
| 853 |
+
sdot: '⋅',
|
| 854 |
+
sdotb: '⊡',
|
| 855 |
+
dotsquare: '⊡',
|
| 856 |
+
triangle: '▵',
|
| 857 |
+
tri: '▵',
|
| 858 |
+
trine: '▵',
|
| 859 |
+
utri: '▵',
|
| 860 |
+
triangledown: '▿',
|
| 861 |
+
dtri: '▿',
|
| 862 |
+
tridown: '▿',
|
| 863 |
+
triangleleft: '◃',
|
| 864 |
+
ltri: '◃',
|
| 865 |
+
triangleright: '▹',
|
| 866 |
+
rtri: '▹',
|
| 867 |
+
blacktriangle: '▴',
|
| 868 |
+
utrif: '▴',
|
| 869 |
+
blacktriangledown: '▾',
|
| 870 |
+
dtrif: '▾',
|
| 871 |
+
blacktriangleleft: '◂',
|
| 872 |
+
ltrif: '◂',
|
| 873 |
+
blacktriangleright: '▸',
|
| 874 |
+
rtrif: '▸',
|
| 875 |
+
loz: '◊',
|
| 876 |
+
lozenge: '◊',
|
| 877 |
+
blacklozenge: '⧫',
|
| 878 |
+
lozf: '⧫',
|
| 879 |
+
bigcirc: '◯',
|
| 880 |
+
xcirc: '◯',
|
| 881 |
+
circ: 'ˆ',
|
| 882 |
+
Circle: '○',
|
| 883 |
+
cir: '○',
|
| 884 |
+
o: '○',
|
| 885 |
+
bullet: '•',
|
| 886 |
+
bull: '•',
|
| 887 |
+
hellip: '…',
|
| 888 |
+
mldr: '…',
|
| 889 |
+
nldr: '‥',
|
| 890 |
+
boxh: '─',
|
| 891 |
+
HorizontalLine: '─',
|
| 892 |
+
boxv: '│',
|
| 893 |
+
boxdr: '┌',
|
| 894 |
+
boxdl: '┐',
|
| 895 |
+
boxur: '└',
|
| 896 |
+
boxul: '┘',
|
| 897 |
+
boxvr: '├',
|
| 898 |
+
boxvl: '┤',
|
| 899 |
+
boxhd: '┬',
|
| 900 |
+
boxhu: '┴',
|
| 901 |
+
boxvh: '┼',
|
| 902 |
+
boxH: '═',
|
| 903 |
+
boxV: '║',
|
| 904 |
+
boxdR: '╒',
|
| 905 |
+
boxDr: '╓',
|
| 906 |
+
boxDR: '╔',
|
| 907 |
+
boxDl: '╕',
|
| 908 |
+
boxdL: '╖',
|
| 909 |
+
boxDL: '╗',
|
| 910 |
+
boxuR: '╘',
|
| 911 |
+
boxUr: '╙',
|
| 912 |
+
boxUR: '╚',
|
| 913 |
+
boxUl: '╜',
|
| 914 |
+
boxuL: '╛',
|
| 915 |
+
boxUL: '╝',
|
| 916 |
+
boxvR: '╞',
|
| 917 |
+
boxVr: '╟',
|
| 918 |
+
boxVR: '╠',
|
| 919 |
+
boxVl: '╢',
|
| 920 |
+
boxvL: '╡',
|
| 921 |
+
boxVL: '╣',
|
| 922 |
+
boxHd: '╤',
|
| 923 |
+
boxhD: '╥',
|
| 924 |
+
boxHD: '╦',
|
| 925 |
+
boxHu: '╧',
|
| 926 |
+
boxhU: '╨',
|
| 927 |
+
boxHU: '╩',
|
| 928 |
+
boxvH: '╪',
|
| 929 |
+
boxVh: '╫',
|
| 930 |
+
boxVH: '╬',
|
| 931 |
+
};
|
| 932 |
+
|
| 933 |
+
/**
|
| 934 |
+
* Punctuation & Diacritics
|
| 935 |
+
* @type {Record<string, string>}
|
| 936 |
+
*/
|
| 937 |
+
export const PUNCTUATION = {
|
| 938 |
+
excl: '!',
|
| 939 |
+
iexcl: '¡',
|
| 940 |
+
brvbar: '¦',
|
| 941 |
+
sect: '§',
|
| 942 |
+
uml: '¨',
|
| 943 |
+
copy: '©',
|
| 944 |
+
ordf: 'ª',
|
| 945 |
+
laquo: '«',
|
| 946 |
+
not: '¬',
|
| 947 |
+
shy: '\u00ad',
|
| 948 |
+
reg: '®',
|
| 949 |
+
macr: '¯',
|
| 950 |
+
deg: '°',
|
| 951 |
+
plusmn: '±',
|
| 952 |
+
sup2: '²',
|
| 953 |
+
sup3: '³',
|
| 954 |
+
acute: '´',
|
| 955 |
+
micro: 'µ',
|
| 956 |
+
para: '¶',
|
| 957 |
+
middot: '·',
|
| 958 |
+
cedil: '¸',
|
| 959 |
+
sup1: '¹',
|
| 960 |
+
ordm: 'º',
|
| 961 |
+
raquo: '»',
|
| 962 |
+
frac14: '¼',
|
| 963 |
+
frac12: '½',
|
| 964 |
+
frac34: '¾',
|
| 965 |
+
iquest: '¿',
|
| 966 |
+
nbsp: '\u00a0',
|
| 967 |
+
comma: ',',
|
| 968 |
+
period: '.',
|
| 969 |
+
colon: ':',
|
| 970 |
+
semi: ';',
|
| 971 |
+
vert: '|',
|
| 972 |
+
Verbar: '‖',
|
| 973 |
+
verbar: '|',
|
| 974 |
+
dblac: '˝',
|
| 975 |
+
circ: 'ˆ',
|
| 976 |
+
caron: 'ˇ',
|
| 977 |
+
breve: '˘',
|
| 978 |
+
dot: '˙',
|
| 979 |
+
ring: '˚',
|
| 980 |
+
ogon: '˛',
|
| 981 |
+
tilde: '˜',
|
| 982 |
+
DiacriticalGrave: '`',
|
| 983 |
+
DiacriticalAcute: '´',
|
| 984 |
+
DiacriticalTilde: '˜',
|
| 985 |
+
DiacriticalDot: '˙',
|
| 986 |
+
DiacriticalDoubleAcute: '˝',
|
| 987 |
+
grave: '`',
|
| 988 |
+
};
|
| 989 |
+
|
| 990 |
+
/**
|
| 991 |
+
* Currency Symbols
|
| 992 |
+
* @type {Record<string, string>}
|
| 993 |
+
*/
|
| 994 |
+
export const CURRENCY = {
|
| 995 |
+
cent: '¢',
|
| 996 |
+
pound: '£',
|
| 997 |
+
curren: '¤',
|
| 998 |
+
yen: '¥',
|
| 999 |
+
euro: '€',
|
| 1000 |
+
dollar: '$',
|
| 1001 |
+
fnof: 'ƒ',
|
| 1002 |
+
inr: '₹',
|
| 1003 |
+
af: '؋',
|
| 1004 |
+
birr: 'ብር',
|
| 1005 |
+
peso: '₱',
|
| 1006 |
+
rub: '₽',
|
| 1007 |
+
won: '₩',
|
| 1008 |
+
yuan: '¥',
|
| 1009 |
+
cedil: '¸',
|
| 1010 |
+
};
|
| 1011 |
+
|
| 1012 |
+
/**
|
| 1013 |
+
* Fractions
|
| 1014 |
+
* @type {Record<string, string>}
|
| 1015 |
+
*/
|
| 1016 |
+
export const FRACTIONS = {
|
| 1017 |
+
frac12: '½',
|
| 1018 |
+
half: '½',
|
| 1019 |
+
frac13: '⅓',
|
| 1020 |
+
frac14: '¼',
|
| 1021 |
+
frac15: '⅕',
|
| 1022 |
+
frac16: '⅙',
|
| 1023 |
+
frac18: '⅛',
|
| 1024 |
+
frac23: '⅔',
|
| 1025 |
+
frac25: '⅖',
|
| 1026 |
+
frac34: '¾',
|
| 1027 |
+
frac35: '⅗',
|
| 1028 |
+
frac38: '⅜',
|
| 1029 |
+
frac45: '⅘',
|
| 1030 |
+
frac56: '⅚',
|
| 1031 |
+
frac58: '⅝',
|
| 1032 |
+
frac78: '⅞',
|
| 1033 |
+
frasl: '⁄',
|
| 1034 |
+
};
|
| 1035 |
+
|
| 1036 |
+
/**
|
| 1037 |
+
* Miscellaneous Symbols
|
| 1038 |
+
* @type {Record<string, string>}
|
| 1039 |
+
*/
|
| 1040 |
+
export const MISC_SYMBOLS = {
|
| 1041 |
+
trade: '™',
|
| 1042 |
+
TRADE: '™',
|
| 1043 |
+
telrec: '⌕',
|
| 1044 |
+
target: '⌖',
|
| 1045 |
+
ulcorn: '⌜',
|
| 1046 |
+
ulcorner: '⌜',
|
| 1047 |
+
urcorn: '⌝',
|
| 1048 |
+
urcorner: '⌝',
|
| 1049 |
+
dlcorn: '⌞',
|
| 1050 |
+
llcorner: '⌞',
|
| 1051 |
+
drcorn: '⌟',
|
| 1052 |
+
lrcorner: '⌟',
|
| 1053 |
+
intercal: '⊺',
|
| 1054 |
+
intcal: '⊺',
|
| 1055 |
+
oplus: '⊕',
|
| 1056 |
+
CirclePlus: '⊕',
|
| 1057 |
+
ominus: '⊖',
|
| 1058 |
+
CircleMinus: '⊖',
|
| 1059 |
+
otimes: '⊗',
|
| 1060 |
+
CircleTimes: '⊗',
|
| 1061 |
+
osol: '⊘',
|
| 1062 |
+
odot: '⊙',
|
| 1063 |
+
CircleDot: '⊙',
|
| 1064 |
+
oast: '⊛',
|
| 1065 |
+
circledast: '⊛',
|
| 1066 |
+
odash: '⊝',
|
| 1067 |
+
circleddash: '⊝',
|
| 1068 |
+
ocirc: '⊚',
|
| 1069 |
+
circledcirc: '⊚',
|
| 1070 |
+
boxplus: '⊞',
|
| 1071 |
+
plusb: '⊞',
|
| 1072 |
+
boxminus: '⊟',
|
| 1073 |
+
minusb: '⊟',
|
| 1074 |
+
boxtimes: '⊠',
|
| 1075 |
+
timesb: '⊠',
|
| 1076 |
+
boxdot: '⊡',
|
| 1077 |
+
sdotb: '⊡',
|
| 1078 |
+
veebar: '⊻',
|
| 1079 |
+
vee: '∨',
|
| 1080 |
+
barvee: '⊽',
|
| 1081 |
+
and: '∧',
|
| 1082 |
+
wedge: '∧',
|
| 1083 |
+
Cap: '⋒',
|
| 1084 |
+
Cup: '⋓',
|
| 1085 |
+
Fork: '⋔',
|
| 1086 |
+
pitchfork: '⋔',
|
| 1087 |
+
epar: '⋕',
|
| 1088 |
+
ltlarr: '⥶',
|
| 1089 |
+
nvap: '≍⃒',
|
| 1090 |
+
nvsim: '∼⃒',
|
| 1091 |
+
nvge: '≥⃒',
|
| 1092 |
+
nvle: '≤⃒',
|
| 1093 |
+
nvlt: '<⃒',
|
| 1094 |
+
nvgt: '>⃒',
|
| 1095 |
+
nvltrie: '⊴⃒',
|
| 1096 |
+
nvrtrie: '⊵⃒',
|
| 1097 |
+
Vdash: '⊩',
|
| 1098 |
+
dashv: '⊣',
|
| 1099 |
+
vDash: '⊨',
|
| 1100 |
+
Vvdash: '⊪',
|
| 1101 |
+
nvdash: '⊬',
|
| 1102 |
+
nvDash: '⊭',
|
| 1103 |
+
nVdash: '⊮',
|
| 1104 |
+
nVDash: '⊯',
|
| 1105 |
+
};
|
| 1106 |
+
|
| 1107 |
+
/**
|
| 1108 |
+
* All entities combined (if you need everything)
|
| 1109 |
+
* @type {Record<string, string>}
|
| 1110 |
+
*/
|
| 1111 |
+
export const ALL_ENTITIES = {
|
| 1112 |
+
...BASIC_LATIN,
|
| 1113 |
+
...LATIN_ACCENTS,
|
| 1114 |
+
...LATIN_EXTENDED,
|
| 1115 |
+
...GREEK,
|
| 1116 |
+
...CYRILLIC,
|
| 1117 |
+
...MATH,
|
| 1118 |
+
...MATH_ADVANCED,
|
| 1119 |
+
...ARROWS,
|
| 1120 |
+
...SHAPES,
|
| 1121 |
+
...PUNCTUATION,
|
| 1122 |
+
...CURRENCY,
|
| 1123 |
+
...FRACTIONS,
|
| 1124 |
+
...MISC_SYMBOLS,
|
| 1125 |
+
};
|
| 1126 |
+
|
| 1127 |
+
export const XML = {
|
| 1128 |
+
amp: "&",
|
| 1129 |
+
apos: "'",
|
| 1130 |
+
gt: ">",
|
| 1131 |
+
lt: "<",
|
| 1132 |
+
quot: "\""
|
| 1133 |
+
}
|
| 1134 |
+
export const COMMON_HTML = {
|
| 1135 |
+
nbsp: '\u00a0',
|
| 1136 |
+
copy: '\u00a9',
|
| 1137 |
+
reg: '\u00ae',
|
| 1138 |
+
trade: '\u2122',
|
| 1139 |
+
mdash: '\u2014',
|
| 1140 |
+
ndash: '\u2013',
|
| 1141 |
+
hellip: '\u2026',
|
| 1142 |
+
laquo: '\u00ab',
|
| 1143 |
+
raquo: '\u00bb',
|
| 1144 |
+
lsquo: '\u2018',
|
| 1145 |
+
rsquo: '\u2019',
|
| 1146 |
+
ldquo: '\u201c',
|
| 1147 |
+
rdquo: '\u201d',
|
| 1148 |
+
bull: '\u2022',
|
| 1149 |
+
para: '\u00b6',
|
| 1150 |
+
sect: '\u00a7',
|
| 1151 |
+
deg: '\u00b0',
|
| 1152 |
+
frac12: '\u00bd',
|
| 1153 |
+
frac14: '\u00bc',
|
| 1154 |
+
frac34: '\u00be',
|
| 1155 |
+
}
|
| 1156 |
+
// ---------------------------------------------------------------------------
|
| 1157 |
+
// Note: NUMERIC_ENTITIES (&#NNN; / &#xHH;) are handled by the scanner directly
|
| 1158 |
+
// via String.fromCodePoint() without any map lookup.
|
| 1159 |
+
// ---------------------------------------------------------------------------
|
node_modules/@nodable/entities/src/entityTries.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// entityTries.js
|
| 2 |
+
// Builds integer-keyed tries so the decoder never allocates a string object
|
| 3 |
+
// during lookup — every key is a plain charCode number.
|
| 4 |
+
//
|
| 5 |
+
// trie1: Map<code0, entity>
|
| 6 |
+
// trie2: Map<code0, Map<code1, entity>>
|
| 7 |
+
// trie3: Map<code0, Map<code1, Map<code2, entity>>>
|
| 8 |
+
|
| 9 |
+
import { ALL_ENTITIES } from './entities.js';
|
| 10 |
+
|
| 11 |
+
// Reverse map: character sequence → "&name;"
|
| 12 |
+
const CHAR_TO_ENTITY = new Map();
|
| 13 |
+
for (const [name, chars] of Object.entries(ALL_ENTITIES)) {
|
| 14 |
+
CHAR_TO_ENTITY.set(chars, `&${name};`);
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
export const trie1 = new Map(); // code0 → entity string
|
| 18 |
+
export const trie2 = new Map(); // code0 → Map → entity string
|
| 19 |
+
export const trie3 = new Map(); // code0 → Map → Map → entity string
|
| 20 |
+
|
| 21 |
+
for (const [chars, entity] of CHAR_TO_ENTITY) {
|
| 22 |
+
const len = chars.length;
|
| 23 |
+
|
| 24 |
+
if (len === 1) {
|
| 25 |
+
const c0 = chars.charCodeAt(0);
|
| 26 |
+
// Keep shortest match only if no longer match already claimed this code
|
| 27 |
+
// (longer matches are inserted in the same pass so we just overwrite —
|
| 28 |
+
// trie1 is only consulted after trie2/trie3 both miss, so no conflict)
|
| 29 |
+
trie1.set(c0, entity);
|
| 30 |
+
|
| 31 |
+
} else if (len === 2) {
|
| 32 |
+
const c0 = chars.charCodeAt(0);
|
| 33 |
+
const c1 = chars.charCodeAt(1);
|
| 34 |
+
let inner = trie2.get(c0);
|
| 35 |
+
if (inner === undefined) { inner = new Map(); trie2.set(c0, inner); }
|
| 36 |
+
inner.set(c1, entity);
|
| 37 |
+
|
| 38 |
+
} else if (len === 3) {
|
| 39 |
+
const c0 = chars.charCodeAt(0);
|
| 40 |
+
const c1 = chars.charCodeAt(1);
|
| 41 |
+
const c2 = chars.charCodeAt(2);
|
| 42 |
+
let mid = trie3.get(c0);
|
| 43 |
+
if (mid === undefined) { mid = new Map(); trie3.set(c0, mid); }
|
| 44 |
+
let inner = mid.get(c1);
|
| 45 |
+
if (inner === undefined) { inner = new Map(); mid.set(c1, inner); }
|
| 46 |
+
inner.set(c2, entity);
|
| 47 |
+
}
|
| 48 |
+
// HTML5 has no named entity whose character sequence is longer than 3 chars
|
| 49 |
+
}
|
node_modules/@nodable/entities/src/index.d.ts
ADDED
|
@@ -0,0 +1,341 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// ---------------------------------------------------------------------------
|
| 2 |
+
// @nodable/entities — TypeScript declarations
|
| 3 |
+
// ---------------------------------------------------------------------------
|
| 4 |
+
|
| 5 |
+
/** A function-based entity replacement value (used for numeric refs). */
|
| 6 |
+
export type EntityValFn = (match: string, captured: string, ...rest: unknown[]) => string;
|
| 7 |
+
|
| 8 |
+
// ---------------------------------------------------------------------------
|
| 9 |
+
// Entity registration hook
|
| 10 |
+
// ---------------------------------------------------------------------------
|
| 11 |
+
|
| 12 |
+
/**
|
| 13 |
+
* Actions returned by `onExternalEntity` / `onInputEntity` hooks.
|
| 14 |
+
* Use the `ENTITY_ACTION` constant object instead of raw strings to avoid typos.
|
| 15 |
+
*
|
| 16 |
+
* - `'allow'` — register and expand the entity normally
|
| 17 |
+
* - `'block'` — silently skip the entity (not registered, treated as unknown)
|
| 18 |
+
* - `'throw'` — abort registration with an error
|
| 19 |
+
*/
|
| 20 |
+
export type EntityHookAction = 'allow' | 'block' | 'throw';
|
| 21 |
+
|
| 22 |
+
/**
|
| 23 |
+
* Immutable constant bag for entity registration hook return values.
|
| 24 |
+
*
|
| 25 |
+
* @example
|
| 26 |
+
* import { ENTITY_ACTION } from '@nodable/entities';
|
| 27 |
+
* const dec = new EntityDecoder({
|
| 28 |
+
* onInputEntity: (_name, _value) => ENTITY_ACTION.BLOCK,
|
| 29 |
+
* });
|
| 30 |
+
*/
|
| 31 |
+
export const ENTITY_ACTION: Readonly<{
|
| 32 |
+
/** Register and expand the entity normally. */
|
| 33 |
+
ALLOW: 'allow';
|
| 34 |
+
/** Silently skip this entity — it will not be registered. */
|
| 35 |
+
BLOCK: 'block';
|
| 36 |
+
/** Throw an error, aborting entity registration. */
|
| 37 |
+
THROW: 'throw';
|
| 38 |
+
}>;
|
| 39 |
+
|
| 40 |
+
/**
|
| 41 |
+
* Callback signature for `onExternalEntity` and `onInputEntity` hooks.
|
| 42 |
+
*
|
| 43 |
+
* Called once per entity **at registration time** (not at decode time).
|
| 44 |
+
* Return `ENTITY_ACTION.ALLOW` (or `'allow'`) to accept, `ENTITY_ACTION.BLOCK`
|
| 45 |
+
* to silently skip, or `ENTITY_ACTION.THROW` to raise an error.
|
| 46 |
+
*
|
| 47 |
+
* @param name — the entity name without `&` / `;`, e.g. `"brand"`
|
| 48 |
+
* @param value — the resolved string value after any `{regex,val}` unwrapping
|
| 49 |
+
*/
|
| 50 |
+
export type EntityRegistrationHook = (name: string, value: string) => EntityHookAction;
|
| 51 |
+
|
| 52 |
+
// ---------------------------------------------------------------------------
|
| 53 |
+
// Encoder options
|
| 54 |
+
// ---------------------------------------------------------------------------
|
| 55 |
+
|
| 56 |
+
export interface EntityEncoderOptions {
|
| 57 |
+
/**
|
| 58 |
+
* Whether to encode XML unsafe characters: `&`, `<`, `>`, `"`, `'`.
|
| 59 |
+
* @default true
|
| 60 |
+
*/
|
| 61 |
+
encodeXmlSafe?: boolean;
|
| 62 |
+
|
| 63 |
+
/**
|
| 64 |
+
* Whether to encode non‑ASCII characters (e.g. `é` → `é`) using the
|
| 65 |
+
* built‑in named entity trie.
|
| 66 |
+
* @default true
|
| 67 |
+
*/
|
| 68 |
+
encodeAllNamed?: boolean;
|
| 69 |
+
|
| 70 |
+
/**
|
| 71 |
+
* Maximum number of replacements performed **cumulatively** across all
|
| 72 |
+
* `encode()` calls. `0` means unlimited.
|
| 73 |
+
*
|
| 74 |
+
* Use `reset()` to reset the internal counter.
|
| 75 |
+
* @default 0
|
| 76 |
+
*/
|
| 77 |
+
maxReplacements?: number;
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
// ---------------------------------------------------------------------------
|
| 81 |
+
// EntityEncoder class
|
| 82 |
+
// ---------------------------------------------------------------------------
|
| 83 |
+
|
| 84 |
+
/**
|
| 85 |
+
* High‑performance encoder that replaces characters with XML/HTML entities.
|
| 86 |
+
*
|
| 87 |
+
* - Escapes XML unsafe characters (`&`, `<`, `>`, `"`, `'`) when `encodeXmlSafe` is true.
|
| 88 |
+
* - Replaces non‑ASCII characters (e.g. `é`, `©`) with named entities using
|
| 89 |
+
* a compact trie‑based lookup when `encodeAllNamed` is true.
|
| 90 |
+
* - Supports a cumulative replacement limit (`maxReplacements`) that persists
|
| 91 |
+
* across multiple `encode()` calls until `reset()` is called.
|
| 92 |
+
*
|
| 93 |
+
* @example
|
| 94 |
+
* const encoder = new EntityEncoder({ encodeXmlSafe: true, encodeAllNamed: true });
|
| 95 |
+
* encoder.encode('<foo>'); // "<foo>"
|
| 96 |
+
* encoder.encode('© 2025'); // "© 2025"
|
| 97 |
+
*
|
| 98 |
+
* // With limit
|
| 99 |
+
* const limited = new EntityEncoder({ maxReplacements: 2 });
|
| 100 |
+
* limited.encode('<>&'); // "<>&" (third replacement omitted)
|
| 101 |
+
* limited.reset(); // reset counter
|
| 102 |
+
*/
|
| 103 |
+
export class EntityEncoder {
|
| 104 |
+
constructor(options?: EntityEncoderOptions);
|
| 105 |
+
|
| 106 |
+
/**
|
| 107 |
+
* Encode a string by replacing XML‑unsafe characters and (optionally)
|
| 108 |
+
* non‑ASCII characters with named entities.
|
| 109 |
+
*
|
| 110 |
+
* If `maxReplacements` is set and the cumulative limit has been reached,
|
| 111 |
+
* the input string is returned unchanged.
|
| 112 |
+
*
|
| 113 |
+
* @returns Encoded string (may be identical to input if no replacements needed
|
| 114 |
+
* or the limit has been exhausted).
|
| 115 |
+
*/
|
| 116 |
+
encode(str: string): string;
|
| 117 |
+
|
| 118 |
+
/**
|
| 119 |
+
* Reset the internal replacement counter.
|
| 120 |
+
* Does **not** change `encodeXmlSafe`, `encodeAllNamed`, or `maxReplacements`.
|
| 121 |
+
*/
|
| 122 |
+
reset(): void;
|
| 123 |
+
}
|
| 124 |
+
|
| 125 |
+
// ---------------------------------------------------------------------------
|
| 126 |
+
// Constructor options for EntityDecoder (existing)
|
| 127 |
+
// ---------------------------------------------------------------------------
|
| 128 |
+
|
| 129 |
+
/**
|
| 130 |
+
* Controls which entity categories count toward the expansion limits.
|
| 131 |
+
*
|
| 132 |
+
* - `'external'` — only untrusted / injected entities (default)
|
| 133 |
+
* - `'base'` — only built‑in XML entities + user‑supplied `namedEntities`
|
| 134 |
+
* - `'all'` — all entities regardless of tier
|
| 135 |
+
* - `string[]` — explicit combination, e.g. `['external', 'base']`
|
| 136 |
+
*/
|
| 137 |
+
export type ApplyLimitsTo = 'external' | 'base' | 'all' | Array<'external' | 'base'>;
|
| 138 |
+
|
| 139 |
+
export interface EntityDecoderLimitOptions {
|
| 140 |
+
/**
|
| 141 |
+
* Maximum number of entity references expanded **per document**.
|
| 142 |
+
* `0` means unlimited.
|
| 143 |
+
* @default 0
|
| 144 |
+
*/
|
| 145 |
+
maxTotalExpansions?: number;
|
| 146 |
+
|
| 147 |
+
/**
|
| 148 |
+
* Maximum number of characters **added** by entity expansion per document.
|
| 149 |
+
* `0` means unlimited.
|
| 150 |
+
* @default 0
|
| 151 |
+
*/
|
| 152 |
+
maxExpandedLength?: number;
|
| 153 |
+
|
| 154 |
+
/**
|
| 155 |
+
* Which entity tiers count toward the expansion limits.
|
| 156 |
+
*
|
| 157 |
+
* - `'external'` (default) – only input/runtime + persistent external entities
|
| 158 |
+
* - `'base'` – only built‑in XML + `namedEntities`
|
| 159 |
+
* - `'all'` – every entity regardless of tier
|
| 160 |
+
* - `string[]` – explicit combination, e.g. `['external', 'base']`
|
| 161 |
+
*
|
| 162 |
+
* @default 'external'
|
| 163 |
+
*/
|
| 164 |
+
applyLimitsTo?: ApplyLimitsTo;
|
| 165 |
+
}
|
| 166 |
+
|
| 167 |
+
export interface EntityDecoderNCROptions {
|
| 168 |
+
/**
|
| 169 |
+
* XML version used for NCR classification.
|
| 170 |
+
* @default 1.0
|
| 171 |
+
*/
|
| 172 |
+
xmlVersion?: 1.0 | 1.1;
|
| 173 |
+
|
| 174 |
+
/**
|
| 175 |
+
* Base action for all numeric references.
|
| 176 |
+
* @default 'allow'
|
| 177 |
+
*/
|
| 178 |
+
onNCR?: 'allow' | 'leave' | 'remove' | 'throw';
|
| 179 |
+
|
| 180 |
+
/**
|
| 181 |
+
* Action for null NCR (U+0000).
|
| 182 |
+
* @default 'remove'
|
| 183 |
+
*/
|
| 184 |
+
nullNCR?: 'remove' | 'throw';
|
| 185 |
+
}
|
| 186 |
+
|
| 187 |
+
export interface EntityDecoderOptions {
|
| 188 |
+
/**
|
| 189 |
+
* Extra named entities merged into the **base map** (trusted, counts as `'base'` tier).
|
| 190 |
+
* These are combined with the built‑in XML entities (`lt`, `gt`, `quot`, `apos`).
|
| 191 |
+
* Values containing `&` are silently skipped to prevent recursive expansion.
|
| 192 |
+
*
|
| 193 |
+
* @default null
|
| 194 |
+
*/
|
| 195 |
+
namedEntities?: Record<string, string | { regex: RegExp; val: string | EntityValFn }> | null;
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
/**
|
| 199 |
+
* Hook called once on the fully decoded string (after all replacements).
|
| 200 |
+
*
|
| 201 |
+
* - Receives `(resolved, original)` and **must return a string**.
|
| 202 |
+
* - To reject expansion, return `original`.
|
| 203 |
+
* - To sanitize, return a cleaned version of `resolved`.
|
| 204 |
+
*
|
| 205 |
+
* @example
|
| 206 |
+
* postCheck: (resolved, original) =>
|
| 207 |
+
* /<[a-z]/i.test(resolved) ? original : resolved
|
| 208 |
+
*/
|
| 209 |
+
postCheck?: ((resolved: string, original: string) => string) | null;
|
| 210 |
+
|
| 211 |
+
/**
|
| 212 |
+
* Whether numeric character references (`&#NNN;`, `&#xHH;`) are allowed.
|
| 213 |
+
* @default true
|
| 214 |
+
*/
|
| 215 |
+
numericAllowed?: boolean;
|
| 216 |
+
|
| 217 |
+
/**
|
| 218 |
+
* Array of entity names or numeric references to leave unexpanded.
|
| 219 |
+
* @default []
|
| 220 |
+
*/
|
| 221 |
+
leave?: string[];
|
| 222 |
+
|
| 223 |
+
/**
|
| 224 |
+
* Array of entity names or numeric references to remove.
|
| 225 |
+
* @default []
|
| 226 |
+
*/
|
| 227 |
+
remove?: string[];
|
| 228 |
+
|
| 229 |
+
/**
|
| 230 |
+
* Security limits for entity expansion.
|
| 231 |
+
*/
|
| 232 |
+
limit?: EntityDecoderLimitOptions;
|
| 233 |
+
|
| 234 |
+
/**
|
| 235 |
+
* Numeric Character Reference (NCR) policy.
|
| 236 |
+
*/
|
| 237 |
+
ncr?: EntityDecoderNCROptions;
|
| 238 |
+
|
| 239 |
+
/**
|
| 240 |
+
* Hook called once **at registration time** for each entity passed to
|
| 241 |
+
* `setExternalEntities()` or `addExternalEntity()`.
|
| 242 |
+
*
|
| 243 |
+
* - `'allow'` (or `ENTITY_ACTION.ALLOW`) — register the entity normally (default)
|
| 244 |
+
* - `'block'` (or `ENTITY_ACTION.BLOCK`) — silently skip; the entity is not stored
|
| 245 |
+
* - `'throw'` (or `ENTITY_ACTION.THROW`) — abort registration with an `Error`
|
| 246 |
+
*
|
| 247 |
+
* The hook receives the entity name (without `&`/`;`) and the resolved string
|
| 248 |
+
* value. It is **not** called during `decode()` — only when entities are added.
|
| 249 |
+
*
|
| 250 |
+
* @example
|
| 251 |
+
* const dec = new EntityDecoder({
|
| 252 |
+
* onExternalEntity: (name, value) =>
|
| 253 |
+
* DANGEROUS_NAMES.has(name) ? ENTITY_ACTION.BLOCK : ENTITY_ACTION.ALLOW,
|
| 254 |
+
* });
|
| 255 |
+
*/
|
| 256 |
+
onExternalEntity?: EntityRegistrationHook | null;
|
| 257 |
+
|
| 258 |
+
/**
|
| 259 |
+
* Hook called once **at registration time** for each entity passed to
|
| 260 |
+
* `addInputEntities()`.
|
| 261 |
+
*
|
| 262 |
+
* Follows the same `'allow' | 'block' | 'throw'` contract as `onExternalEntity`.
|
| 263 |
+
*
|
| 264 |
+
* @example
|
| 265 |
+
* const dec = new EntityDecoder({
|
| 266 |
+
* // Block all input / DOCTYPE entities unconditionally
|
| 267 |
+
* onInputEntity: () => ENTITY_ACTION.BLOCK,
|
| 268 |
+
* });
|
| 269 |
+
*/
|
| 270 |
+
onInputEntity?: EntityRegistrationHook | null;
|
| 271 |
+
}
|
| 272 |
+
|
| 273 |
+
// ---------------------------------------------------------------------------
|
| 274 |
+
// EntityDecoder class (default export)
|
| 275 |
+
// ---------------------------------------------------------------------------
|
| 276 |
+
|
| 277 |
+
/**
|
| 278 |
+
* Single‑pass, zero‑regex entity decoder for XML/HTML content.
|
| 279 |
+
*
|
| 280 |
+
* ## Entity lookup priority (highest → lowest)
|
| 281 |
+
* 1. **input / runtime** – injected via `addInputEntities()` (DOCTYPE per document)
|
| 282 |
+
* 2. **persistent external** – set via `setExternalEntities()` / `addExternalEntity()`
|
| 283 |
+
* 3. **base map** – built‑in XML entities + user‑supplied `namedEntities`
|
| 284 |
+
*
|
| 285 |
+
* Numeric references (`&#NNN;`, `&#xHH;`) are resolved directly and count as the `'base'` tier.
|
| 286 |
+
*
|
| 287 |
+
* @example
|
| 288 |
+
* const decoder = new EntityDecoder({
|
| 289 |
+
* namedEntities: COMMON_HTML,
|
| 290 |
+
* maxTotalExpansions: 100
|
| 291 |
+
* });
|
| 292 |
+
* decoder.setExternalEntities({ brand: 'Acme' });
|
| 293 |
+
*
|
| 294 |
+
* decoder.addInputEntities({ version: '1.0' });
|
| 295 |
+
* decoder.decode('&brand; v&version; <'); // 'Acme v1.0 <'
|
| 296 |
+
*
|
| 297 |
+
* decoder.reset(); // clears input entities + counters, keeps external entities
|
| 298 |
+
*/
|
| 299 |
+
export default class EntityDecoder {
|
| 300 |
+
constructor(options?: EntityDecoderOptions);
|
| 301 |
+
|
| 302 |
+
setExternalEntities(
|
| 303 |
+
map: Record<string, string | { regex: RegExp; val: string | EntityValFn }>
|
| 304 |
+
): void;
|
| 305 |
+
|
| 306 |
+
addExternalEntity(key: string, value: string): void;
|
| 307 |
+
|
| 308 |
+
addInputEntities(
|
| 309 |
+
map: Record<
|
| 310 |
+
string,
|
| 311 |
+
| string
|
| 312 |
+
| { regx: RegExp; val: string | EntityValFn }
|
| 313 |
+
| { regex: RegExp; val: string | EntityValFn }
|
| 314 |
+
>
|
| 315 |
+
): void;
|
| 316 |
+
|
| 317 |
+
reset(): this;
|
| 318 |
+
|
| 319 |
+
decode(str: string): string;
|
| 320 |
+
}
|
| 321 |
+
|
| 322 |
+
// ---------------------------------------------------------------------------
|
| 323 |
+
// Named entity group exports (for use with `namedEntities` option)
|
| 324 |
+
// ---------------------------------------------------------------------------
|
| 325 |
+
|
| 326 |
+
export const COMMON_HTML: Record<string, string>;
|
| 327 |
+
export const ALL_ENTITIES: Record<string, string>;
|
| 328 |
+
export const XML: Record<string, string>;
|
| 329 |
+
export const BASIC_LATIN: Record<string, string>;
|
| 330 |
+
export const LATIN_ACCENTS: Record<string, string>;
|
| 331 |
+
export const LATIN_EXTENDED: Record<string, string>;
|
| 332 |
+
export const GREEK: Record<string, string>;
|
| 333 |
+
export const CYRILLIC: Record<string, string>;
|
| 334 |
+
export const MATH: Record<string, string>;
|
| 335 |
+
export const MATH_ADVANCED: Record<string, string>;
|
| 336 |
+
export const ARROWS: Record<string, string>;
|
| 337 |
+
export const SHAPES: Record<string, string>;
|
| 338 |
+
export const PUNCTUATION: Record<string, string>;
|
| 339 |
+
export const CURRENCY: Record<string, string>;
|
| 340 |
+
export const FRACTIONS: Record<string, string>;
|
| 341 |
+
export const MISC_SYMBOLS: Record<string, string>;
|
node_modules/@nodable/entities/src/index.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* @nodable/entities
|
| 3 |
+
*
|
| 4 |
+
* Standalone, zero-dependency XML/HTML entity replacement.
|
| 5 |
+
*
|
| 6 |
+
|
| 7 |
+
*/
|
| 8 |
+
|
| 9 |
+
export { default as EntityDecoder, ENTITY_ACTION } from './EntityDecoder.js';
|
| 10 |
+
export {
|
| 11 |
+
COMMON_HTML,
|
| 12 |
+
XML,
|
| 13 |
+
ALL_ENTITIES,
|
| 14 |
+
ARROWS,
|
| 15 |
+
BASIC_LATIN,
|
| 16 |
+
CURRENCY,
|
| 17 |
+
MATH,
|
| 18 |
+
MATH_ADVANCED,
|
| 19 |
+
CYRILLIC,
|
| 20 |
+
FRACTIONS,
|
| 21 |
+
GREEK,
|
| 22 |
+
LATIN_ACCENTS,
|
| 23 |
+
LATIN_EXTENDED,
|
| 24 |
+
MISC_SYMBOLS,
|
| 25 |
+
PUNCTUATION,
|
| 26 |
+
SHAPES,
|
| 27 |
+
} from './entities.js';
|
| 28 |
+
|
| 29 |
+
export { default as EntityEncoder } from './EntityEncoder.js';
|
node_modules/@open-draft/deferred-promise/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2022–present Artem Zakharchenko
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
| 6 |
+
of this software and associated documentation files (the "Software"), to deal
|
| 7 |
+
in the Software without restriction, including without limitation the rights
|
| 8 |
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
| 9 |
+
copies of the Software, and to permit persons to whom the Software is
|
| 10 |
+
furnished to do so, subject to the following conditions:
|
| 11 |
+
|
| 12 |
+
The above copyright notice and this permission notice shall be included in all
|
| 13 |
+
copies or substantial portions of the Software.
|
| 14 |
+
|
| 15 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
| 16 |
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
| 17 |
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
| 18 |
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
| 19 |
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
| 20 |
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
| 21 |
+
SOFTWARE.
|
node_modules/@open-dra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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Deferred Promise
|
| 2 |
+
|
| 3 |
+
The `DeferredPromise` class is a Promise-compatible abstraction that defers resolving/rejecting promises to another closure. This class is primarily useful when one part of your system establishes as promise but another part of your system fulfills it.
|
| 4 |
+
|
| 5 |
+
> This class is conceptually inspired by the [`createDeferredPromise()`](https://github.com/nodejs/node/blob/696fd4b14fc34cc2d01497a3abd9bb441b89be50/lib/internal/util.js#L468-L477) internal utility in Node.js. Unlike the Node.js implementation, however, `DeferredProimse` _extends_ a native `Promise`, allowing the consumer to handle deferred promises like regular promises (no `.promise` instance nesting).
|
| 6 |
+
|
| 7 |
+
## Getting started
|
| 8 |
+
|
| 9 |
+
```sh
|
| 10 |
+
npm install @open-draft/deferred-promise
|
| 11 |
+
```
|
| 12 |
+
|
| 13 |
+
## Documentation
|
| 14 |
+
|
| 15 |
+
- [**`createDeferredExecutor()`**](#createdeferredexecutor)
|
| 16 |
+
- [`DeferredExecutor.state`](#deferredexecutorstate)
|
| 17 |
+
- [`DeferredExecutor.resolve()`](#deferredexecutorresolve)
|
| 18 |
+
- [`DeferredExecutor.reject()`](#deferredexecutorreject)
|
| 19 |
+
- [`DeferredExecutor.rejectionReason`](#deferredexecutorrejectionreason)
|
| 20 |
+
- [**Class: `DeferredPromise`**](#class-deferredpromise)
|
| 21 |
+
- [`new DeferredPromise()`](#new-deferredpromise)
|
| 22 |
+
- [`deferredPromise.state`](#deferredpromisestate)
|
| 23 |
+
- [`deferredPromise.resolve()`](#deferredpromiseresolve)
|
| 24 |
+
- [`deferredPromise.reject()`](#deferredpromisereject)
|
| 25 |
+
- [`deferredPromise.rejectionReason`](#deferredpromiserejectionreason)
|
| 26 |
+
|
| 27 |
+
---
|
| 28 |
+
|
| 29 |
+
## `createDeferredExecutor()`
|
| 30 |
+
|
| 31 |
+
Creates a Promise executor function that delegates its resolution to the current scope.
|
| 32 |
+
|
| 33 |
+
```js
|
| 34 |
+
import { createDeferredExecutor } from '@open-draft/deferred-promise'
|
| 35 |
+
|
| 36 |
+
const executor = createDeferredExecutor()
|
| 37 |
+
const promise = new Promise(executor)
|
| 38 |
+
|
| 39 |
+
executor.resolve('hello')
|
| 40 |
+
// executor.reject(new Error('Reason'))
|
| 41 |
+
```
|
| 42 |
+
|
| 43 |
+
Deferred executor allows you to control any promise remotely and doesn't affect the Promise instance in any way. Similar to the [`DeferredPromise`](#class-deferredpromise) instance, the deferred executor exposes additional promise properties like `state`, `rejectionReason`, `resolve`, and `reject`. In fact, the `DeferredPromise` class is implemented on top of the deferred executor.
|
| 44 |
+
|
| 45 |
+
```js
|
| 46 |
+
const executor = createDeferredExecutor()
|
| 47 |
+
const promise = new Promise(executor)
|
| 48 |
+
|
| 49 |
+
executor.reject('reason')
|
| 50 |
+
|
| 51 |
+
nextTick(() => {
|
| 52 |
+
console.log(executor.rejectionReason) // "reason"
|
| 53 |
+
})
|
| 54 |
+
```
|
| 55 |
+
|
| 56 |
+
### `DeferredExecutor.state`
|
| 57 |
+
|
| 58 |
+
- `<"pending" | "fulfilled" | "rejected">` **Default:** `"pending"`
|
| 59 |
+
|
| 60 |
+
```js
|
| 61 |
+
const executor = createDeferredExecutor()
|
| 62 |
+
const promise = new Promise(executor)
|
| 63 |
+
|
| 64 |
+
console.log(executor.state) // "pending"
|
| 65 |
+
```
|
| 66 |
+
|
| 67 |
+
Calling [`resolve()`](#deferredexecutorresolve) and [`reject()`](#deferredexecutorreject) methods of the executor transitions the state to "fulfilled" and "rejected" respectively.
|
| 68 |
+
|
| 69 |
+
### `DeferredExecutor.resolve()`
|
| 70 |
+
|
| 71 |
+
Resolves the promise with a given value.
|
| 72 |
+
|
| 73 |
+
```js
|
| 74 |
+
const executor = createDeferredExecutor()
|
| 75 |
+
const promise = new Promise(executor)
|
| 76 |
+
|
| 77 |
+
console.log(executor.state) // "pending"
|
| 78 |
+
|
| 79 |
+
executor.resolve()
|
| 80 |
+
|
| 81 |
+
// The promise state is still "pending"
|
| 82 |
+
// because promises are settled in the next microtask.
|
| 83 |
+
console.log(executor.state) // "pending"
|
| 84 |
+
|
| 85 |
+
nextTick(() => {
|
| 86 |
+
// In the next microtask, the promise's state is resolved.
|
| 87 |
+
console.log(executor.state) // "fulfilled"
|
| 88 |
+
})
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
### `DeferredExecutor.reject()`
|
| 92 |
+
|
| 93 |
+
Rejects the promise with a given reason.
|
| 94 |
+
|
| 95 |
+
```js
|
| 96 |
+
const executor = createDeferredExecutor()
|
| 97 |
+
const promise = new Promise(executor)
|
| 98 |
+
|
| 99 |
+
executor.reject(new Error('Failed to fetch'))
|
| 100 |
+
|
| 101 |
+
nextTick(() => {
|
| 102 |
+
console.log(executor.state) // "rejected"
|
| 103 |
+
console.log(executor.rejectionReason) // Error("Failed to fetch")
|
| 104 |
+
})
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
You can access the rejection reason of the promise at any time by the [`rejectionReason`](#deferredexecutorrejectionreason) property of the deferred executor.
|
| 108 |
+
|
| 109 |
+
### `DeferredExecutor.rejectionReason`
|
| 110 |
+
|
| 111 |
+
Returns the reason of the promise rejection. If no reason has been provided to the `reject()` call, `undefined` is returned instead.
|
| 112 |
+
|
| 113 |
+
```js
|
| 114 |
+
const executor = createDeferredExecutor()
|
| 115 |
+
const promise = new Promise(executor)
|
| 116 |
+
|
| 117 |
+
executor.reject(new Error('Internal Server Error'))
|
| 118 |
+
|
| 119 |
+
nextTick(() => {
|
| 120 |
+
console.log(executor.rejectionReason) // Error("Internal Server Error")
|
| 121 |
+
})
|
| 122 |
+
```
|
| 123 |
+
|
| 124 |
+
---
|
| 125 |
+
|
| 126 |
+
## Class: `DeferredPromise`
|
| 127 |
+
|
| 128 |
+
### `new DeferredPromise()`
|
| 129 |
+
|
| 130 |
+
Creates a new instance of a deferred promise.
|
| 131 |
+
|
| 132 |
+
```js
|
| 133 |
+
import { DeferredPromise } from '@open-draft/deferred-promise'
|
| 134 |
+
|
| 135 |
+
const promise = new DeferredPromise()
|
| 136 |
+
```
|
| 137 |
+
|
| 138 |
+
A deferred promise is a Promise-compatible class that constructs a regular Promise instance under the hood, controlling it via the [deferred executor](#createdeferredexecutor).
|
| 139 |
+
|
| 140 |
+
A deferred promise is fully compatible with the regular Promise, both type- and runtime-wise, e.g. a deferred promise can be chained and awaited normally.
|
| 141 |
+
|
| 142 |
+
```js
|
| 143 |
+
const promise = new DeferredPromise()
|
| 144 |
+
.then((value) => value.toUpperCase())
|
| 145 |
+
.then((value) => value.substring(0, 2))
|
| 146 |
+
.catch((error) => console.error(error))
|
| 147 |
+
|
| 148 |
+
await promise
|
| 149 |
+
```
|
| 150 |
+
|
| 151 |
+
Unlike the regular Promise, however, a deferred promise doesn't accept the `executor` function as the constructor argument. Instead, the resolution of the deferred promise is deferred to the current scope (thus the name).
|
| 152 |
+
|
| 153 |
+
```js
|
| 154 |
+
function getPort() {
|
| 155 |
+
// Notice that you don't provide any executor function
|
| 156 |
+
// when constructing a deferred promise.
|
| 157 |
+
const portPromise = new DeferredPromise()
|
| 158 |
+
|
| 159 |
+
port.on('open', (port) => {
|
| 160 |
+
// Resolve the deferred promise whenever necessary.
|
| 161 |
+
portPromise.resolve(port)
|
| 162 |
+
})
|
| 163 |
+
|
| 164 |
+
// Return the deferred promise immediately.
|
| 165 |
+
return portPromise
|
| 166 |
+
}
|
| 167 |
+
```
|
| 168 |
+
|
| 169 |
+
Use the [`resolve()`](#deferredpromiseresolve) and [`reject()`](#deferredpromisereject) methods of the deferred promise instance to resolve and reject that promise respectively.
|
| 170 |
+
|
| 171 |
+
### `deferredPromise.state`
|
| 172 |
+
|
| 173 |
+
See [`DeferredExecutor.state`](#deferredexecutorstate)
|
| 174 |
+
|
| 175 |
+
### `deferredPromise.resolve()`
|
| 176 |
+
|
| 177 |
+
See [`DeferredExecutor.resolve()`](#deferredexecutorresolve)
|
| 178 |
+
|
| 179 |
+
### `deferredPromise.reject()`
|
| 180 |
+
|
| 181 |
+
See [`DeferredExecutor.reject()`](#deferredexecutorreject)
|
| 182 |
+
|
| 183 |
+
### `deferredPromise.rejectionReason`
|
| 184 |
+
|
| 185 |
+
See [`DeferredExecutor.rejectionReason`](#deferredexecutorrejectionreason)
|
| 186 |
+
|
| 187 |
+
---
|
| 188 |
+
|
| 189 |
+
## Mentions
|
| 190 |
+
|
| 191 |
+
- [Jonas Kuske](https://github.com/jonaskuske) for the phenomenal work around improving Promise-compliance.
|
node_modules/@open-draft/deferred-promise/build/index.d.mts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//#region src/create-deferred-executor.d.ts
|
| 2 |
+
type PromiseState = 'pending' | 'fulfilled' | 'rejected';
|
| 3 |
+
type Executor<Value> = ConstructorParameters<typeof Promise<Value>>[0];
|
| 4 |
+
type ResolveFunction<Value> = Parameters<Executor<Value>>[0];
|
| 5 |
+
type RejectFunction<Reason> = Parameters<Executor<Reason>>[1];
|
| 6 |
+
type DeferredPromiseExecutor<Input = never, Output = Input> = {
|
| 7 |
+
(resolve?: ResolveFunction<Input>, reject?: RejectFunction<any>): void;
|
| 8 |
+
resolve: ResolveFunction<Input>;
|
| 9 |
+
reject: RejectFunction<any>;
|
| 10 |
+
result?: Output;
|
| 11 |
+
state: PromiseState;
|
| 12 |
+
rejectionReason?: unknown;
|
| 13 |
+
};
|
| 14 |
+
declare function createDeferredExecutor<Input = never, Output = Input>(): DeferredPromiseExecutor<Input, Output>;
|
| 15 |
+
//#endregion
|
| 16 |
+
//#region src/deferred-promise.d.ts
|
| 17 |
+
declare class DeferredPromise<Input, Output = Input> extends Promise<Input> {
|
| 18 |
+
#private;
|
| 19 |
+
resolve: ResolveFunction<Output>;
|
| 20 |
+
reject: RejectFunction<Output>;
|
| 21 |
+
constructor(executor?: Executor<Input> | null);
|
| 22 |
+
get state(): PromiseState;
|
| 23 |
+
get rejectionReason(): unknown;
|
| 24 |
+
then<ThenResult = Input, CatchResult = never>(onFulfilled?: (value: Input) => ThenResult | PromiseLike<ThenResult>, onRejected?: (reason: any) => CatchResult | PromiseLike<CatchResult>): DeferredPromise<ThenResult | CatchResult, Output>;
|
| 25 |
+
catch<CatchResult = never>(onRejected?: (reason: any) => CatchResult | PromiseLike<CatchResult>): DeferredPromise<Input | CatchResult, Output>;
|
| 26 |
+
finally(onfinally?: () => void | Promise<any>): DeferredPromise<Input, Output>;
|
| 27 |
+
}
|
| 28 |
+
//#endregion
|
| 29 |
+
export { DeferredPromise, type DeferredPromiseExecutor, type Executor, type PromiseState, type RejectFunction, type ResolveFunction, createDeferredExecutor };
|
| 30 |
+
//# sourceMappingURL=index.d.mts.map
|
node_modules/@open-draft/deferred-promise/build/index.mjs
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
//#region src/create-deferred-executor.ts
|
| 2 |
+
function createDeferredExecutor() {
|
| 3 |
+
const executor = ((resolve, reject) => {
|
| 4 |
+
executor.state = "pending";
|
| 5 |
+
executor.resolve = (data) => {
|
| 6 |
+
if (executor.state !== "pending") return;
|
| 7 |
+
executor.result = data;
|
| 8 |
+
const onFulfilled = (value) => {
|
| 9 |
+
executor.state = "fulfilled";
|
| 10 |
+
return value;
|
| 11 |
+
};
|
| 12 |
+
return resolve(data instanceof Promise ? data : Promise.resolve(data).then(onFulfilled));
|
| 13 |
+
};
|
| 14 |
+
executor.reject = (reason) => {
|
| 15 |
+
if (executor.state !== "pending") return;
|
| 16 |
+
queueMicrotask(() => {
|
| 17 |
+
executor.state = "rejected";
|
| 18 |
+
});
|
| 19 |
+
return reject(executor.rejectionReason = reason);
|
| 20 |
+
};
|
| 21 |
+
});
|
| 22 |
+
return executor;
|
| 23 |
+
}
|
| 24 |
+
//#endregion
|
| 25 |
+
//#region src/deferred-promise.ts
|
| 26 |
+
var DeferredPromise = class extends Promise {
|
| 27 |
+
#executor;
|
| 28 |
+
resolve;
|
| 29 |
+
reject;
|
| 30 |
+
constructor(executor = null) {
|
| 31 |
+
const deferredExecutor = createDeferredExecutor();
|
| 32 |
+
super((originalResolve, originalReject) => {
|
| 33 |
+
deferredExecutor(originalResolve, originalReject);
|
| 34 |
+
executor?.(deferredExecutor.resolve, deferredExecutor.reject);
|
| 35 |
+
});
|
| 36 |
+
this.#executor = deferredExecutor;
|
| 37 |
+
this.resolve = this.#executor.resolve;
|
| 38 |
+
this.reject = this.#executor.reject;
|
| 39 |
+
}
|
| 40 |
+
get state() {
|
| 41 |
+
return this.#executor.state;
|
| 42 |
+
}
|
| 43 |
+
get rejectionReason() {
|
| 44 |
+
return this.#executor.rejectionReason;
|
| 45 |
+
}
|
| 46 |
+
then(onFulfilled, onRejected) {
|
| 47 |
+
return this.#decorate(super.then(onFulfilled, onRejected));
|
| 48 |
+
}
|
| 49 |
+
catch(onRejected) {
|
| 50 |
+
return this.#decorate(super.catch(onRejected));
|
| 51 |
+
}
|
| 52 |
+
finally(onfinally) {
|
| 53 |
+
return this.#decorate(super.finally(onfinally));
|
| 54 |
+
}
|
| 55 |
+
#decorate(promise) {
|
| 56 |
+
return Object.defineProperties(promise, {
|
| 57 |
+
resolve: {
|
| 58 |
+
configurable: true,
|
| 59 |
+
value: this.resolve
|
| 60 |
+
},
|
| 61 |
+
reject: {
|
| 62 |
+
configurable: true,
|
| 63 |
+
value: this.reject
|
| 64 |
+
}
|
| 65 |
+
});
|
| 66 |
+
}
|
| 67 |
+
};
|
| 68 |
+
//#endregion
|
| 69 |
+
export { DeferredPromise, createDeferredExecutor };
|
| 70 |
+
|
| 71 |
+
//# sourceMappingURL=index.mjs.map
|
node_modules/@open-draft/deferred-promise/build/index.mjs.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"version":3,"file":"index.mjs","names":["#executor","#decorate"],"sources":["../src/create-deferred-executor.ts","../src/deferred-promise.ts"],"sourcesContent":["export type PromiseState = 'pending' | 'fulfilled' | 'rejected'\n\nexport type Executor<Value> = ConstructorParameters<typeof Promise<Value>>[0]\nexport type ResolveFunction<Value> = Parameters<Executor<Value>>[0]\nexport type RejectFunction<Reason> = Parameters<Executor<Reason>>[1]\n\nexport type DeferredPromiseExecutor<Input = never, Output = Input> = {\n (resolve?: ResolveFunction<Input>, reject?: RejectFunction<any>): void\n\n resolve: ResolveFunction<Input>\n reject: RejectFunction<any>\n result?: Output\n state: PromiseState\n rejectionReason?: unknown\n}\nexport function createDeferredExecutor<\n Input = never,\n Output = Input\n>(): DeferredPromiseExecutor<Input, Output> {\n const executor = <DeferredPromiseExecutor<Input, Output>>((\n resolve,\n reject\n ) => {\n executor.state = 'pending'\n\n executor.resolve = (data) => {\n if (executor.state !== 'pending') {\n return\n }\n\n executor.result = data as Output\n\n const onFulfilled = <Value>(value: Value) => {\n executor.state = 'fulfilled'\n return value\n }\n\n return resolve(\n data instanceof Promise ? data : Promise.resolve(data).then(onFulfilled)\n )\n }\n\n executor.reject = (reason) => {\n if (executor.state !== 'pending') {\n return\n }\n\n queueMicrotask(() => {\n executor.state = 'rejected'\n })\n\n return reject((executor.rejectionReason = reason))\n }\n })\n\n return executor\n}\n","import {\n type Executor,\n type RejectFunction,\n type ResolveFunction,\n type DeferredPromiseExecutor,\n createDeferredExecutor,\n} from './create-deferred-executor'\n\nexport class DeferredPromise<Input, Output = Input> extends Promise<Input> {\n #executor: DeferredPromiseExecutor\n\n public resolve: ResolveFunction<Output>\n public reject: RejectFunction<Output>\n\n constructor(executor: Executor<Input> | null = null) {\n const deferredExecutor = createDeferredExecutor()\n super((originalResolve, originalReject) => {\n deferredExecutor(originalResolve, originalReject)\n executor?.(deferredExecutor.resolve, deferredExecutor.reject)\n })\n\n this.#executor = deferredExecutor\n this.resolve = this.#executor.resolve\n this.reject = this.#executor.reject\n }\n\n public get state() {\n return this.#executor.state\n }\n\n public get rejectionReason() {\n return this.#executor.rejectionReason\n }\n\n public then<ThenResult = Input, CatchResult = never>(\n onFulfilled?: (value: Input) => ThenResult | PromiseLike<ThenResult>,\n onRejected?: (reason: any) => CatchResult | PromiseLike<CatchResult>,\n ) {\n return this.#decorate(super.then(onFulfilled, onRejected))\n }\n\n public catch<CatchResult = never>(\n onRejected?: (reason: any) => CatchResult | PromiseLike<CatchResult>,\n ) {\n return this.#decorate(super.catch(onRejected))\n }\n\n public finally(onfinally?: () => void | Promise<any>) {\n return this.#decorate(super.finally(onfinally))\n }\n\n #decorate<ChildInput>(\n promise: Promise<ChildInput>,\n ): DeferredPromise<ChildInput, Output> {\n return Object.defineProperties(promise, {\n resolve: { configurable: true, value: this.resolve },\n reject: { configurable: true, value: this.reject },\n }) as DeferredPromise<ChildInput, Output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open-draft/deferred-promise/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"type": "module",
|
| 3 |
+
"name": "@open-draft/deferred-promise",
|
| 4 |
+
"version": "3.0.0",
|
| 5 |
+
"description": "A Promise-compatible abstraction that defers resolving/rejecting promises to another closure.",
|
| 6 |
+
"exports": {
|
| 7 |
+
".": "./build/index.mjs"
|
| 8 |
+
},
|
| 9 |
+
"scripts": {
|
| 10 |
+
"start": "tsdown -w",
|
| 11 |
+
"lint": "publint",
|
| 12 |
+
"test": "vitest",
|
| 13 |
+
"test:compliance": "export NODE_OPTIONS=--import=tsx || set NODE_OPTIONS=--import=tsx&& npx -y promises-aplus-tests ./test/aplus-tests-adapter.ts",
|
| 14 |
+
"build": "tsdown",
|
| 15 |
+
"release": "release publish"
|
| 16 |
+
},
|
| 17 |
+
"files": [
|
| 18 |
+
"./build"
|
| 19 |
+
],
|
| 20 |
+
"keywords": [
|
| 21 |
+
"promise",
|
| 22 |
+
"defer",
|
| 23 |
+
"deferred",
|
| 24 |
+
"resolve",
|
| 25 |
+
"reject",
|
| 26 |
+
"executor"
|
| 27 |
+
],
|
| 28 |
+
"author": "Artem Zakharchenko",
|
| 29 |
+
"license": "MIT",
|
| 30 |
+
"repository": {
|
| 31 |
+
"url": "https://github.com/open-draft/deferred-promise"
|
| 32 |
+
},
|
| 33 |
+
"devDependencies": {
|
| 34 |
+
"@ossjs/release": "^0.10.1",
|
| 35 |
+
"publint": "^0.3.18",
|
| 36 |
+
"tsdown": "^0.21.4",
|
| 37 |
+
"tsx": "^4.21.0",
|
| 38 |
+
"typescript": "^5.9.3",
|
| 39 |
+
"vitest": "^4.1.1"
|
| 40 |
+
}
|
| 41 |
+
}
|
node_modules/@open-draft/logger/LICENSE
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
MIT License
|
| 2 |
+
|
| 3 |
+
Copyright (c) 2023—present Artem Zakharchenko
|
| 4 |
+
|
| 5 |
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
| 6 |
+
|
| 7 |
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
| 8 |
+
|
| 9 |
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
node_modules/@open-draft/logger/README.md
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Logger
|
| 2 |
+
|
| 3 |
+
Environment-agnostic, ESM-friendly logger for simple needs.
|
| 4 |
+
|
| 5 |
+
## Why does this exist?
|
| 6 |
+
|
| 7 |
+
I've been using `debug` for quite some time but wanted to migrate my projects to better ESM support. Alas, `debug` doesn't ship as ESM so I went and wrote this little logger just for my needs. You will likely see it printing useful data in Mock Service Worker and beyond.
|
| 8 |
+
|
| 9 |
+
## Installation
|
| 10 |
+
|
| 11 |
+
```sh
|
| 12 |
+
npm install @open-draft/logger
|
| 13 |
+
```
|
| 14 |
+
|
| 15 |
+
## Usage
|
| 16 |
+
|
| 17 |
+
This package has the same API for both browser and Node.js and can run in those environments out of the box.
|
| 18 |
+
|
| 19 |
+
```js
|
| 20 |
+
// app.js
|
| 21 |
+
import { Logger } from '@open-draft/logger'
|
| 22 |
+
|
| 23 |
+
const logger = new Logger('parser')
|
| 24 |
+
|
| 25 |
+
logger.info('starting parsing...')
|
| 26 |
+
logger.warning('found legacy document format')
|
| 27 |
+
logger.success('parsed 120 documents!')
|
| 28 |
+
```
|
| 29 |
+
|
| 30 |
+
Logging is disabled by default. To enable logging, provide the `DEBUG` environment variable:
|
| 31 |
+
|
| 32 |
+
```sh
|
| 33 |
+
DEBUG=1 node ./app.js
|
| 34 |
+
```
|
| 35 |
+
|
| 36 |
+
> You can also use `true` instead of `1`. You can also use a specific logger's name to enable [logger filtering](#logger-filtering).
|
| 37 |
+
|
| 38 |
+
## API
|
| 39 |
+
|
| 40 |
+
- Class: `Logger`
|
| 41 |
+
- [`new Logger(name)`](#new-loggername)
|
| 42 |
+
- [`logger.debug(message, ...positionals)`](#loggerdebugmessage-positionals)
|
| 43 |
+
- [`logger.info(message, ...positionals)`](#loggerinfomessage-positionals)
|
| 44 |
+
- [`logger.success(message, ...positionals)`](#loggersuccessmessage-positionals)
|
| 45 |
+
- [`logger.warning(message, ...positionals)`](#loggerwarningmessage-positionals)
|
| 46 |
+
- [`logger.error(message, ...positionals)`](#loggererrormessage-positionals)
|
| 47 |
+
- [`logger.extend(name)`](#loggerextendprefix)
|
| 48 |
+
- [`logger.only(callback)`](#loggeronlycallback)
|
| 49 |
+
|
| 50 |
+
### `new Logger(name)`
|
| 51 |
+
|
| 52 |
+
- `name` `string` the name of the logger.
|
| 53 |
+
|
| 54 |
+
Creates a new instance of the logger. Each message printed by the logger will be prefixed with the given `name`. You can have multiple loggers with different names for different areas of your system.
|
| 55 |
+
|
| 56 |
+
```js
|
| 57 |
+
const logger = new Logger('parser')
|
| 58 |
+
```
|
| 59 |
+
|
| 60 |
+
> You can nest loggers via [`logger.extend()`](#loggerextendprefix).
|
| 61 |
+
|
| 62 |
+
### `logger.debug(message, ...positionals)`
|
| 63 |
+
|
| 64 |
+
- `message` `string`
|
| 65 |
+
- `positionals` `unknown[]`
|
| 66 |
+
|
| 67 |
+
Prints a debug message.
|
| 68 |
+
|
| 69 |
+
```js
|
| 70 |
+
logger.debug('no duplicates found, skipping...')
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
```
|
| 74 |
+
12:34:56:789 [parser] no duplicates found, skipping...
|
| 75 |
+
```
|
| 76 |
+
|
| 77 |
+
### `logger.info(message, ...positionals)`
|
| 78 |
+
|
| 79 |
+
- `message` `string`
|
| 80 |
+
- `positionals` `unknown[]`
|
| 81 |
+
|
| 82 |
+
Prints an info message.
|
| 83 |
+
|
| 84 |
+
```js
|
| 85 |
+
logger.info('new parse request')
|
| 86 |
+
```
|
| 87 |
+
|
| 88 |
+
```
|
| 89 |
+
12:34:56:789 [parser] new parse request
|
| 90 |
+
```
|
| 91 |
+
|
| 92 |
+
### `logger.success(message, ...positionals)`
|
| 93 |
+
|
| 94 |
+
- `message` `string`
|
| 95 |
+
- `positionals` `unknown[]`
|
| 96 |
+
|
| 97 |
+
Prints a success message.
|
| 98 |
+
|
| 99 |
+
```js
|
| 100 |
+
logger.success('prased 123 documents!')
|
| 101 |
+
```
|
| 102 |
+
|
| 103 |
+
```
|
| 104 |
+
12:34:56:789 ✔ [parser] prased 123 documents!
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
### `logger.warning(message, ...positionals)`
|
| 108 |
+
|
| 109 |
+
- `message` `string`
|
| 110 |
+
- `positionals` `unknown[]`
|
| 111 |
+
|
| 112 |
+
Prints a warning. In Node.js, prints it to `process.stderr`.
|
| 113 |
+
|
| 114 |
+
```js
|
| 115 |
+
logger.warning('found legacy document format')
|
| 116 |
+
```
|
| 117 |
+
|
| 118 |
+
```
|
| 119 |
+
12:34:56:789 ⚠ [parser] found legacy document format
|
| 120 |
+
```
|
| 121 |
+
|
| 122 |
+
### `logger.error(message, ...positionals)`
|
| 123 |
+
|
| 124 |
+
- `message` `string`
|
| 125 |
+
- `positionals` `unknown[]`
|
| 126 |
+
|
| 127 |
+
Prints an error. In Node.js, prints it to `process.stderr`.
|
| 128 |
+
|
| 129 |
+
```js
|
| 130 |
+
logger.error('failed to parse document')
|
| 131 |
+
```
|
| 132 |
+
|
| 133 |
+
```
|
| 134 |
+
12:34:56:789 ✖ [parser] failed to parse document
|
| 135 |
+
```
|
| 136 |
+
|
| 137 |
+
### `logger.extend(prefix)`
|
| 138 |
+
|
| 139 |
+
- `prefix` `string` Additional prefix to append to the logger's name.
|
| 140 |
+
|
| 141 |
+
Creates a new logger out of the current one.
|
| 142 |
+
|
| 143 |
+
```js
|
| 144 |
+
const logger = new Logger('parser')
|
| 145 |
+
|
| 146 |
+
function parseRequest(request) {
|
| 147 |
+
const requestLogger = logger.extend(`${request.method} ${request.url}`)
|
| 148 |
+
requestLogger.info('start parsing...')
|
| 149 |
+
}
|
| 150 |
+
```
|
| 151 |
+
|
| 152 |
+
```
|
| 153 |
+
12:34:56:789 [parser] [GET https://example.com] start parsing...
|
| 154 |
+
```
|
| 155 |
+
|
| 156 |
+
### `logger.only(callback)`
|
| 157 |
+
|
| 158 |
+
Executes a given callback only when the logging is activated. Useful for computing additional information for logs.
|
| 159 |
+
|
| 160 |
+
```js
|
| 161 |
+
logger.only(() => {
|
| 162 |
+
const documentSize = getSizeBytes(document)
|
| 163 |
+
logger.debug(`document size: ${documentSize}`)
|
| 164 |
+
})
|
| 165 |
+
```
|
| 166 |
+
|
| 167 |
+
> You can nest `logger.*` methods in the callback to `logger.only()`.
|
| 168 |
+
|
| 169 |
+
## Log levels
|
| 170 |
+
|
| 171 |
+
You can specify the log levels to print using the `LOG_LEVEL` environment variable.
|
| 172 |
+
|
| 173 |
+
There are the following log levels:
|
| 174 |
+
|
| 175 |
+
- `debug`
|
| 176 |
+
- `info`
|
| 177 |
+
- `success`
|
| 178 |
+
- `warning`
|
| 179 |
+
- `error`
|
| 180 |
+
|
| 181 |
+
> Providing no log level will print all the messages.
|
| 182 |
+
|
| 183 |
+
Here's an example of how to print only warnings:
|
| 184 |
+
|
| 185 |
+
```js
|
| 186 |
+
// app.js
|
| 187 |
+
import { Logger } from '@open-draft/logger'
|
| 188 |
+
|
| 189 |
+
const logger = new Logger('parser')
|
| 190 |
+
|
| 191 |
+
logger.info('some info')
|
| 192 |
+
logger.warning('some warning')
|
| 193 |
+
logger.error('some error')
|
| 194 |
+
```
|
| 195 |
+
|
| 196 |
+
```js
|
| 197 |
+
LOG_LEVEL=warning node ./app.js
|
| 198 |
+
```
|
| 199 |
+
|
| 200 |
+
```
|
| 201 |
+
12:34:56:789 ⚠ [parser] some warning
|
| 202 |
+
```
|
| 203 |
+
|
| 204 |
+
## Logger filtering
|
| 205 |
+
|
| 206 |
+
You can only print a specific logger by providing its name as the `DEBUG` environment variable.
|
| 207 |
+
|
| 208 |
+
```js
|
| 209 |
+
// app.js
|
| 210 |
+
import { Logger } from '@open-draft/logger'
|
| 211 |
+
|
| 212 |
+
const appLogger = new Logger('app')
|
| 213 |
+
const parserLogger = new Logger('parser')
|
| 214 |
+
|
| 215 |
+
appLogger.info('starting app...')
|
| 216 |
+
parserLogger.info('creating a new parser...')
|
| 217 |
+
```
|
| 218 |
+
|
| 219 |
+
```sh
|
| 220 |
+
DEBUG=app node ./app.js
|
| 221 |
+
```
|
| 222 |
+
|
| 223 |
+
```
|
| 224 |
+
12:34:56:789 [app] starting app...
|
| 225 |
+
```
|
node_modules/@open-draft/logger/lib/index.d.ts
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
type ColorFunction = (text: string) => void;
|
| 2 |
+
declare function yellow(text: string): string;
|
| 3 |
+
declare function blue(text: string): string;
|
| 4 |
+
declare function gray(text: string): string;
|
| 5 |
+
declare function red(text: string): string;
|
| 6 |
+
declare function green(text: string): string;
|
| 7 |
+
|
| 8 |
+
type colors_ColorFunction = ColorFunction;
|
| 9 |
+
declare const colors_blue: typeof blue;
|
| 10 |
+
declare const colors_gray: typeof gray;
|
| 11 |
+
declare const colors_green: typeof green;
|
| 12 |
+
declare const colors_red: typeof red;
|
| 13 |
+
declare const colors_yellow: typeof yellow;
|
| 14 |
+
declare namespace colors {
|
| 15 |
+
export {
|
| 16 |
+
colors_ColorFunction as ColorFunction,
|
| 17 |
+
colors_blue as blue,
|
| 18 |
+
colors_gray as gray,
|
| 19 |
+
colors_green as green,
|
| 20 |
+
colors_red as red,
|
| 21 |
+
colors_yellow as yellow,
|
| 22 |
+
};
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
type LogLevel = 'debug' | 'info' | 'success' | 'warning' | 'error';
|
| 26 |
+
type LogColors = keyof typeof colors;
|
| 27 |
+
interface LogEntry {
|
| 28 |
+
timestamp: Date;
|
| 29 |
+
level: LogLevel;
|
| 30 |
+
message: any;
|
| 31 |
+
}
|
| 32 |
+
declare class Logger {
|
| 33 |
+
private readonly name;
|
| 34 |
+
private prefix;
|
| 35 |
+
constructor(name: string);
|
| 36 |
+
extend(domain: string): Logger;
|
| 37 |
+
/**
|
| 38 |
+
* Print a debug message.
|
| 39 |
+
* @example
|
| 40 |
+
* logger.debug('no duplicates found, creating a document...')
|
| 41 |
+
*/
|
| 42 |
+
debug(message: any, ...positionals: Array<unknown>): void;
|
| 43 |
+
/**
|
| 44 |
+
* Print an info message.
|
| 45 |
+
* @example
|
| 46 |
+
* logger.info('start parsing...')
|
| 47 |
+
*/
|
| 48 |
+
info(message: any, ...positionals: Array<unknown>): (message: any, ...positionals: Array<unknown>) => void;
|
| 49 |
+
/**
|
| 50 |
+
* Print a success message.
|
| 51 |
+
* @example
|
| 52 |
+
* logger.success('successfully created document')
|
| 53 |
+
*/
|
| 54 |
+
success(message: any, ...positionals: Array<unknown>): void;
|
| 55 |
+
/**
|
| 56 |
+
* Print a warning.
|
| 57 |
+
* @example
|
| 58 |
+
* logger.warning('found legacy document format')
|
| 59 |
+
*/
|
| 60 |
+
warning(message: any, ...positionals: Array<unknown>): void;
|
| 61 |
+
/**
|
| 62 |
+
* Print an error message.
|
| 63 |
+
* @example
|
| 64 |
+
* logger.error('something went wrong')
|
| 65 |
+
*/
|
| 66 |
+
error(message: any, ...positionals: Array<unknown>): void;
|
| 67 |
+
/**
|
| 68 |
+
* Execute the given callback only when the logging is enabled.
|
| 69 |
+
* This is skipped in its entirety and has no runtime cost otherwise.
|
| 70 |
+
* This executes regardless of the log level.
|
| 71 |
+
* @example
|
| 72 |
+
* logger.only(() => {
|
| 73 |
+
* logger.info('additional info')
|
| 74 |
+
* })
|
| 75 |
+
*/
|
| 76 |
+
only(callback: () => void): void;
|
| 77 |
+
private createEntry;
|
| 78 |
+
private logEntry;
|
| 79 |
+
private formatTimestamp;
|
| 80 |
+
private getWriter;
|
| 81 |
+
}
|
| 82 |
+
|
| 83 |
+
export { LogColors, LogEntry, LogLevel, Logger };
|
node_modules/@open-draft/logger/lib/index.js
ADDED
|
@@ -0,0 +1,295 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
var __defProp = Object.defineProperty;
|
| 2 |
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
| 3 |
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
| 4 |
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
| 5 |
+
var __export = (target, all) => {
|
| 6 |
+
for (var name in all)
|
| 7 |
+
__defProp(target, name, { get: all[name], enumerable: true });
|
| 8 |
+
};
|
| 9 |
+
var __copyProps = (to, from, except, desc) => {
|
| 10 |
+
if (from && typeof from === "object" || typeof from === "function") {
|
| 11 |
+
for (let key of __getOwnPropNames(from))
|
| 12 |
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
| 13 |
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
| 14 |
+
}
|
| 15 |
+
return to;
|
| 16 |
+
};
|
| 17 |
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
| 18 |
+
|
| 19 |
+
// src/index.ts
|
| 20 |
+
var src_exports = {};
|
| 21 |
+
__export(src_exports, {
|
| 22 |
+
Logger: () => Logger
|
| 23 |
+
});
|
| 24 |
+
module.exports = __toCommonJS(src_exports);
|
| 25 |
+
var import_is_node_process = require("is-node-process");
|
| 26 |
+
var import_outvariant = require("outvariant");
|
| 27 |
+
|
| 28 |
+
// src/colors.ts
|
| 29 |
+
var colors_exports = {};
|
| 30 |
+
__export(colors_exports, {
|
| 31 |
+
blue: () => blue,
|
| 32 |
+
gray: () => gray,
|
| 33 |
+
green: () => green,
|
| 34 |
+
red: () => red,
|
| 35 |
+
yellow: () => yellow
|
| 36 |
+
});
|
| 37 |
+
function yellow(text) {
|
| 38 |
+
return `\x1B[33m${text}\x1B[0m`;
|
| 39 |
+
}
|
| 40 |
+
function blue(text) {
|
| 41 |
+
return `\x1B[34m${text}\x1B[0m`;
|
| 42 |
+
}
|
| 43 |
+
function gray(text) {
|
| 44 |
+
return `\x1B[90m${text}\x1B[0m`;
|
| 45 |
+
}
|
| 46 |
+
function red(text) {
|
| 47 |
+
return `\x1B[31m${text}\x1B[0m`;
|
| 48 |
+
}
|
| 49 |
+
function green(text) {
|
| 50 |
+
return `\x1B[32m${text}\x1B[0m`;
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
// src/index.ts
|
| 54 |
+
var IS_NODE = (0, import_is_node_process.isNodeProcess)();
|
| 55 |
+
var Logger = class {
|
| 56 |
+
constructor(name) {
|
| 57 |
+
this.name = name;
|
| 58 |
+
this.prefix = `[${this.name}]`;
|
| 59 |
+
const LOGGER_NAME = getVariable("DEBUG");
|
| 60 |
+
const LOGGER_LEVEL = getVariable("LOG_LEVEL");
|
| 61 |
+
const isLoggingEnabled = LOGGER_NAME === "1" || LOGGER_NAME === "true" || typeof LOGGER_NAME !== "undefined" && this.name.startsWith(LOGGER_NAME);
|
| 62 |
+
if (isLoggingEnabled) {
|
| 63 |
+
this.debug = isDefinedAndNotEquals(LOGGER_LEVEL, "debug") ? noop : this.debug;
|
| 64 |
+
this.info = isDefinedAndNotEquals(LOGGER_LEVEL, "info") ? noop : this.info;
|
| 65 |
+
this.success = isDefinedAndNotEquals(LOGGER_LEVEL, "success") ? noop : this.success;
|
| 66 |
+
this.warning = isDefinedAndNotEquals(LOGGER_LEVEL, "warning") ? noop : this.warning;
|
| 67 |
+
this.error = isDefinedAndNotEquals(LOGGER_LEVEL, "error") ? noop : this.error;
|
| 68 |
+
} else {
|
| 69 |
+
this.info = noop;
|
| 70 |
+
this.success = noop;
|
| 71 |
+
this.warning = noop;
|
| 72 |
+
this.error = noop;
|
| 73 |
+
this.only = noop;
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
prefix;
|
| 77 |
+
extend(domain) {
|
| 78 |
+
return new Logger(`${this.name}:${domain}`);
|
| 79 |
+
}
|
| 80 |
+
/**
|
| 81 |
+
* Print a debug message.
|
| 82 |
+
* @example
|
| 83 |
+
* logger.debug('no duplicates found, creating a document...')
|
| 84 |
+
*/
|
| 85 |
+
debug(message, ...positionals) {
|
| 86 |
+
this.logEntry({
|
| 87 |
+
level: "debug",
|
| 88 |
+
message: gray(message),
|
| 89 |
+
positionals,
|
| 90 |
+
prefix: this.prefix,
|
| 91 |
+
colors: {
|
| 92 |
+
prefix: "gray"
|
| 93 |
+
}
|
| 94 |
+
});
|
| 95 |
+
}
|
| 96 |
+
/**
|
| 97 |
+
* Print an info message.
|
| 98 |
+
* @example
|
| 99 |
+
* logger.info('start parsing...')
|
| 100 |
+
*/
|
| 101 |
+
info(message, ...positionals) {
|
| 102 |
+
this.logEntry({
|
| 103 |
+
level: "info",
|
| 104 |
+
message,
|
| 105 |
+
positionals,
|
| 106 |
+
prefix: this.prefix,
|
| 107 |
+
colors: {
|
| 108 |
+
prefix: "blue"
|
| 109 |
+
}
|
| 110 |
+
});
|
| 111 |
+
const performance2 = new PerformanceEntry();
|
| 112 |
+
return (message2, ...positionals2) => {
|
| 113 |
+
performance2.measure();
|
| 114 |
+
this.logEntry({
|
| 115 |
+
level: "info",
|
| 116 |
+
message: `${message2} ${gray(`${performance2.deltaTime}ms`)}`,
|
| 117 |
+
positionals: positionals2,
|
| 118 |
+
prefix: this.prefix,
|
| 119 |
+
colors: {
|
| 120 |
+
prefix: "blue"
|
| 121 |
+
}
|
| 122 |
+
});
|
| 123 |
+
};
|
| 124 |
+
}
|
| 125 |
+
/**
|
| 126 |
+
* Print a success message.
|
| 127 |
+
* @example
|
| 128 |
+
* logger.success('successfully created document')
|
| 129 |
+
*/
|
| 130 |
+
success(message, ...positionals) {
|
| 131 |
+
this.logEntry({
|
| 132 |
+
level: "info",
|
| 133 |
+
message,
|
| 134 |
+
positionals,
|
| 135 |
+
prefix: `\u2714 ${this.prefix}`,
|
| 136 |
+
colors: {
|
| 137 |
+
timestamp: "green",
|
| 138 |
+
prefix: "green"
|
| 139 |
+
}
|
| 140 |
+
});
|
| 141 |
+
}
|
| 142 |
+
/**
|
| 143 |
+
* Print a warning.
|
| 144 |
+
* @example
|
| 145 |
+
* logger.warning('found legacy document format')
|
| 146 |
+
*/
|
| 147 |
+
warning(message, ...positionals) {
|
| 148 |
+
this.logEntry({
|
| 149 |
+
level: "warning",
|
| 150 |
+
message,
|
| 151 |
+
positionals,
|
| 152 |
+
prefix: `\u26A0 ${this.prefix}`,
|
| 153 |
+
colors: {
|
| 154 |
+
timestamp: "yellow",
|
| 155 |
+
prefix: "yellow"
|
| 156 |
+
}
|
| 157 |
+
});
|
| 158 |
+
}
|
| 159 |
+
/**
|
| 160 |
+
* Print an error message.
|
| 161 |
+
* @example
|
| 162 |
+
* logger.error('something went wrong')
|
| 163 |
+
*/
|
| 164 |
+
error(message, ...positionals) {
|
| 165 |
+
this.logEntry({
|
| 166 |
+
level: "error",
|
| 167 |
+
message,
|
| 168 |
+
positionals,
|
| 169 |
+
prefix: `\u2716 ${this.prefix}`,
|
| 170 |
+
colors: {
|
| 171 |
+
timestamp: "red",
|
| 172 |
+
prefix: "red"
|
| 173 |
+
}
|
| 174 |
+
});
|
| 175 |
+
}
|
| 176 |
+
/**
|
| 177 |
+
* Execute the given callback only when the logging is enabled.
|
| 178 |
+
* This is skipped in its entirety and has no runtime cost otherwise.
|
| 179 |
+
* This executes regardless of the log level.
|
| 180 |
+
* @example
|
| 181 |
+
* logger.only(() => {
|
| 182 |
+
* logger.info('additional info')
|
| 183 |
+
* })
|
| 184 |
+
*/
|
| 185 |
+
only(callback) {
|
| 186 |
+
callback();
|
| 187 |
+
}
|
| 188 |
+
createEntry(level, message) {
|
| 189 |
+
return {
|
| 190 |
+
timestamp: /* @__PURE__ */ new Date(),
|
| 191 |
+
level,
|
| 192 |
+
message
|
| 193 |
+
};
|
| 194 |
+
}
|
| 195 |
+
logEntry(args) {
|
| 196 |
+
const {
|
| 197 |
+
level,
|
| 198 |
+
message,
|
| 199 |
+
prefix,
|
| 200 |
+
colors: customColors,
|
| 201 |
+
positionals = []
|
| 202 |
+
} = args;
|
| 203 |
+
const entry = this.createEntry(level, message);
|
| 204 |
+
const timestampColor = customColors?.timestamp || "gray";
|
| 205 |
+
const prefixColor = customColors?.prefix || "gray";
|
| 206 |
+
const colorize = {
|
| 207 |
+
timestamp: colors_exports[timestampColor],
|
| 208 |
+
prefix: colors_exports[prefixColor]
|
| 209 |
+
};
|
| 210 |
+
const write = this.getWriter(level);
|
| 211 |
+
write(
|
| 212 |
+
[colorize.timestamp(this.formatTimestamp(entry.timestamp))].concat(prefix != null ? colorize.prefix(prefix) : []).concat(serializeInput(message)).join(" "),
|
| 213 |
+
...positionals.map(serializeInput)
|
| 214 |
+
);
|
| 215 |
+
}
|
| 216 |
+
formatTimestamp(timestamp) {
|
| 217 |
+
return `${timestamp.toLocaleTimeString(
|
| 218 |
+
"en-GB"
|
| 219 |
+
)}:${timestamp.getMilliseconds()}`;
|
| 220 |
+
}
|
| 221 |
+
getWriter(level) {
|
| 222 |
+
switch (level) {
|
| 223 |
+
case "debug":
|
| 224 |
+
case "success":
|
| 225 |
+
case "info": {
|
| 226 |
+
return log;
|
| 227 |
+
}
|
| 228 |
+
case "warning": {
|
| 229 |
+
return warn;
|
| 230 |
+
}
|
| 231 |
+
case "error": {
|
| 232 |
+
return error;
|
| 233 |
+
}
|
| 234 |
+
}
|
| 235 |
+
}
|
| 236 |
+
};
|
| 237 |
+
var PerformanceEntry = class {
|
| 238 |
+
startTime;
|
| 239 |
+
endTime;
|
| 240 |
+
deltaTime;
|
| 241 |
+
constructor() {
|
| 242 |
+
this.startTime = performance.now();
|
| 243 |
+
}
|
| 244 |
+
measure() {
|
| 245 |
+
this.endTime = performance.now();
|
| 246 |
+
const deltaTime = this.endTime - this.startTime;
|
| 247 |
+
this.deltaTime = deltaTime.toFixed(2);
|
| 248 |
+
}
|
| 249 |
+
};
|
| 250 |
+
var noop = () => void 0;
|
| 251 |
+
function log(message, ...positionals) {
|
| 252 |
+
if (IS_NODE) {
|
| 253 |
+
process.stdout.write((0, import_outvariant.format)(message, ...positionals) + "\n");
|
| 254 |
+
return;
|
| 255 |
+
}
|
| 256 |
+
console.log(message, ...positionals);
|
| 257 |
+
}
|
| 258 |
+
function warn(message, ...positionals) {
|
| 259 |
+
if (IS_NODE) {
|
| 260 |
+
process.stderr.write((0, import_outvariant.format)(message, ...positionals) + "\n");
|
| 261 |
+
return;
|
| 262 |
+
}
|
| 263 |
+
console.warn(message, ...positionals);
|
| 264 |
+
}
|
| 265 |
+
function error(message, ...positionals) {
|
| 266 |
+
if (IS_NODE) {
|
| 267 |
+
process.stderr.write((0, import_outvariant.format)(message, ...positionals) + "\n");
|
| 268 |
+
return;
|
| 269 |
+
}
|
| 270 |
+
console.error(message, ...positionals);
|
| 271 |
+
}
|
| 272 |
+
function getVariable(variableName) {
|
| 273 |
+
if (IS_NODE) {
|
| 274 |
+
return process.env[variableName];
|
| 275 |
+
}
|
| 276 |
+
return globalThis[variableName]?.toString();
|
| 277 |
+
}
|
| 278 |
+
function isDefinedAndNotEquals(value, expected) {
|
| 279 |
+
return value !== void 0 && value !== expected;
|
| 280 |
+
}
|
| 281 |
+
function serializeInput(message) {
|
| 282 |
+
if (typeof message === "undefined") {
|
| 283 |
+
return "undefined";
|
| 284 |
+
}
|
| 285 |
+
if (message === null) {
|
| 286 |
+
return "null";
|
| 287 |
+
}
|
| 288 |
+
if (typeof message === "string") {
|
| 289 |
+
return message;
|
| 290 |
+
}
|
| 291 |
+
if (typeof message === "object") {
|
| 292 |
+
return JSON.stringify(message);
|
| 293 |
+
}
|
| 294 |
+
return message.toString();
|
| 295 |
+
}
|
node_modules/@open-draft/logger/lib/index.mjs
ADDED
|
@@ -0,0 +1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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
var __defProp = Object.defineProperty;
|
| 2 |
+
var __export = (target, all) => {
|
| 3 |
+
for (var name in all)
|
| 4 |
+
__defProp(target, name, { get: all[name], enumerable: true });
|
| 5 |
+
};
|
| 6 |
+
|
| 7 |
+
// src/index.ts
|
| 8 |
+
import { isNodeProcess } from "is-node-process";
|
| 9 |
+
import { format } from "outvariant";
|
| 10 |
+
|
| 11 |
+
// src/colors.ts
|
| 12 |
+
var colors_exports = {};
|
| 13 |
+
__export(colors_exports, {
|
| 14 |
+
blue: () => blue,
|
| 15 |
+
gray: () => gray,
|
| 16 |
+
green: () => green,
|
| 17 |
+
red: () => red,
|
| 18 |
+
yellow: () => yellow
|
| 19 |
+
});
|
| 20 |
+
function yellow(text) {
|
| 21 |
+
return `\x1B[33m${text}\x1B[0m`;
|
| 22 |
+
}
|
| 23 |
+
function blue(text) {
|
| 24 |
+
return `\x1B[34m${text}\x1B[0m`;
|
| 25 |
+
}
|
| 26 |
+
function gray(text) {
|
| 27 |
+
return `\x1B[90m${text}\x1B[0m`;
|
| 28 |
+
}
|
| 29 |
+
function red(text) {
|
| 30 |
+
return `\x1B[31m${text}\x1B[0m`;
|
| 31 |
+
}
|
| 32 |
+
function green(text) {
|
| 33 |
+
return `\x1B[32m${text}\x1B[0m`;
|
| 34 |
+
}
|
| 35 |
+
|
| 36 |
+
// src/index.ts
|
| 37 |
+
var IS_NODE = isNodeProcess();
|
| 38 |
+
var Logger = class {
|
| 39 |
+
constructor(name) {
|
| 40 |
+
this.name = name;
|
| 41 |
+
this.prefix = `[${this.name}]`;
|
| 42 |
+
const LOGGER_NAME = getVariable("DEBUG");
|
| 43 |
+
const LOGGER_LEVEL = getVariable("LOG_LEVEL");
|
| 44 |
+
const isLoggingEnabled = LOGGER_NAME === "1" || LOGGER_NAME === "true" || typeof LOGGER_NAME !== "undefined" && this.name.startsWith(LOGGER_NAME);
|
| 45 |
+
if (isLoggingEnabled) {
|
| 46 |
+
this.debug = isDefinedAndNotEquals(LOGGER_LEVEL, "debug") ? noop : this.debug;
|
| 47 |
+
this.info = isDefinedAndNotEquals(LOGGER_LEVEL, "info") ? noop : this.info;
|
| 48 |
+
this.success = isDefinedAndNotEquals(LOGGER_LEVEL, "success") ? noop : this.success;
|
| 49 |
+
this.warning = isDefinedAndNotEquals(LOGGER_LEVEL, "warning") ? noop : this.warning;
|
| 50 |
+
this.error = isDefinedAndNotEquals(LOGGER_LEVEL, "error") ? noop : this.error;
|
| 51 |
+
} else {
|
| 52 |
+
this.info = noop;
|
| 53 |
+
this.success = noop;
|
| 54 |
+
this.warning = noop;
|
| 55 |
+
this.error = noop;
|
| 56 |
+
this.only = noop;
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
prefix;
|
| 60 |
+
extend(domain) {
|
| 61 |
+
return new Logger(`${this.name}:${domain}`);
|
| 62 |
+
}
|
| 63 |
+
/**
|
| 64 |
+
* Print a debug message.
|
| 65 |
+
* @example
|
| 66 |
+
* logger.debug('no duplicates found, creating a document...')
|
| 67 |
+
*/
|
| 68 |
+
debug(message, ...positionals) {
|
| 69 |
+
this.logEntry({
|
| 70 |
+
level: "debug",
|
| 71 |
+
message: gray(message),
|
| 72 |
+
positionals,
|
| 73 |
+
prefix: this.prefix,
|
| 74 |
+
colors: {
|
| 75 |
+
prefix: "gray"
|
| 76 |
+
}
|
| 77 |
+
});
|
| 78 |
+
}
|
| 79 |
+
/**
|
| 80 |
+
* Print an info message.
|
| 81 |
+
* @example
|
| 82 |
+
* logger.info('start parsing...')
|
| 83 |
+
*/
|
| 84 |
+
info(message, ...positionals) {
|
| 85 |
+
this.logEntry({
|
| 86 |
+
level: "info",
|
| 87 |
+
message,
|
| 88 |
+
positionals,
|
| 89 |
+
prefix: this.prefix,
|
| 90 |
+
colors: {
|
| 91 |
+
prefix: "blue"
|
| 92 |
+
}
|
| 93 |
+
});
|
| 94 |
+
const performance2 = new PerformanceEntry();
|
| 95 |
+
return (message2, ...positionals2) => {
|
| 96 |
+
performance2.measure();
|
| 97 |
+
this.logEntry({
|
| 98 |
+
level: "info",
|
| 99 |
+
message: `${message2} ${gray(`${performance2.deltaTime}ms`)}`,
|
| 100 |
+
positionals: positionals2,
|
| 101 |
+
prefix: this.prefix,
|
| 102 |
+
colors: {
|
| 103 |
+
prefix: "blue"
|
| 104 |
+
}
|
| 105 |
+
});
|
| 106 |
+
};
|
| 107 |
+
}
|
| 108 |
+
/**
|
| 109 |
+
* Print a success message.
|
| 110 |
+
* @example
|
| 111 |
+
* logger.success('successfully created document')
|
| 112 |
+
*/
|
| 113 |
+
success(message, ...positionals) {
|
| 114 |
+
this.logEntry({
|
| 115 |
+
level: "info",
|
| 116 |
+
message,
|
| 117 |
+
positionals,
|
| 118 |
+
prefix: `\u2714 ${this.prefix}`,
|
| 119 |
+
colors: {
|
| 120 |
+
timestamp: "green",
|
| 121 |
+
prefix: "green"
|
| 122 |
+
}
|
| 123 |
+
});
|
| 124 |
+
}
|
| 125 |
+
/**
|
| 126 |
+
* Print a warning.
|
| 127 |
+
* @example
|
| 128 |
+
* logger.warning('found legacy document format')
|
| 129 |
+
*/
|
| 130 |
+
warning(message, ...positionals) {
|
| 131 |
+
this.logEntry({
|
| 132 |
+
level: "warning",
|
| 133 |
+
message,
|
| 134 |
+
positionals,
|
| 135 |
+
prefix: `\u26A0 ${this.prefix}`,
|
| 136 |
+
colors: {
|
| 137 |
+
timestamp: "yellow",
|
| 138 |
+
prefix: "yellow"
|
| 139 |
+
}
|
| 140 |
+
});
|
| 141 |
+
}
|
| 142 |
+
/**
|
| 143 |
+
* Print an error message.
|
| 144 |
+
* @example
|
| 145 |
+
* logger.error('something went wrong')
|
| 146 |
+
*/
|
| 147 |
+
error(message, ...positionals) {
|
| 148 |
+
this.logEntry({
|
| 149 |
+
level: "error",
|
| 150 |
+
message,
|
| 151 |
+
positionals,
|
| 152 |
+
prefix: `\u2716 ${this.prefix}`,
|
| 153 |
+
colors: {
|
| 154 |
+
timestamp: "red",
|
| 155 |
+
prefix: "red"
|
| 156 |
+
}
|
| 157 |
+
});
|
| 158 |
+
}
|
| 159 |
+
/**
|
| 160 |
+
* Execute the given callback only when the logging is enabled.
|
| 161 |
+
* This is skipped in its entirety and has no runtime cost otherwise.
|
| 162 |
+
* This executes regardless of the log level.
|
| 163 |
+
* @example
|
| 164 |
+
* logger.only(() => {
|
| 165 |
+
* logger.info('additional info')
|
| 166 |
+
* })
|
| 167 |
+
*/
|
| 168 |
+
only(callback) {
|
| 169 |
+
callback();
|
| 170 |
+
}
|
| 171 |
+
createEntry(level, message) {
|
| 172 |
+
return {
|
| 173 |
+
timestamp: /* @__PURE__ */ new Date(),
|
| 174 |
+
level,
|
| 175 |
+
message
|
| 176 |
+
};
|
| 177 |
+
}
|
| 178 |
+
logEntry(args) {
|
| 179 |
+
const {
|
| 180 |
+
level,
|
| 181 |
+
message,
|
| 182 |
+
prefix,
|
| 183 |
+
colors: customColors,
|
| 184 |
+
positionals = []
|
| 185 |
+
} = args;
|
| 186 |
+
const entry = this.createEntry(level, message);
|
| 187 |
+
const timestampColor = customColors?.timestamp || "gray";
|
| 188 |
+
const prefixColor = customColors?.prefix || "gray";
|
| 189 |
+
const colorize = {
|
| 190 |
+
timestamp: colors_exports[timestampColor],
|
| 191 |
+
prefix: colors_exports[prefixColor]
|
| 192 |
+
};
|
| 193 |
+
const write = this.getWriter(level);
|
| 194 |
+
write(
|
| 195 |
+
[colorize.timestamp(this.formatTimestamp(entry.timestamp))].concat(prefix != null ? colorize.prefix(prefix) : []).concat(serializeInput(message)).join(" "),
|
| 196 |
+
...positionals.map(serializeInput)
|
| 197 |
+
);
|
| 198 |
+
}
|
| 199 |
+
formatTimestamp(timestamp) {
|
| 200 |
+
return `${timestamp.toLocaleTimeString(
|
| 201 |
+
"en-GB"
|
| 202 |
+
)}:${timestamp.getMilliseconds()}`;
|
| 203 |
+
}
|
| 204 |
+
getWriter(level) {
|
| 205 |
+
switch (level) {
|
| 206 |
+
case "debug":
|
| 207 |
+
case "success":
|
| 208 |
+
case "info": {
|
| 209 |
+
return log;
|
| 210 |
+
}
|
| 211 |
+
case "warning": {
|
| 212 |
+
return warn;
|
| 213 |
+
}
|
| 214 |
+
case "error": {
|
| 215 |
+
return error;
|
| 216 |
+
}
|
| 217 |
+
}
|
| 218 |
+
}
|
| 219 |
+
};
|
| 220 |
+
var PerformanceEntry = class {
|
| 221 |
+
startTime;
|
| 222 |
+
endTime;
|
| 223 |
+
deltaTime;
|
| 224 |
+
constructor() {
|
| 225 |
+
this.startTime = performance.now();
|
| 226 |
+
}
|
| 227 |
+
measure() {
|
| 228 |
+
this.endTime = performance.now();
|
| 229 |
+
const deltaTime = this.endTime - this.startTime;
|
| 230 |
+
this.deltaTime = deltaTime.toFixed(2);
|
| 231 |
+
}
|
| 232 |
+
};
|
| 233 |
+
var noop = () => void 0;
|
| 234 |
+
function log(message, ...positionals) {
|
| 235 |
+
if (IS_NODE) {
|
| 236 |
+
process.stdout.write(format(message, ...positionals) + "\n");
|
| 237 |
+
return;
|
| 238 |
+
}
|
| 239 |
+
console.log(message, ...positionals);
|
| 240 |
+
}
|
| 241 |
+
function warn(message, ...positionals) {
|
| 242 |
+
if (IS_NODE) {
|
| 243 |
+
process.stderr.write(format(message, ...positionals) + "\n");
|
| 244 |
+
return;
|
| 245 |
+
}
|
| 246 |
+
console.warn(message, ...positionals);
|
| 247 |
+
}
|
| 248 |
+
function error(message, ...positionals) {
|
| 249 |
+
if (IS_NODE) {
|
| 250 |
+
process.stderr.write(format(message, ...positionals) + "\n");
|
| 251 |
+
return;
|
| 252 |
+
}
|
| 253 |
+
console.error(message, ...positionals);
|
| 254 |
+
}
|
| 255 |
+
function getVariable(variableName) {
|
| 256 |
+
if (IS_NODE) {
|
| 257 |
+
return process.env[variableName];
|
| 258 |
+
}
|
| 259 |
+
return globalThis[variableName]?.toString();
|
| 260 |
+
}
|
| 261 |
+
function isDefinedAndNotEquals(value, expected) {
|
| 262 |
+
return value !== void 0 && value !== expected;
|
| 263 |
+
}
|
| 264 |
+
function serializeInput(message) {
|
| 265 |
+
if (typeof message === "undefined") {
|
| 266 |
+
return "undefined";
|
| 267 |
+
}
|
| 268 |
+
if (message === null) {
|
| 269 |
+
return "null";
|
| 270 |
+
}
|
| 271 |
+
if (typeof message === "string") {
|
| 272 |
+
return message;
|
| 273 |
+
}
|
| 274 |
+
if (typeof message === "object") {
|
| 275 |
+
return JSON.stringify(message);
|
| 276 |
+
}
|
| 277 |
+
return message.toString();
|
| 278 |
+
}
|
| 279 |
+
export {
|
| 280 |
+
Logger
|
| 281 |
+
};
|