@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/esm/_md.js ADDED
@@ -0,0 +1,201 @@
1
+ /**
2
+ * Internal Merkle-Damgard hash utils.
3
+ * @module
4
+ */
5
+ import { abytes, aexists, aoutput, clean, createView, } 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 function Chi(a, b, c) {
22
+ return (a & b) ^ (~a & c);
23
+ }
24
+ /**
25
+ * Shared 32-bit majority primitive reused by SHA-256 and SHA-1.
26
+ * Returns bits shared by at least two inputs.
27
+ * @param a - first input word
28
+ * @param b - second input word
29
+ * @param c - third input word
30
+ * @returns Mixed 32-bit word.
31
+ * @example
32
+ * Combine three words with the shared 32-bit majority primitive.
33
+ * ```ts
34
+ * Maj(0xffffffff, 0x12345678, 0x87654321);
35
+ * ```
36
+ */
37
+ export function Maj(a, b, c) {
38
+ return (a & b) ^ (a & c) ^ (b & c);
39
+ }
40
+ /**
41
+ * Merkle-Damgard hash construction base class.
42
+ * Could be used to create MD5, RIPEMD, SHA1, SHA2.
43
+ * Accepts only byte-aligned `Uint8Array` input, even when the underlying spec describes bit
44
+ * strings with partial-byte tails.
45
+ * @param blockLen - internal block size in bytes
46
+ * @param outputLen - digest size in bytes
47
+ * @param padOffset - trailing length field size in bytes
48
+ * @param isLE - whether length and state words are encoded in little-endian
49
+ * @example
50
+ * Use a concrete subclass to get the shared Merkle-Damgard update/digest flow.
51
+ * ```ts
52
+ * import { _SHA1 } from '@decryption/hashes/legacy.js';
53
+ * const hash = new _SHA1();
54
+ * hash.update(new Uint8Array([97, 98, 99]));
55
+ * hash.digest();
56
+ * ```
57
+ */
58
+ export class HashMD {
59
+ blockLen;
60
+ outputLen;
61
+ canXOF = false;
62
+ padOffset;
63
+ isLE;
64
+ // For partial updates less than block size
65
+ buffer;
66
+ view;
67
+ finished = false;
68
+ length = 0;
69
+ pos = 0;
70
+ destroyed = false;
71
+ constructor(blockLen, outputLen, padOffset, isLE) {
72
+ this.blockLen = blockLen;
73
+ this.outputLen = outputLen;
74
+ this.padOffset = padOffset;
75
+ this.isLE = isLE;
76
+ this.buffer = new Uint8Array(blockLen);
77
+ this.view = createView(this.buffer);
78
+ }
79
+ update(data) {
80
+ aexists(this);
81
+ abytes(data);
82
+ const { view, buffer, blockLen } = this;
83
+ const len = data.length;
84
+ for (let pos = 0; pos < len;) {
85
+ const take = Math.min(blockLen - this.pos, len - pos);
86
+ // Fast path only when there is no buffered partial block: `take === blockLen` implies
87
+ // `this.pos === 0`, so we can process full blocks directly from the input view.
88
+ if (take === blockLen) {
89
+ const dataView = createView(data);
90
+ for (; blockLen <= len - pos; pos += blockLen)
91
+ this.process(dataView, pos);
92
+ continue;
93
+ }
94
+ buffer.set(data.subarray(pos, pos + take), this.pos);
95
+ this.pos += take;
96
+ pos += take;
97
+ if (this.pos === blockLen) {
98
+ this.process(view, 0);
99
+ this.pos = 0;
100
+ }
101
+ }
102
+ this.length += data.length;
103
+ this.roundClean();
104
+ return this;
105
+ }
106
+ digestInto(out) {
107
+ aexists(this);
108
+ aoutput(out, this);
109
+ this.finished = true;
110
+ // Padding
111
+ // We can avoid allocation of buffer for padding completely if it
112
+ // was previously not allocated here. But it won't change performance.
113
+ const { buffer, view, blockLen, isLE } = this;
114
+ let { pos } = this;
115
+ // append the bit '1' to the message
116
+ buffer[pos++] = 0b10000000;
117
+ clean(this.buffer.subarray(pos));
118
+ // we have less than padOffset left in buffer, so we cannot put length in
119
+ // current block, need process it and pad again
120
+ if (this.padOffset > blockLen - pos) {
121
+ this.process(view, 0);
122
+ pos = 0;
123
+ }
124
+ // Pad until full block byte with zeros
125
+ for (let i = pos; i < blockLen; i++)
126
+ buffer[i] = 0;
127
+ // `padOffset` reserves the whole length field. For SHA-384/512 the high 64 bits stay zero from
128
+ // the padding fill above, and JS will overflow before user input can make that half non-zero.
129
+ // So we only need to write the low 64 bits here.
130
+ view.setBigUint64(blockLen - 8, BigInt(this.length * 8), isLE);
131
+ this.process(view, 0);
132
+ const oview = createView(out);
133
+ const len = this.outputLen;
134
+ // NOTE: we do division by 4 later, which must be fused in single op with modulo by JIT
135
+ if (len % 4)
136
+ throw new Error('_sha2: outputLen must be aligned to 32bit');
137
+ const outLen = len / 4;
138
+ const state = this.get();
139
+ if (outLen > state.length)
140
+ throw new Error('_sha2: outputLen bigger than state');
141
+ for (let i = 0; i < outLen; i++)
142
+ oview.setUint32(4 * i, state[i], isLE);
143
+ }
144
+ digest() {
145
+ const { buffer, outputLen } = this;
146
+ this.digestInto(buffer);
147
+ // Copy before destroy(): subclasses wipe `buffer` during cleanup, but `digest()` must return
148
+ // fresh bytes to the caller.
149
+ const res = buffer.slice(0, outputLen);
150
+ this.destroy();
151
+ return res;
152
+ }
153
+ _cloneInto(to) {
154
+ to ||= new this.constructor();
155
+ to.set(...this.get());
156
+ const { blockLen, buffer, length, finished, destroyed, pos } = this;
157
+ to.destroyed = destroyed;
158
+ to.finished = finished;
159
+ to.length = length;
160
+ to.pos = pos;
161
+ // Only partial-block bytes need copying: when `length % blockLen === 0`, `pos === 0` and
162
+ // later `update()` / `digestInto()` overwrite `to.buffer` from the start before reading it.
163
+ if (length % blockLen)
164
+ to.buffer.set(buffer);
165
+ return to;
166
+ }
167
+ clone() {
168
+ return this._cloneInto();
169
+ }
170
+ }
171
+ /**
172
+ * Initial SHA-2 state: fractional parts of square roots of first 16 primes 2..53.
173
+ * Check out `test/misc/sha2-gen-iv.js` for recomputation guide.
174
+ */
175
+ /** Initial SHA256 state from RFC 6234 §6.1: the first 32 bits of the fractional parts of the
176
+ * square roots of the first eight prime numbers. Exported as a shared table; callers must treat
177
+ * it as read-only because constructors copy words from it by index. */
178
+ export const SHA256_IV = /* @__PURE__ */ Uint32Array.from([
179
+ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19,
180
+ ]);
181
+ /** Initial SHA224 state `H(0)` from RFC 6234 §6.1. Exported as a shared table; callers must
182
+ * treat it as read-only because constructors copy words from it by index. */
183
+ export const SHA224_IV = /* @__PURE__ */ Uint32Array.from([
184
+ 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4,
185
+ ]);
186
+ /** Initial SHA384 state from RFC 6234 §6.3: eight RFC 64-bit `H(0)` words stored as sixteen
187
+ * big-endian 32-bit halves. Derived from the fractional parts of the square roots of the ninth
188
+ * through sixteenth prime numbers. Exported as a shared table; callers must treat it as read-only
189
+ * because constructors copy halves from it by index. */
190
+ export const SHA384_IV = /* @__PURE__ */ Uint32Array.from([
191
+ 0xcbbb9d5d, 0xc1059ed8, 0x629a292a, 0x367cd507, 0x9159015a, 0x3070dd17, 0x152fecd8, 0xf70e5939,
192
+ 0x67332667, 0xffc00b31, 0x8eb44a87, 0x68581511, 0xdb0c2e0d, 0x64f98fa7, 0x47b5481d, 0xbefa4fa4,
193
+ ]);
194
+ /** Initial SHA512 state from RFC 6234 §6.3: eight RFC 64-bit `H(0)` words stored as sixteen
195
+ * big-endian 32-bit halves. Derived from the fractional parts of the square roots of the first
196
+ * eight prime numbers. Exported as a shared table; callers must treat it as read-only because
197
+ * constructors copy halves from it by index. */
198
+ export const SHA512_IV = /* @__PURE__ */ Uint32Array.from([
199
+ 0x6a09e667, 0xf3bcc908, 0xbb67ae85, 0x84caa73b, 0x3c6ef372, 0xfe94f82b, 0xa54ff53a, 0x5f1d36f1,
200
+ 0x510e527f, 0xade682d1, 0x9b05688c, 0x2b3e6c1f, 0x1f83d9ab, 0xfb41bd6b, 0x5be0cd19, 0x137e2179,
201
+ ]);
package/esm/_u64.js ADDED
@@ -0,0 +1,83 @@
1
+ const U32_MASK64 = /* @__PURE__ */ BigInt(2 ** 32 - 1);
2
+ const _32n = /* @__PURE__ */ BigInt(32);
3
+ // Split bigint into two 32-bit halves. With `le=true`, returned fields become `{ h: low, l: high
4
+ // }` to match little-endian word order rather than the property names.
5
+ function fromBig(n, le = false) {
6
+ if (le)
7
+ return { h: Number(n & U32_MASK64), l: Number((n >> _32n) & U32_MASK64) };
8
+ return { h: Number((n >> _32n) & U32_MASK64) | 0, l: Number(n & U32_MASK64) | 0 };
9
+ }
10
+ // Split bigint list into `[highWords, lowWords]` when `le=false`; with `le=true`, the first array
11
+ // holds the low halves because `fromBig(...)` swaps the semantic meaning of `h` and `l`.
12
+ function split(lst, le = false) {
13
+ const len = lst.length;
14
+ let Ah = new Uint32Array(len);
15
+ let Al = new Uint32Array(len);
16
+ for (let i = 0; i < len; i++) {
17
+ const { h, l } = fromBig(lst[i], le);
18
+ [Ah[i], Al[i]] = [h, l];
19
+ }
20
+ return [Ah, Al];
21
+ }
22
+ // Combine explicit `(high, low)` 32-bit halves into a bigint; `>>> 0` normalizes signed JS
23
+ // bitwise results back to uint32 first, and little-endian callers must swap.
24
+ const toBig = (h, l) => (BigInt(h >>> 0) << _32n) | BigInt(l >>> 0);
25
+ // High 32-bit half of a 64-bit logical right shift for `s` in `0..31`.
26
+ const shrSH = (h, _l, s) => h >>> s;
27
+ // Low 32-bit half of a 64-bit logical right shift, valid for `s` in `1..31`.
28
+ const shrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
29
+ // High 32-bit half of a 64-bit right rotate, valid for `s` in `1..31`.
30
+ const rotrSH = (h, l, s) => (h >>> s) | (l << (32 - s));
31
+ // Low 32-bit half of a 64-bit right rotate, valid for `s` in `1..31`.
32
+ const rotrSL = (h, l, s) => (h << (32 - s)) | (l >>> s);
33
+ // High 32-bit half of a 64-bit right rotate, valid for `s` in `33..63`; `32` uses `rotr32*`.
34
+ const rotrBH = (h, l, s) => (h << (64 - s)) | (l >>> (s - 32));
35
+ // Low 32-bit half of a 64-bit right rotate, valid for `s` in `33..63`; `32` uses `rotr32*`.
36
+ const rotrBL = (h, l, s) => (h >>> (s - 32)) | (l << (64 - s));
37
+ // High 32-bit half of a 64-bit right rotate for `s === 32`; this is just the swapped low half.
38
+ const rotr32H = (_h, l) => l;
39
+ // Low 32-bit half of a 64-bit right rotate for `s === 32`; this is just the swapped high half.
40
+ const rotr32L = (h, _l) => h;
41
+ // High 32-bit half of a 64-bit left rotate, valid for `s` in `1..31`.
42
+ const rotlSH = (h, l, s) => (h << s) | (l >>> (32 - s));
43
+ // Low 32-bit half of a 64-bit left rotate, valid for `s` in `1..31`.
44
+ const rotlSL = (h, l, s) => (l << s) | (h >>> (32 - s));
45
+ // High 32-bit half of a 64-bit left rotate, valid for `s` in `33..63`; `32` uses `rotr32*`.
46
+ const rotlBH = (h, l, s) => (l << (s - 32)) | (h >>> (64 - s));
47
+ // Low 32-bit half of a 64-bit left rotate, valid for `s` in `33..63`; `32` uses `rotr32*`.
48
+ const rotlBL = (h, l, s) => (h << (s - 32)) | (l >>> (64 - s));
49
+ // Add two split 64-bit words and return the split `{ h, l }` sum.
50
+ // JS uses 32-bit signed integers for bitwise operations, so we cannot simply shift the carry out
51
+ // of the low sum and instead use division.
52
+ function add(Ah, Al, Bh, Bl) {
53
+ const l = (Al >>> 0) + (Bl >>> 0);
54
+ return { h: (Ah + Bh + ((l / 2 ** 32) | 0)) | 0, l: l | 0 };
55
+ }
56
+ // Addition with more than 2 elements
57
+ // Unmasked low-word accumulator for 3-way addition; pass the raw result into `add3H(...)`.
58
+ const add3L = (Al, Bl, Cl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0);
59
+ // High-word finalize step for 3-way addition; `low` must be the untruncated output of `add3L(...)`.
60
+ const add3H = (low, Ah, Bh, Ch) => (Ah + Bh + Ch + ((low / 2 ** 32) | 0)) | 0;
61
+ // Unmasked low-word accumulator for 4-way addition; pass the raw result into `add4H(...)`.
62
+ const add4L = (Al, Bl, Cl, Dl) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0);
63
+ // High-word finalize step for 4-way addition; `low` must be the untruncated output of `add4L(...)`.
64
+ const add4H = (low, Ah, Bh, Ch, Dh) => (Ah + Bh + Ch + Dh + ((low / 2 ** 32) | 0)) | 0;
65
+ // Unmasked low-word accumulator for 5-way addition; pass the raw result into `add5H(...)`.
66
+ const add5L = (Al, Bl, Cl, Dl, El) => (Al >>> 0) + (Bl >>> 0) + (Cl >>> 0) + (Dl >>> 0) + (El >>> 0);
67
+ // High-word finalize step for 5-way addition; `low` must be the untruncated output of `add5L(...)`.
68
+ const add5H = (low, Ah, Bh, Ch, Dh, Eh) => (Ah + Bh + Ch + Dh + Eh + ((low / 2 ** 32) | 0)) | 0;
69
+ // prettier-ignore
70
+ export { add, add3H, add3L, add4H, add4L, add5H, add5L, fromBig, rotlBH, rotlBL, rotlSH, rotlSL, rotr32H, rotr32L, rotrBH, rotrBL, rotrSH, rotrSL, shrSH, shrSL, split, toBig };
71
+ // Canonical grouped namespace for callers that prefer one object.
72
+ // Named exports stay for direct imports.
73
+ // prettier-ignore
74
+ const u64 = {
75
+ fromBig, split, toBig,
76
+ shrSH, shrSL,
77
+ rotrSH, rotrSL, rotrBH, rotrBL,
78
+ rotr32H, rotr32L,
79
+ rotlSH, rotlSL, rotlBH, rotlBL,
80
+ add, add3L, add3H, add4L, add4H, add5H, add5L,
81
+ };
82
+ // Default export mirrors named `u64` for compatibility with object-style imports.
83
+ export default u64;