@decryption/hashes 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +75 -0
  3. package/_blake.d.ts +20 -0
  4. package/_blake.js +56 -0
  5. package/_md.d.ts +98 -0
  6. package/_md.js +207 -0
  7. package/_u64.d.ts +62 -0
  8. package/_u64.js +106 -0
  9. package/argon2.d.ts +111 -0
  10. package/argon2.js +525 -0
  11. package/blake1.d.ts +155 -0
  12. package/blake1.js +599 -0
  13. package/blake2.d.ts +183 -0
  14. package/blake2.js +540 -0
  15. package/blake3.d.ts +67 -0
  16. package/blake3.js +287 -0
  17. package/eskdf.d.ts +91 -0
  18. package/eskdf.js +223 -0
  19. package/esm/_blake.js +51 -0
  20. package/esm/_md.js +201 -0
  21. package/esm/_u64.js +83 -0
  22. package/esm/argon2.js +516 -0
  23. package/esm/blake1.js +559 -0
  24. package/esm/blake2.js +500 -0
  25. package/esm/blake3.js +283 -0
  26. package/esm/eskdf.js +217 -0
  27. package/esm/hkdf.js +119 -0
  28. package/esm/hmac.js +92 -0
  29. package/esm/index.js +34 -0
  30. package/esm/legacy.js +326 -0
  31. package/esm/pbkdf2.js +134 -0
  32. package/esm/scrypt.js +256 -0
  33. package/esm/sha2.js +457 -0
  34. package/esm/sha3-addons.js +782 -0
  35. package/esm/sha3.js +430 -0
  36. package/esm/utils.js +577 -0
  37. package/esm/webcrypto.js +163 -0
  38. package/hkdf.d.ts +63 -0
  39. package/hkdf.js +125 -0
  40. package/hmac.d.ts +46 -0
  41. package/hmac.js +96 -0
  42. package/index.d.ts +0 -0
  43. package/index.js +34 -0
  44. package/legacy.d.ts +101 -0
  45. package/legacy.js +332 -0
  46. package/package.json +47 -0
  47. package/pbkdf2.d.ts +55 -0
  48. package/pbkdf2.js +138 -0
  49. package/scrypt.d.ts +64 -0
  50. package/scrypt.js +260 -0
  51. package/sha2.d.ts +248 -0
  52. package/sha2.js +499 -0
  53. package/sha3-addons.d.ts +460 -0
  54. package/sha3-addons.js +791 -0
  55. package/sha3.d.ts +198 -0
  56. package/sha3.js +435 -0
  57. package/utils.d.ts +518 -0
  58. package/utils.js +606 -0
  59. package/webcrypto.d.ts +119 -0
  60. package/webcrypto.js +168 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2022 Paul Miller (https://paulmillr.com)
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
13
+ all 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
21
+ THE SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,75 @@
1
+ # @decryption/hashes
2
+
3
+ > **NOTE:** This is a fork of [`@noble/hashes`](https://www.npmjs.com/package/@noble/hashes) (v2.2.0).
4
+ > We dual-publish as both **CJS and ESM** because upstream is ESM-only (`"type": "module"`), which breaks
5
+ > `require()` in Jest, older bundlers and CJS-based Node.js tooling. The source is unchanged from upstream —
6
+ > only the build tooling differs, plus `@noble/*` / `@scure/*` import specifiers are rewritten to their
7
+ > `@decryption/*` forks.
8
+
9
+ Audited & minimal 0-dependency JS implementation of SHA, RIPEMD, BLAKE, HMAC, HKDF, PBKDF2, Scrypt & Argon2. Dual CJS+ESM fork of @noble/hashes.
10
+
11
+ ## Installation
12
+
13
+ ```bash
14
+ npm install @decryption/hashes
15
+ ```
16
+
17
+ ## Usage
18
+
19
+ Import submodules directly — `dist/` is the package root once published, so deep imports work in both CJS and ESM:
20
+
21
+ ```typescript
22
+ import { sha256 } from '@decryption/hashes/sha2';
23
+ import { xchacha20poly1305 } from '@decryption/ciphers/chacha';
24
+ ```
25
+
26
+ Refer to the [upstream documentation](https://github.com/paulmillr/hashes) for the full API.
27
+
28
+ ## Syncing upstream
29
+
30
+ ```bash
31
+ ./scripts/vendor-fork.sh @noble/hashes <version> hashes
32
+ node scripts/fix-imports.js packages/hashes hashes
33
+ pnpm --filter @decryption/hashes build && pnpm --filter @decryption/hashes test
34
+ ```
35
+
36
+ ## License
37
+
38
+ MIT — Copyright (c) 2022 Paul Miller (https://paulmillr.com). See [LICENSE](./LICENSE).
39
+
40
+ ---
41
+
42
+ ## Development
43
+
44
+ ### Setup
45
+
46
+ 1. Clone the repository:
47
+
48
+ ```bash
49
+ git clone https://github.com/constructive-io/decryption.git
50
+ ```
51
+
52
+ 2. Install dependencies:
53
+
54
+ ```bash
55
+ cd decryption
56
+ pnpm install
57
+ pnpm build
58
+ ```
59
+
60
+ 3. Test the package of interest:
61
+
62
+ ```bash
63
+ cd packages/<packagename>
64
+ pnpm test:watch
65
+ ```
66
+
67
+ ## Credits
68
+
69
+ **🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
70
+
71
+ ## Disclaimer
72
+
73
+ AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
74
+
75
+ No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
package/_blake.d.ts ADDED
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Internal helpers for blake hash.
3
+ * @module
4
+ */
5
+ import { type TRet } from './utils';
6
+ /**
7
+ * Internal blake permutation table.
8
+ * Rows `0..9` serve BLAKE2s, rows `0..11` serve BLAKE2b with `10..11 = 0..1`, and Blake1 also
9
+ * reuses the later rows shown below. Blake1 expands rounds `10..15` as `SIGMA[i % 10]`, so rows
10
+ * `10..15` intentionally repeat rows `0..5` for the 14-round (256) and 16-round (512) variants.
11
+ */
12
+ export declare const BSIGMA: TRet<Uint8Array>;
13
+ export type Num4 = {
14
+ a: number;
15
+ b: number;
16
+ c: number;
17
+ d: number;
18
+ };
19
+ export declare function G1s(a: number, b: number, c: number, d: number, x: number): Num4;
20
+ export declare function G2s(a: number, b: number, c: number, d: number, x: number): Num4;
package/_blake.js ADDED
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.BSIGMA = void 0;
4
+ exports.G1s = G1s;
5
+ exports.G2s = G2s;
6
+ /**
7
+ * Internal helpers for blake hash.
8
+ * @module
9
+ */
10
+ const utils_1 = require("./utils");
11
+ /**
12
+ * Internal blake permutation table.
13
+ * Rows `0..9` serve BLAKE2s, rows `0..11` serve BLAKE2b with `10..11 = 0..1`, and Blake1 also
14
+ * reuses the later rows shown below. Blake1 expands rounds `10..15` as `SIGMA[i % 10]`, so rows
15
+ * `10..15` intentionally repeat rows `0..5` for the 14-round (256) and 16-round (512) variants.
16
+ */
17
+ // prettier-ignore
18
+ exports.BSIGMA = Uint8Array.from([
19
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
20
+ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3,
21
+ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4,
22
+ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8,
23
+ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13,
24
+ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9,
25
+ 12, 5, 1, 15, 14, 13, 4, 10, 0, 7, 6, 3, 9, 2, 8, 11,
26
+ 13, 11, 7, 14, 12, 1, 3, 9, 5, 0, 15, 4, 8, 6, 2, 10,
27
+ 6, 15, 14, 9, 11, 3, 0, 8, 12, 2, 13, 7, 1, 4, 10, 5,
28
+ 10, 2, 8, 4, 7, 6, 1, 5, 15, 11, 9, 14, 3, 12, 13, 0,
29
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
30
+ 14, 10, 4, 8, 9, 15, 13, 6, 1, 12, 0, 2, 11, 7, 5, 3,
31
+ // Blake1, unused in others
32
+ 11, 8, 12, 0, 5, 2, 15, 13, 10, 14, 3, 6, 7, 1, 9, 4,
33
+ 7, 9, 3, 1, 13, 12, 11, 14, 2, 6, 5, 10, 4, 0, 15, 8,
34
+ 9, 0, 5, 7, 2, 4, 10, 15, 14, 1, 11, 12, 6, 8, 3, 13,
35
+ 2, 12, 6, 10, 0, 11, 8, 3, 4, 13, 7, 5, 15, 14, 1, 9,
36
+ ]);
37
+ // 32-bit / BLAKE2s first half of G, with the fixed `(16, 12)` rotation pair.
38
+ // Parameter `x` is the RFC 7693 first-half message word, or Blake1's pre-mixed
39
+ // `m[sigma[r][2i]] ^ u[sigma[r][2i+1]]` addend in the 32-bit path.
40
+ function G1s(a, b, c, d, x) {
41
+ a = (a + b + x) | 0;
42
+ d = (0, utils_1.rotr)(d ^ a, 16);
43
+ c = (c + d) | 0;
44
+ b = (0, utils_1.rotr)(b ^ c, 12);
45
+ return { a, b, c, d };
46
+ }
47
+ // 32-bit / BLAKE2s second half of G.
48
+ // Parameter `x` is the RFC 7693 second-half (`y`) message word, or Blake1's pre-mixed
49
+ // `m[sigma[r][2i + 1]] ^ u[sigma[r][2i]]` addend in the 32-bit path.
50
+ function G2s(a, b, c, d, x) {
51
+ a = (a + b + x) | 0;
52
+ d = (0, utils_1.rotr)(d ^ a, 8);
53
+ c = (c + d) | 0;
54
+ b = (0, utils_1.rotr)(b ^ c, 7);
55
+ return { a, b, c, d };
56
+ }
package/_md.d.ts ADDED
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Internal Merkle-Damgard hash utils.
3
+ * @module
4
+ */
5
+ import { type Hash, type TArg, type TRet } from './utils';
6
+ /**
7
+ * Shared 32-bit conditional boolean primitive reused by SHA-256, SHA-1, and MD5 `F`.
8
+ * Returns bits from `b` when `a` is set, otherwise from `c`.
9
+ * The XOR form is equivalent to MD5's `F(X,Y,Z) = XY v not(X)Z` because the masked terms never
10
+ * set the same bit.
11
+ * @param a - selector word
12
+ * @param b - word chosen when selector bit is set
13
+ * @param c - word chosen when selector bit is clear
14
+ * @returns Mixed 32-bit word.
15
+ * @example
16
+ * Combine three words with the shared 32-bit choice primitive.
17
+ * ```ts
18
+ * Chi(0xffffffff, 0x12345678, 0x87654321);
19
+ * ```
20
+ */
21
+ export declare function Chi(a: number, b: number, c: number): number;
22
+ /**
23
+ * Shared 32-bit majority primitive reused by SHA-256 and SHA-1.
24
+ * Returns bits shared by at least two inputs.
25
+ * @param a - first input word
26
+ * @param b - second input word
27
+ * @param c - third input word
28
+ * @returns Mixed 32-bit word.
29
+ * @example
30
+ * Combine three words with the shared 32-bit majority primitive.
31
+ * ```ts
32
+ * Maj(0xffffffff, 0x12345678, 0x87654321);
33
+ * ```
34
+ */
35
+ export declare function Maj(a: number, b: number, c: number): number;
36
+ /**
37
+ * Merkle-Damgard hash construction base class.
38
+ * Could be used to create MD5, RIPEMD, SHA1, SHA2.
39
+ * Accepts only byte-aligned `Uint8Array` input, even when the underlying spec describes bit
40
+ * strings with partial-byte tails.
41
+ * @param blockLen - internal block size in bytes
42
+ * @param outputLen - digest size in bytes
43
+ * @param padOffset - trailing length field size in bytes
44
+ * @param isLE - whether length and state words are encoded in little-endian
45
+ * @example
46
+ * Use a concrete subclass to get the shared Merkle-Damgard update/digest flow.
47
+ * ```ts
48
+ * import { _SHA1 } from '@decryption/hashes/legacy.js';
49
+ * const hash = new _SHA1();
50
+ * hash.update(new Uint8Array([97, 98, 99]));
51
+ * hash.digest();
52
+ * ```
53
+ */
54
+ export declare abstract class HashMD<T extends HashMD<T>> implements Hash<T> {
55
+ protected abstract process(buf: DataView, offset: number): void;
56
+ protected abstract get(): number[];
57
+ protected abstract set(...args: number[]): void;
58
+ abstract destroy(): void;
59
+ protected abstract roundClean(): void;
60
+ readonly blockLen: number;
61
+ readonly outputLen: number;
62
+ readonly canXOF = false;
63
+ readonly padOffset: number;
64
+ readonly isLE: boolean;
65
+ protected buffer: Uint8Array;
66
+ protected view: DataView;
67
+ protected finished: boolean;
68
+ protected length: number;
69
+ protected pos: number;
70
+ protected destroyed: boolean;
71
+ constructor(blockLen: number, outputLen: number, padOffset: number, isLE: boolean);
72
+ update(data: TArg<Uint8Array>): this;
73
+ digestInto(out: TArg<Uint8Array>): void;
74
+ digest(): TRet<Uint8Array>;
75
+ _cloneInto(to?: T): T;
76
+ clone(): T;
77
+ }
78
+ /**
79
+ * Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
80
+ * Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
81
+ */
82
+ /** Initial SHA256 state from RFC 6234 §6.1: the first 32 bits of the fractional parts of the
83
+ * square roots of the first eight prime numbers. Exported as a shared table; callers must treat
84
+ * it as read-only because constructors copy words from it by index. */
85
+ export declare const SHA256_IV: TRet<Uint32Array>;
86
+ /** Initial SHA224 state `H(0)` from RFC 6234 §6.1. Exported as a shared table; callers must
87
+ * treat it as read-only because constructors copy words from it by index. */
88
+ export declare const SHA224_IV: TRet<Uint32Array>;
89
+ /** Initial SHA384 state from RFC 6234 §6.3: eight RFC 64-bit `H(0)` words stored as sixteen
90
+ * big-endian 32-bit halves. Derived from the fractional parts of the square roots of the ninth
91
+ * through sixteenth prime numbers. Exported as a shared table; callers must treat it as read-only
92
+ * because constructors copy halves from it by index. */
93
+ export declare const SHA384_IV: TRet<Uint32Array>;
94
+ /** Initial SHA512 state from RFC 6234 §6.3: eight RFC 64-bit `H(0)` words stored as sixteen
95
+ * big-endian 32-bit halves. Derived from the fractional parts of the square roots of the first
96
+ * eight prime numbers. Exported as a shared table; callers must treat it as read-only because
97
+ * constructors copy halves from it by index. */
98
+ export declare const SHA512_IV: TRet<Uint32Array>;
package/_md.js ADDED
@@ -0,0 +1,207 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SHA512_IV = exports.SHA384_IV = exports.SHA224_IV = exports.SHA256_IV = exports.HashMD = void 0;
4
+ exports.Chi = Chi;
5
+ exports.Maj = Maj;
6
+ /**
7
+ * Internal Merkle-Damgard hash utils.
8
+ * @module
9
+ */
10
+ const utils_1 = require("./utils");
11
+ /**
12
+ * Shared 32-bit conditional boolean primitive reused by SHA-256, SHA-1, and MD5 `F`.
13
+ * Returns bits from `b` when `a` is set, otherwise from `c`.
14
+ * The XOR form is equivalent to MD5's `F(X,Y,Z) = XY v not(X)Z` because the masked terms never
15
+ * set the same bit.
16
+ * @param a - selector word
17
+ * @param b - word chosen when selector bit is set
18
+ * @param c - word chosen when selector bit is clear
19
+ * @returns Mixed 32-bit word.
20
+ * @example
21
+ * Combine three words with the shared 32-bit choice primitive.
22
+ * ```ts
23
+ * Chi(0xffffffff, 0x12345678, 0x87654321);
24
+ * ```
25
+ */
26
+ function Chi(a, b, c) {
27
+ return (a & b) ^ (~a & c);
28
+ }
29
+ /**
30
+ * Shared 32-bit majority primitive reused by SHA-256 and SHA-1.
31
+ * Returns bits shared by at least two inputs.
32
+ * @param a - first input word
33
+ * @param b - second input word
34
+ * @param c - third input word
35
+ * @returns Mixed 32-bit word.
36
+ * @example
37
+ * Combine three words with the shared 32-bit majority primitive.
38
+ * ```ts
39
+ * Maj(0xffffffff, 0x12345678, 0x87654321);
40
+ * ```
41
+ */
42
+ function Maj(a, b, c) {
43
+ return (a & b) ^ (a & c) ^ (b & c);
44
+ }
45
+ /**
46
+ * Merkle-Damgard hash construction base class.
47
+ * Could be used to create MD5, RIPEMD, SHA1, SHA2.
48
+ * Accepts only byte-aligned `Uint8Array` input, even when the underlying spec describes bit
49
+ * strings with partial-byte tails.
50
+ * @param blockLen - internal block size in bytes
51
+ * @param outputLen - digest size in bytes
52
+ * @param padOffset - trailing length field size in bytes
53
+ * @param isLE - whether length and state words are encoded in little-endian
54
+ * @example
55
+ * Use a concrete subclass to get the shared Merkle-Damgard update/digest flow.
56
+ * ```ts
57
+ * import { _SHA1 } from '@decryption/hashes/legacy.js';
58
+ * const hash = new _SHA1();
59
+ * hash.update(new Uint8Array([97, 98, 99]));
60
+ * hash.digest();
61
+ * ```
62
+ */
63
+ class HashMD {
64
+ blockLen;
65
+ outputLen;
66
+ canXOF = false;
67
+ padOffset;
68
+ isLE;
69
+ // For partial updates less than block size
70
+ buffer;
71
+ view;
72
+ finished = false;
73
+ length = 0;
74
+ pos = 0;
75
+ destroyed = false;
76
+ constructor(blockLen, outputLen, padOffset, isLE) {
77
+ this.blockLen = blockLen;
78
+ this.outputLen = outputLen;
79
+ this.padOffset = padOffset;
80
+ this.isLE = isLE;
81
+ this.buffer = new Uint8Array(blockLen);
82
+ this.view = (0, utils_1.createView)(this.buffer);
83
+ }
84
+ update(data) {
85
+ (0, utils_1.aexists)(this);
86
+ (0, utils_1.abytes)(data);
87
+ const { view, buffer, blockLen } = this;
88
+ const len = data.length;
89
+ for (let pos = 0; pos < len;) {
90
+ const take = Math.min(blockLen - this.pos, len - pos);
91
+ // Fast path only when there is no buffered partial block: `take === blockLen` implies
92
+ // `this.pos === 0`, so we can process full blocks directly from the input view.
93
+ if (take === blockLen) {
94
+ const dataView = (0, utils_1.createView)(data);
95
+ for (; blockLen <= len - pos; pos += blockLen)
96
+ this.process(dataView, pos);
97
+ continue;
98
+ }
99
+ buffer.set(data.subarray(pos, pos + take), this.pos);
100
+ this.pos += take;
101
+ pos += take;
102
+ if (this.pos === blockLen) {
103
+ this.process(view, 0);
104
+ this.pos = 0;
105
+ }
106
+ }
107
+ this.length += data.length;
108
+ this.roundClean();
109
+ return this;
110
+ }
111
+ digestInto(out) {
112
+ (0, utils_1.aexists)(this);
113
+ (0, utils_1.aoutput)(out, this);
114
+ this.finished = true;
115
+ // Padding
116
+ // We can avoid allocation of buffer for padding completely if it
117
+ // was previously not allocated here. But it won't change performance.
118
+ const { buffer, view, blockLen, isLE } = this;
119
+ let { pos } = this;
120
+ // append the bit '1' to the message
121
+ buffer[pos++] = 0b10000000;
122
+ (0, utils_1.clean)(this.buffer.subarray(pos));
123
+ // we have less than padOffset left in buffer, so we cannot put length in
124
+ // current block, need process it and pad again
125
+ if (this.padOffset > blockLen - pos) {
126
+ this.process(view, 0);
127
+ pos = 0;
128
+ }
129
+ // Pad until full block byte with zeros
130
+ for (let i = pos; i < blockLen; i++)
131
+ buffer[i] = 0;
132
+ // `padOffset` reserves the whole length field. For SHA-384/512 the high 64 bits stay zero from
133
+ // the padding fill above, and JS will overflow before user input can make that half non-zero.
134
+ // So we only need to write the low 64 bits here.
135
+ view.setBigUint64(blockLen - 8, BigInt(this.length * 8), isLE);
136
+ this.process(view, 0);
137
+ const oview = (0, utils_1.createView)(out);
138
+ const len = this.outputLen;
139
+ // NOTE: we do division by 4 later, which must be fused in single op with modulo by JIT
140
+ if (len % 4)
141
+ throw new Error('_sha2: outputLen must be aligned to 32bit');
142
+ const outLen = len / 4;
143
+ const state = this.get();
144
+ if (outLen > state.length)
145
+ throw new Error('_sha2: outputLen bigger than state');
146
+ for (let i = 0; i < outLen; i++)
147
+ oview.setUint32(4 * i, state[i], isLE);
148
+ }
149
+ digest() {
150
+ const { buffer, outputLen } = this;
151
+ this.digestInto(buffer);
152
+ // Copy before destroy(): subclasses wipe `buffer` during cleanup, but `digest()` must return
153
+ // fresh bytes to the caller.
154
+ const res = buffer.slice(0, outputLen);
155
+ this.destroy();
156
+ return res;
157
+ }
158
+ _cloneInto(to) {
159
+ to ||= new this.constructor();
160
+ to.set(...this.get());
161
+ const { blockLen, buffer, length, finished, destroyed, pos } = this;
162
+ to.destroyed = destroyed;
163
+ to.finished = finished;
164
+ to.length = length;
165
+ to.pos = pos;
166
+ // Only partial-block bytes need copying: when `length % blockLen === 0`, `pos === 0` and
167
+ // later `update()` / `digestInto()` overwrite `to.buffer` from the start before reading it.
168
+ if (length % blockLen)
169
+ to.buffer.set(buffer);
170
+ return to;
171
+ }
172
+ clone() {
173
+ return this._cloneInto();
174
+ }
175
+ }
176
+ exports.HashMD = HashMD;
177
+ /**
178
+ * Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
179
+ * Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
180
+ */
181
+ /** Initial SHA256 state from RFC 6234 §6.1: the first 32 bits of the fractional parts of the
182
+ * square roots of the first eight prime numbers. Exported as a shared table; callers must treat
183
+ * it as read-only because constructors copy words from it by index. */
184
+ exports.SHA256_IV = Uint32Array.from([
185
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
186
+ ]);
187
+ /** Initial SHA224 state `H(0)` from RFC 6234 §6.1. Exported as a shared table; callers must
188
+ * treat it as read-only because constructors copy words from it by index. */
189
+ exports.SHA224_IV = Uint32Array.from([
190
+ 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4,
191
+ ]);
192
+ /** Initial SHA384 state from RFC 6234 §6.3: eight RFC 64-bit `H(0)` words stored as sixteen
193
+ * big-endian 32-bit halves. Derived from the fractional parts of the square roots of the ninth
194
+ * through sixteenth prime numbers. Exported as a shared table; callers must treat it as read-only
195
+ * because constructors copy halves from it by index. */
196
+ exports.SHA384_IV = Uint32Array.from([
197
+ 0xcbbb9d5d, 0xc1059ed8, 0x629a292a, 0x367cd507, 0x9159015a, 0x3070dd17, 0x152fecd8, 0xf70e5939,
198
+ 0x67332667, 0xffc00b31, 0x8eb44a87, 0x68581511, 0xdb0c2e0d, 0x64f98fa7, 0x47b5481d, 0xbefa4fa4,
199
+ ]);
200
+ /** Initial SHA512 state from RFC 6234 §6.3: eight RFC 64-bit `H(0)` words stored as sixteen
201
+ * big-endian 32-bit halves. Derived from the fractional parts of the square roots of the first
202
+ * eight prime numbers. Exported as a shared table; callers must treat it as read-only because
203
+ * constructors copy halves from it by index. */
204
+ exports.SHA512_IV = Uint32Array.from([
205
+ 0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa73b, 0x3c6ef372, 0xfe94f82b, 0xa54ff53a, 0x5f1d36f1,
206
+ 0x510e527f, 0xade682d1, 0x9b05688c, 0x2b3e6c1f, 0x1f83d9ab, 0xfb41bd6b, 0x5be0cd19, 0x137e2179,
207
+ ]);
package/_u64.d.ts ADDED
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Internal helpers for u64.
3
+ * BigUint64Array is too slow as per 2026, so we implement it using
4
+ * Uint32Array.
5
+ * @privateRemarks TODO: re-check {@link https://issues.chromium.org/issues/42212588}
6
+ * @module
7
+ */
8
+ import type { TRet } from './utils';
9
+ declare function fromBig(n: bigint, le?: boolean): {
10
+ h: number;
11
+ l: number;
12
+ };
13
+ declare function split(lst: bigint[], le?: boolean): TRet<Uint32Array[]>;
14
+ declare const toBig: (h: number, l: number) => bigint;
15
+ declare const shrSH: (h: number, _l: number, s: number) => number;
16
+ declare const shrSL: (h: number, l: number, s: number) => number;
17
+ declare const rotrSH: (h: number, l: number, s: number) => number;
18
+ declare const rotrSL: (h: number, l: number, s: number) => number;
19
+ declare const rotrBH: (h: number, l: number, s: number) => number;
20
+ declare const rotrBL: (h: number, l: number, s: number) => number;
21
+ declare const rotr32H: (_h: number, l: number) => number;
22
+ declare const rotr32L: (h: number, _l: number) => number;
23
+ declare const rotlSH: (h: number, l: number, s: number) => number;
24
+ declare const rotlSL: (h: number, l: number, s: number) => number;
25
+ declare const rotlBH: (h: number, l: number, s: number) => number;
26
+ declare const rotlBL: (h: number, l: number, s: number) => number;
27
+ declare function add(Ah: number, Al: number, Bh: number, Bl: number): {
28
+ h: number;
29
+ l: number;
30
+ };
31
+ declare const add3L: (Al: number, Bl: number, Cl: number) => number;
32
+ declare const add3H: (low: number, Ah: number, Bh: number, Ch: number) => number;
33
+ declare const add4L: (Al: number, Bl: number, Cl: number, Dl: number) => number;
34
+ declare const add4H: (low: number, Ah: number, Bh: number, Ch: number, Dh: number) => number;
35
+ declare const add5L: (Al: number, Bl: number, Cl: number, Dl: number, El: number) => number;
36
+ declare const add5H: (low: number, Ah: number, Bh: number, Ch: number, Dh: number, Eh: number) => number;
37
+ export { add, add3H, add3L, add4H, add4L, add5H, add5L, fromBig, rotlBH, rotlBL, rotlSH, rotlSL, rotr32H, rotr32L, rotrBH, rotrBL, rotrSH, rotrSL, shrSH, shrSL, split, toBig };
38
+ declare const u64: {
39
+ fromBig: typeof fromBig;
40
+ split: typeof split;
41
+ toBig: (h: number, l: number) => bigint;
42
+ shrSH: (h: number, _l: number, s: number) => number;
43
+ shrSL: (h: number, l: number, s: number) => number;
44
+ rotrSH: (h: number, l: number, s: number) => number;
45
+ rotrSL: (h: number, l: number, s: number) => number;
46
+ rotrBH: (h: number, l: number, s: number) => number;
47
+ rotrBL: (h: number, l: number, s: number) => number;
48
+ rotr32H: (_h: number, l: number) => number;
49
+ rotr32L: (h: number, _l: number) => number;
50
+ rotlSH: (h: number, l: number, s: number) => number;
51
+ rotlSL: (h: number, l: number, s: number) => number;
52
+ rotlBH: (h: number, l: number, s: number) => number;
53
+ rotlBL: (h: number, l: number, s: number) => number;
54
+ add: typeof add;
55
+ add3L: (Al: number, Bl: number, Cl: number) => number;
56
+ add3H: (low: number, Ah: number, Bh: number, Ch: number) => number;
57
+ add4L: (Al: number, Bl: number, Cl: number, Dl: number) => number;
58
+ add4H: (low: number, Ah: number, Bh: number, Ch: number, Dh: number) => number;
59
+ add5H: (low: number, Ah: number, Bh: number, Ch: number, Dh: number, Eh: number) => number;
60
+ add5L: (Al: number, Bl: number, Cl: number, Dl: number, El: number) => number;
61
+ };
62
+ export default u64;
package/_u64.js ADDED
@@ -0,0 +1,106 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.toBig = exports.shrSL = exports.shrSH = exports.rotrSL = exports.rotrSH = exports.rotrBL = exports.rotrBH = exports.rotr32L = exports.rotr32H = exports.rotlSL = exports.rotlSH = exports.rotlBL = exports.rotlBH = exports.add5L = exports.add5H = exports.add4L = exports.add4H = exports.add3L = exports.add3H = void 0;
4
+ exports.add = add;
5
+ exports.fromBig = fromBig;
6
+ exports.split = split;
7
+ const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
8
+ const _32n = /* @__PURE__ */ BigInt(32);
9
+ // Split bigint into two 32-bit halves. With `le=true`, returned fields become `{ h: low, l: high
10
+ // }` to match little-endian word order rather than the property names.
11
+ function fromBig(n, le = false) {
12
+ if (le)
13
+ return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
14
+ return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
15
+ }
16
+ // Split bigint list into `[highWords, lowWords]` when `le=false`; with `le=true`, the first array
17
+ // holds the low halves because `fromBig(...)` swaps the semantic meaning of `h` and `l`.
18
+ function split(lst, le = false) {
19
+ const len = lst.length;
20
+ let Ah = new Uint32Array(len);
21
+ let Al = new Uint32Array(len);
22
+ for (let i = 0; i < len; i++) {
23
+ const { h, l } = fromBig(lst[i], le);
24
+ [Ah[i], Al[i]] = [h, l];
25
+ }
26
+ return [Ah, Al];
27
+ }
28
+ // Combine explicit `(high, low)` 32-bit halves into a bigint; `>>> 0` normalizes signed JS
29
+ // bitwise results back to uint32 first, and little-endian callers must swap.
30
+ const toBig = (h, l) => (BigInt(h >>> 0) << _32n) | BigInt(l >>> 0);
31
+ exports.toBig = toBig;
32
+ // High 32-bit half of a 64-bit logical right shift for `s` in `0..31`.
33
+ const shrSH = (h, _l, s) => h >>> s;
34
+ exports.shrSH = shrSH;
35
+ // Low 32-bit half of a 64-bit logical right shift, valid for `s` in `1..31`.
36
+ const shrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
37
+ exports.shrSL = shrSL;
38
+ // High 32-bit half of a 64-bit right rotate, valid for `s` in `1..31`.
39
+ const rotrSH = (h, l, s) => (h >>> s) | (l << (32 - s));
40
+ exports.rotrSH = rotrSH;
41
+ // Low 32-bit half of a 64-bit right rotate, valid for `s` in `1..31`.
42
+ const rotrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
43
+ exports.rotrSL = rotrSL;
44
+ // High 32-bit half of a 64-bit right rotate, valid for `s` in `33..63`; `32` uses `rotr32*`.
45
+ const rotrBH = (h, l, s) => (h << (64 - s)) | (l >>> (s - 32));
46
+ exports.rotrBH = rotrBH;
47
+ // Low 32-bit half of a 64-bit right rotate, valid for `s` in `33..63`; `32` uses `rotr32*`.
48
+ const rotrBL = (h, l, s) => (h >>> (s - 32)) | (l << (64 - s));
49
+ exports.rotrBL = rotrBL;
50
+ // High 32-bit half of a 64-bit right rotate for `s === 32`; this is just the swapped low half.
51
+ const rotr32H = (_h, l) => l;
52
+ exports.rotr32H = rotr32H;
53
+ // Low 32-bit half of a 64-bit right rotate for `s === 32`; this is just the swapped high half.
54
+ const rotr32L = (h, _l) => h;
55
+ exports.rotr32L = rotr32L;
56
+ // High 32-bit half of a 64-bit left rotate, valid for `s` in `1..31`.
57
+ const rotlSH = (h, l, s) => (h << s) | (l >>> (32 - s));
58
+ exports.rotlSH = rotlSH;
59
+ // Low 32-bit half of a 64-bit left rotate, valid for `s` in `1..31`.
60
+ const rotlSL = (h, l, s) => (l << s) | (h >>> (32 - s));
61
+ exports.rotlSL = rotlSL;
62
+ // High 32-bit half of a 64-bit left rotate, valid for `s` in `33..63`; `32` uses `rotr32*`.
63
+ const rotlBH = (h, l, s) => (l << (s - 32)) | (h >>> (64 - s));
64
+ exports.rotlBH = rotlBH;
65
+ // Low 32-bit half of a 64-bit left rotate, valid for `s` in `33..63`; `32` uses `rotr32*`.
66
+ const rotlBL = (h, l, s) => (h << (s - 32)) | (l >>> (64 - s));
67
+ exports.rotlBL = rotlBL;
68
+ // Add two split 64-bit words and return the split `{ h, l }` sum.
69
+ // JS uses 32-bit signed integers for bitwise operations, so we cannot simply shift the carry out
70
+ // of the low sum and instead use division.
71
+ function add(Ah, Al, Bh, Bl) {
72
+ const l = (Al >>> 0) + (Bl >>> 0);
73
+ return { h: (Ah + Bh + ((l / 2 ** 32) | 0)) | 0, l: l | 0 };
74
+ }
75
+ // Addition with more than 2 elements
76
+ // Unmasked low-word accumulator for 3-way addition; pass the raw result into `add3H(...)`.
77
+ const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
78
+ exports.add3L = add3L;
79
+ // High-word finalize step for 3-way addition; `low` must be the untruncated output of `add3L(...)`.
80
+ const add3H = (low, Ah, Bh, Ch) => (Ah + Bh + Ch + ((low / 2 ** 32) | 0)) | 0;
81
+ exports.add3H = add3H;
82
+ // Unmasked low-word accumulator for 4-way addition; pass the raw result into `add4H(...)`.
83
+ const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
84
+ exports.add4L = add4L;
85
+ // High-word finalize step for 4-way addition; `low` must be the untruncated output of `add4L(...)`.
86
+ const add4H = (low, Ah, Bh, Ch, Dh) => (Ah + Bh + Ch + Dh + ((low / 2 ** 32) | 0)) | 0;
87
+ exports.add4H = add4H;
88
+ // Unmasked low-word accumulator for 5-way addition; pass the raw result into `add5H(...)`.
89
+ const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
90
+ exports.add5L = add5L;
91
+ // High-word finalize step for 5-way addition; `low` must be the untruncated output of `add5L(...)`.
92
+ const add5H = (low, Ah, Bh, Ch, Dh, Eh) => (Ah + Bh + Ch + Dh + Eh + ((low / 2 ** 32) | 0)) | 0;
93
+ exports.add5H = add5H;
94
+ // Canonical grouped namespace for callers that prefer one object.
95
+ // Named exports stay for direct imports.
96
+ // prettier-ignore
97
+ const u64 = {
98
+ fromBig, split, toBig,
99
+ shrSH, shrSL,
100
+ rotrSH, rotrSL, rotrBH, rotrBL,
101
+ rotr32H, rotr32L,
102
+ rotlSH, rotlSL, rotlBH, rotlBL,
103
+ add, add3L, add3H, add4L, add4H, add5H, add5L,
104
+ };
105
+ // Default export mirrors named `u64` for compatibility with object-style imports.
106
+ exports.default = u64;