@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.
- package/LICENSE +21 -0
- package/README.md +75 -0
- package/_blake.d.ts +20 -0
- package/_blake.js +56 -0
- package/_md.d.ts +98 -0
- package/_md.js +207 -0
- package/_u64.d.ts +62 -0
- package/_u64.js +106 -0
- package/argon2.d.ts +111 -0
- package/argon2.js +525 -0
- package/blake1.d.ts +155 -0
- package/blake1.js +599 -0
- package/blake2.d.ts +183 -0
- package/blake2.js +540 -0
- package/blake3.d.ts +67 -0
- package/blake3.js +287 -0
- package/eskdf.d.ts +91 -0
- package/eskdf.js +223 -0
- package/esm/_blake.js +51 -0
- package/esm/_md.js +201 -0
- package/esm/_u64.js +83 -0
- package/esm/argon2.js +516 -0
- package/esm/blake1.js +559 -0
- package/esm/blake2.js +500 -0
- package/esm/blake3.js +283 -0
- package/esm/eskdf.js +217 -0
- package/esm/hkdf.js +119 -0
- package/esm/hmac.js +92 -0
- package/esm/index.js +34 -0
- package/esm/legacy.js +326 -0
- package/esm/pbkdf2.js +134 -0
- package/esm/scrypt.js +256 -0
- package/esm/sha2.js +457 -0
- package/esm/sha3-addons.js +782 -0
- package/esm/sha3.js +430 -0
- package/esm/utils.js +577 -0
- package/esm/webcrypto.js +163 -0
- package/hkdf.d.ts +63 -0
- package/hkdf.js +125 -0
- package/hmac.d.ts +46 -0
- package/hmac.js +96 -0
- package/index.d.ts +0 -0
- package/index.js +34 -0
- package/legacy.d.ts +101 -0
- package/legacy.js +332 -0
- package/package.json +47 -0
- package/pbkdf2.d.ts +55 -0
- package/pbkdf2.js +138 -0
- package/scrypt.d.ts +64 -0
- package/scrypt.js +260 -0
- package/sha2.d.ts +248 -0
- package/sha2.js +499 -0
- package/sha3-addons.d.ts +460 -0
- package/sha3-addons.js +791 -0
- package/sha3.d.ts +198 -0
- package/sha3.js +435 -0
- package/utils.d.ts +518 -0
- package/utils.js +606 -0
- package/webcrypto.d.ts +119 -0
- package/webcrypto.js +168 -0
package/scrypt.js
ADDED
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.scrypt = scrypt;
|
|
4
|
+
exports.scryptAsync = scryptAsync;
|
|
5
|
+
/**
|
|
6
|
+
* RFC 7914 Scrypt KDF. Can be used to create a key from password and salt.
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
const pbkdf2_1 = require("./pbkdf2");
|
|
10
|
+
const sha2_1 = require("./sha2");
|
|
11
|
+
// prettier-ignore
|
|
12
|
+
const utils_1 = require("./utils");
|
|
13
|
+
// The main Scrypt loop: uses Salsa extensively.
|
|
14
|
+
// Six versions of the function were tried, this is the fastest one.
|
|
15
|
+
// RFC 7914 §3 / §4 step 2 applies Salsa20/8 to one 16-word (64-byte) block
|
|
16
|
+
// after xor'ing two such blocks.
|
|
17
|
+
// The local `y*` snapshot keeps the xor input stable even when `out` aliases `prev` or `input`.
|
|
18
|
+
// prettier-ignore
|
|
19
|
+
function XorAndSalsa(prev, pi, input, ii, out, oi) {
|
|
20
|
+
// Based on https://cr.yp.to/salsa20.html and RFC 7914's Salsa20/8 core.
|
|
21
|
+
// Xor blocks
|
|
22
|
+
let y00 = prev[pi++] ^ input[ii++], y01 = prev[pi++] ^ input[ii++];
|
|
23
|
+
let y02 = prev[pi++] ^ input[ii++], y03 = prev[pi++] ^ input[ii++];
|
|
24
|
+
let y04 = prev[pi++] ^ input[ii++], y05 = prev[pi++] ^ input[ii++];
|
|
25
|
+
let y06 = prev[pi++] ^ input[ii++], y07 = prev[pi++] ^ input[ii++];
|
|
26
|
+
let y08 = prev[pi++] ^ input[ii++], y09 = prev[pi++] ^ input[ii++];
|
|
27
|
+
let y10 = prev[pi++] ^ input[ii++], y11 = prev[pi++] ^ input[ii++];
|
|
28
|
+
let y12 = prev[pi++] ^ input[ii++], y13 = prev[pi++] ^ input[ii++];
|
|
29
|
+
let y14 = prev[pi++] ^ input[ii++], y15 = prev[pi++] ^ input[ii++];
|
|
30
|
+
// Save state to temporary variables (salsa)
|
|
31
|
+
let x00 = y00, x01 = y01, x02 = y02, x03 = y03, x04 = y04, x05 = y05, x06 = y06, x07 = y07, x08 = y08, x09 = y09, x10 = y10, x11 = y11, x12 = y12, x13 = y13, x14 = y14, x15 = y15;
|
|
32
|
+
// Main loop (salsa)
|
|
33
|
+
for (let i = 0; i < 8; i += 2) {
|
|
34
|
+
x04 ^= (0, utils_1.rotl)(x00 + x12 | 0, 7);
|
|
35
|
+
x08 ^= (0, utils_1.rotl)(x04 + x00 | 0, 9);
|
|
36
|
+
x12 ^= (0, utils_1.rotl)(x08 + x04 | 0, 13);
|
|
37
|
+
x00 ^= (0, utils_1.rotl)(x12 + x08 | 0, 18);
|
|
38
|
+
x09 ^= (0, utils_1.rotl)(x05 + x01 | 0, 7);
|
|
39
|
+
x13 ^= (0, utils_1.rotl)(x09 + x05 | 0, 9);
|
|
40
|
+
x01 ^= (0, utils_1.rotl)(x13 + x09 | 0, 13);
|
|
41
|
+
x05 ^= (0, utils_1.rotl)(x01 + x13 | 0, 18);
|
|
42
|
+
x14 ^= (0, utils_1.rotl)(x10 + x06 | 0, 7);
|
|
43
|
+
x02 ^= (0, utils_1.rotl)(x14 + x10 | 0, 9);
|
|
44
|
+
x06 ^= (0, utils_1.rotl)(x02 + x14 | 0, 13);
|
|
45
|
+
x10 ^= (0, utils_1.rotl)(x06 + x02 | 0, 18);
|
|
46
|
+
x03 ^= (0, utils_1.rotl)(x15 + x11 | 0, 7);
|
|
47
|
+
x07 ^= (0, utils_1.rotl)(x03 + x15 | 0, 9);
|
|
48
|
+
x11 ^= (0, utils_1.rotl)(x07 + x03 | 0, 13);
|
|
49
|
+
x15 ^= (0, utils_1.rotl)(x11 + x07 | 0, 18);
|
|
50
|
+
x01 ^= (0, utils_1.rotl)(x00 + x03 | 0, 7);
|
|
51
|
+
x02 ^= (0, utils_1.rotl)(x01 + x00 | 0, 9);
|
|
52
|
+
x03 ^= (0, utils_1.rotl)(x02 + x01 | 0, 13);
|
|
53
|
+
x00 ^= (0, utils_1.rotl)(x03 + x02 | 0, 18);
|
|
54
|
+
x06 ^= (0, utils_1.rotl)(x05 + x04 | 0, 7);
|
|
55
|
+
x07 ^= (0, utils_1.rotl)(x06 + x05 | 0, 9);
|
|
56
|
+
x04 ^= (0, utils_1.rotl)(x07 + x06 | 0, 13);
|
|
57
|
+
x05 ^= (0, utils_1.rotl)(x04 + x07 | 0, 18);
|
|
58
|
+
x11 ^= (0, utils_1.rotl)(x10 + x09 | 0, 7);
|
|
59
|
+
x08 ^= (0, utils_1.rotl)(x11 + x10 | 0, 9);
|
|
60
|
+
x09 ^= (0, utils_1.rotl)(x08 + x11 | 0, 13);
|
|
61
|
+
x10 ^= (0, utils_1.rotl)(x09 + x08 | 0, 18);
|
|
62
|
+
x12 ^= (0, utils_1.rotl)(x15 + x14 | 0, 7);
|
|
63
|
+
x13 ^= (0, utils_1.rotl)(x12 + x15 | 0, 9);
|
|
64
|
+
x14 ^= (0, utils_1.rotl)(x13 + x12 | 0, 13);
|
|
65
|
+
x15 ^= (0, utils_1.rotl)(x14 + x13 | 0, 18);
|
|
66
|
+
}
|
|
67
|
+
// Write output (salsa)
|
|
68
|
+
out[oi++] = (y00 + x00) | 0;
|
|
69
|
+
out[oi++] = (y01 + x01) | 0;
|
|
70
|
+
out[oi++] = (y02 + x02) | 0;
|
|
71
|
+
out[oi++] = (y03 + x03) | 0;
|
|
72
|
+
out[oi++] = (y04 + x04) | 0;
|
|
73
|
+
out[oi++] = (y05 + x05) | 0;
|
|
74
|
+
out[oi++] = (y06 + x06) | 0;
|
|
75
|
+
out[oi++] = (y07 + x07) | 0;
|
|
76
|
+
out[oi++] = (y08 + x08) | 0;
|
|
77
|
+
out[oi++] = (y09 + x09) | 0;
|
|
78
|
+
out[oi++] = (y10 + x10) | 0;
|
|
79
|
+
out[oi++] = (y11 + x11) | 0;
|
|
80
|
+
out[oi++] = (y12 + x12) | 0;
|
|
81
|
+
out[oi++] = (y13 + x13) | 0;
|
|
82
|
+
out[oi++] = (y14 + x14) | 0;
|
|
83
|
+
out[oi++] = (y15 + x15) | 0;
|
|
84
|
+
}
|
|
85
|
+
function BlockMix(input, ii, out, oi, r) {
|
|
86
|
+
// The block B is `r` 128-byte chunks, i.e. `2r` 16-word (64-byte) Salsa blocks.
|
|
87
|
+
let head = oi + 0;
|
|
88
|
+
let tail = oi + 16 * r;
|
|
89
|
+
for (let i = 0; i < 16; i++)
|
|
90
|
+
out[tail + i] = input[ii + (2 * r - 1) * 16 + i]; // X ← B[2r−1]
|
|
91
|
+
for (let i = 0; i < r; i++, head += 16, ii += 16) {
|
|
92
|
+
// RFC 7914 §4 step 3 outputs `Y[0], Y[2], ...` first, then `Y[1], Y[3], ...`;
|
|
93
|
+
// `head` and `tail` lay out those even/odd halves in place.
|
|
94
|
+
XorAndSalsa(out, tail, input, ii, out, head); // head[i] = Salsa(blockIn[2*i] ^ tail[i-1])
|
|
95
|
+
if (i > 0)
|
|
96
|
+
tail += 16; // First iteration overwrites tmp value in tail
|
|
97
|
+
// tail[i] = Salsa(blockIn[2*i+1] ^ head[i])
|
|
98
|
+
XorAndSalsa(out, head, input, (ii += 16), out, tail);
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
// Common prologue and epilogue for sync/async functions
|
|
102
|
+
function scryptInit(password, salt, _opts) {
|
|
103
|
+
// Maxmem - 1GB+1KB by default
|
|
104
|
+
const opts = (0, utils_1.checkOpts)({
|
|
105
|
+
dkLen: 32,
|
|
106
|
+
asyncTick: 10,
|
|
107
|
+
maxmem: 1024 ** 3 + 1024,
|
|
108
|
+
}, _opts);
|
|
109
|
+
const { N, r, p, dkLen, asyncTick, maxmem, onProgress } = opts;
|
|
110
|
+
(0, utils_1.anumber)(N, 'N');
|
|
111
|
+
(0, utils_1.anumber)(r, 'r');
|
|
112
|
+
(0, utils_1.anumber)(p, 'p');
|
|
113
|
+
(0, utils_1.anumber)(dkLen, 'dkLen');
|
|
114
|
+
(0, utils_1.anumber)(asyncTick, 'asyncTick');
|
|
115
|
+
(0, utils_1.anumber)(maxmem, 'maxmem');
|
|
116
|
+
if (onProgress !== undefined && typeof onProgress !== 'function')
|
|
117
|
+
throw new Error('progressCb must be a function');
|
|
118
|
+
const blockSize = 128 * r;
|
|
119
|
+
const blockSize32 = blockSize / 4;
|
|
120
|
+
// Max N is 2^32 (Integrify is 32-bit).
|
|
121
|
+
// Real limit can be 2^22: some JS engines limit Uint8Array to 4GB.
|
|
122
|
+
// Spec check `N >= 2^(blockSize / 8)` is not done for compat with popular libs,
|
|
123
|
+
// which used incorrect r: 1, p: 8. Also, the check seems to be a spec error:
|
|
124
|
+
// https://www.rfc-editor.org/errata_search.php?rfc=7914
|
|
125
|
+
const pow32 = Math.pow(2, 32);
|
|
126
|
+
if (N <= 1 || (N & (N - 1)) !== 0 || N > pow32)
|
|
127
|
+
throw new Error('"N" expected a power of 2, and 2^1 <= N <= 2^32');
|
|
128
|
+
if (p < 1 || p > ((pow32 - 1) * 32) / blockSize)
|
|
129
|
+
throw new Error('"p" expected integer 1..((2^32 - 1) * 32) / (128 * r)');
|
|
130
|
+
// RFC 7914 §2 defines `dkLen` as a positive integer.
|
|
131
|
+
if (dkLen < 1 || dkLen > (pow32 - 1) * 32)
|
|
132
|
+
throw new Error('"dkLen" expected integer 1..(2^32 - 1) * 32');
|
|
133
|
+
// Include the shared `tmp` scratch block so `maxmem` matches noble's actual temporary allocation.
|
|
134
|
+
// Node requires more headroom here, so this accounting is intentionally noble-specific.
|
|
135
|
+
const memUsed = blockSize * (N + p + 1);
|
|
136
|
+
if (memUsed > maxmem)
|
|
137
|
+
throw new Error('"maxmem" limit was hit: memUsed(128*r*(N+p+1))=' + memUsed + ', maxmem=' + maxmem);
|
|
138
|
+
// [B0...Bp−1] ← PBKDF2HMAC-SHA256(Passphrase, Salt, 1, blockSize*ParallelizationFactor)
|
|
139
|
+
// Since it has only one iteration there is no reason to use async variant
|
|
140
|
+
const B = (0, pbkdf2_1.pbkdf2)(sha2_1.sha256, password, salt, { c: 1, dkLen: blockSize * p });
|
|
141
|
+
const B32 = (0, utils_1.u32)(B);
|
|
142
|
+
// Re-used between parallel iterations. Array(iterations) of B
|
|
143
|
+
const V = (0, utils_1.u32)(new Uint8Array(blockSize * N));
|
|
144
|
+
const tmp = (0, utils_1.u32)(new Uint8Array(blockSize));
|
|
145
|
+
let blockMixCb = () => { };
|
|
146
|
+
if (onProgress) {
|
|
147
|
+
const totalBlockMix = 2 * N * p;
|
|
148
|
+
// Invoke callback if progress changes from 10.01 to 10.02
|
|
149
|
+
// Allows to draw smooth progress bar on up to 8K screen
|
|
150
|
+
const callbackPer = Math.max(Math.floor(totalBlockMix / 10000), 1);
|
|
151
|
+
let blockMixCnt = 0;
|
|
152
|
+
blockMixCb = () => {
|
|
153
|
+
blockMixCnt++;
|
|
154
|
+
if (onProgress && (!(blockMixCnt % callbackPer) || blockMixCnt === totalBlockMix))
|
|
155
|
+
onProgress(blockMixCnt / totalBlockMix);
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
return { N, r, p, dkLen, blockSize32, V, B32, B, tmp, blockMixCb, asyncTick };
|
|
159
|
+
}
|
|
160
|
+
function scryptOutput(password, dkLen, B, V, tmp) {
|
|
161
|
+
// Shared final PBKDF2-and-cleanup step: keep the derived key, wipe the scrypt workspace.
|
|
162
|
+
const res = (0, pbkdf2_1.pbkdf2)(sha2_1.sha256, password, B, { c: 1, dkLen });
|
|
163
|
+
(0, utils_1.clean)(B, V, tmp);
|
|
164
|
+
return res;
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Scrypt KDF from RFC 7914. See {@link ScryptOpts}.
|
|
168
|
+
* @param password - password or key material to derive from;
|
|
169
|
+
* JS string inputs are UTF-8 encoded first
|
|
170
|
+
* @param salt - unique salt bytes or string; JS string inputs are UTF-8 encoded first
|
|
171
|
+
* @param opts - Scrypt cost and memory parameters. `dkLen`, if provided,
|
|
172
|
+
* must be `>= 1` per RFC 7914 §2. See {@link ScryptOpts}.
|
|
173
|
+
* @returns Derived key bytes.
|
|
174
|
+
* @throws If the Scrypt cost, memory, or callback options are invalid. {@link Error}
|
|
175
|
+
* @example
|
|
176
|
+
* Derive a key with scrypt.
|
|
177
|
+
* ```ts
|
|
178
|
+
* scrypt('password', 'salt', { N: 2**18, r: 8, p: 1, dkLen: 32 });
|
|
179
|
+
* ```
|
|
180
|
+
*/
|
|
181
|
+
function scrypt(password, salt, opts) {
|
|
182
|
+
const { N, r, p, dkLen, blockSize32, V, B32, B, tmp, blockMixCb } = scryptInit(password, salt, opts);
|
|
183
|
+
(0, utils_1.swap32IfBE)(B32);
|
|
184
|
+
for (let pi = 0; pi < p; pi++) {
|
|
185
|
+
const Pi = blockSize32 * pi;
|
|
186
|
+
for (let i = 0; i < blockSize32; i++)
|
|
187
|
+
V[i] = B32[Pi + i]; // V[0] = B[i]
|
|
188
|
+
for (let i = 0, pos = 0; i < N - 1; i++) {
|
|
189
|
+
BlockMix(V, pos, V, (pos += blockSize32), r); // V[i] = BlockMix(V[i-1]);
|
|
190
|
+
blockMixCb();
|
|
191
|
+
}
|
|
192
|
+
BlockMix(V, (N - 1) * blockSize32, B32, Pi, r); // Process last element
|
|
193
|
+
blockMixCb();
|
|
194
|
+
for (let i = 0; i < N; i++) {
|
|
195
|
+
// First u32 of the last 64-byte block (u32 is LE)
|
|
196
|
+
// RFC 7914 Integerify(X) uses the whole last 64-byte block, but mod N
|
|
197
|
+
// only depends on the low word here because N is a power of two and
|
|
198
|
+
// this implementation caps N at 2^32.
|
|
199
|
+
// & (N - 1) is % N as N is a power of 2, N & (N - 1) = 0 is checked
|
|
200
|
+
// above; >>> 0 for unsigned, input fits in u32.
|
|
201
|
+
const j = (B32[Pi + blockSize32 - 16] & (N - 1)) >>> 0; // j = Integrify(X) % iterations
|
|
202
|
+
// tmp = B ^ V[j]
|
|
203
|
+
for (let k = 0; k < blockSize32; k++)
|
|
204
|
+
tmp[k] = B32[Pi + k] ^ V[j * blockSize32 + k];
|
|
205
|
+
BlockMix(tmp, 0, B32, Pi, r); // B = BlockMix(B ^ V[j])
|
|
206
|
+
blockMixCb();
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
(0, utils_1.swap32IfBE)(B32);
|
|
210
|
+
return scryptOutput(password, dkLen, B, V, tmp);
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Scrypt KDF from RFC 7914. Async version. See {@link ScryptOpts}.
|
|
214
|
+
* @param password - password or key material to derive from;
|
|
215
|
+
* JS string inputs are UTF-8 encoded first
|
|
216
|
+
* @param salt - unique salt bytes or string; JS string inputs are UTF-8 encoded first
|
|
217
|
+
* @param opts - Scrypt cost and memory parameters. `dkLen`, if provided,
|
|
218
|
+
* must be `>= 1` per RFC 7914 §2. `asyncTick` is only a local
|
|
219
|
+
* scheduler-yield control for this JS wrapper, not part of RFC 7914.
|
|
220
|
+
* See {@link ScryptOpts}.
|
|
221
|
+
* @returns Promise resolving to derived key bytes.
|
|
222
|
+
* @throws If the Scrypt cost, memory, or callback options are invalid. {@link Error}
|
|
223
|
+
* @example
|
|
224
|
+
* Derive a key with scrypt asynchronously.
|
|
225
|
+
* ```ts
|
|
226
|
+
* await scryptAsync('password', 'salt', { N: 2**18, r: 8, p: 1, dkLen: 32 });
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
async function scryptAsync(password, salt, opts) {
|
|
230
|
+
const { N, r, p, dkLen, blockSize32, V, B32, B, tmp, blockMixCb, asyncTick } = scryptInit(password, salt, opts);
|
|
231
|
+
(0, utils_1.swap32IfBE)(B32);
|
|
232
|
+
for (let pi = 0; pi < p; pi++) {
|
|
233
|
+
const Pi = blockSize32 * pi;
|
|
234
|
+
for (let i = 0; i < blockSize32; i++)
|
|
235
|
+
V[i] = B32[Pi + i]; // V[0] = B[i]
|
|
236
|
+
let pos = 0;
|
|
237
|
+
await (0, utils_1.asyncLoop)(N - 1, asyncTick, () => {
|
|
238
|
+
BlockMix(V, pos, V, (pos += blockSize32), r); // V[i] = BlockMix(V[i-1]);
|
|
239
|
+
blockMixCb();
|
|
240
|
+
});
|
|
241
|
+
BlockMix(V, (N - 1) * blockSize32, B32, Pi, r); // Process last element
|
|
242
|
+
blockMixCb();
|
|
243
|
+
await (0, utils_1.asyncLoop)(N, asyncTick, () => {
|
|
244
|
+
// First u32 of the last 64-byte block (u32 is LE)
|
|
245
|
+
// RFC 7914 Integerify(X) uses the whole last 64-byte block, but mod N
|
|
246
|
+
// only depends on the low word here because N is a power of two and
|
|
247
|
+
// this implementation caps N at 2^32.
|
|
248
|
+
// & (N - 1) is % N as N is a power of 2, N & (N - 1) = 0 is checked
|
|
249
|
+
// above; >>> 0 for unsigned, input fits in u32.
|
|
250
|
+
const j = (B32[Pi + blockSize32 - 16] & (N - 1)) >>> 0; // j = Integrify(X) % iterations
|
|
251
|
+
// tmp = B ^ V[j]
|
|
252
|
+
for (let k = 0; k < blockSize32; k++)
|
|
253
|
+
tmp[k] = B32[Pi + k] ^ V[j * blockSize32 + k];
|
|
254
|
+
BlockMix(tmp, 0, B32, Pi, r); // B = BlockMix(B ^ V[j])
|
|
255
|
+
blockMixCb();
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
(0, utils_1.swap32IfBE)(B32);
|
|
259
|
+
return scryptOutput(password, dkLen, B, V, tmp);
|
|
260
|
+
}
|
package/sha2.d.ts
ADDED
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* SHA2 hash function. A.k.a. sha256, sha384, sha512, sha512_224, sha512_256.
|
|
3
|
+
* SHA256 is the fastest hash implementable in JS, even faster than Blake3.
|
|
4
|
+
* Check out {@link https://www.rfc-editor.org/rfc/rfc4634 | RFC 4634} and
|
|
5
|
+
* {@link https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf | FIPS 180-4}.
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { HashMD } from './_md';
|
|
9
|
+
import { type CHash, type TRet } from './utils';
|
|
10
|
+
/** Internal SHA-224 / SHA-256 compression engine from RFC 6234 §6.2. */
|
|
11
|
+
declare abstract class SHA2_32B<T extends SHA2_32B<T>> extends HashMD<T> {
|
|
12
|
+
protected abstract A: number;
|
|
13
|
+
protected abstract B: number;
|
|
14
|
+
protected abstract C: number;
|
|
15
|
+
protected abstract D: number;
|
|
16
|
+
protected abstract E: number;
|
|
17
|
+
protected abstract F: number;
|
|
18
|
+
protected abstract G: number;
|
|
19
|
+
protected abstract H: number;
|
|
20
|
+
constructor(outputLen: number);
|
|
21
|
+
protected get(): [number, number, number, number, number, number, number, number];
|
|
22
|
+
protected set(A: number, B: number, C: number, D: number, E: number, F: number, G: number, H: number): void;
|
|
23
|
+
protected process(view: DataView, offset: number): void;
|
|
24
|
+
protected roundClean(): void;
|
|
25
|
+
destroy(): void;
|
|
26
|
+
}
|
|
27
|
+
/** Internal SHA-256 hash class grounded in RFC 6234 §6.2. */
|
|
28
|
+
export declare class _SHA256 extends SHA2_32B<_SHA256> {
|
|
29
|
+
protected A: number;
|
|
30
|
+
protected B: number;
|
|
31
|
+
protected C: number;
|
|
32
|
+
protected D: number;
|
|
33
|
+
protected E: number;
|
|
34
|
+
protected F: number;
|
|
35
|
+
protected G: number;
|
|
36
|
+
protected H: number;
|
|
37
|
+
constructor();
|
|
38
|
+
}
|
|
39
|
+
/** Internal SHA-224 hash class grounded in RFC 6234 §6.2 and §8.5. */
|
|
40
|
+
export declare class _SHA224 extends SHA2_32B<_SHA224> {
|
|
41
|
+
protected A: number;
|
|
42
|
+
protected B: number;
|
|
43
|
+
protected C: number;
|
|
44
|
+
protected D: number;
|
|
45
|
+
protected E: number;
|
|
46
|
+
protected F: number;
|
|
47
|
+
protected G: number;
|
|
48
|
+
protected H: number;
|
|
49
|
+
constructor();
|
|
50
|
+
}
|
|
51
|
+
/** Internal SHA-384 / SHA-512 compression engine from RFC 6234 §6.4. */
|
|
52
|
+
declare abstract class SHA2_64B<T extends SHA2_64B<T>> extends HashMD<T> {
|
|
53
|
+
protected abstract Ah: number;
|
|
54
|
+
protected abstract Al: number;
|
|
55
|
+
protected abstract Bh: number;
|
|
56
|
+
protected abstract Bl: number;
|
|
57
|
+
protected abstract Ch: number;
|
|
58
|
+
protected abstract Cl: number;
|
|
59
|
+
protected abstract Dh: number;
|
|
60
|
+
protected abstract Dl: number;
|
|
61
|
+
protected abstract Eh: number;
|
|
62
|
+
protected abstract El: number;
|
|
63
|
+
protected abstract Fh: number;
|
|
64
|
+
protected abstract Fl: number;
|
|
65
|
+
protected abstract Gh: number;
|
|
66
|
+
protected abstract Gl: number;
|
|
67
|
+
protected abstract Hh: number;
|
|
68
|
+
protected abstract Hl: number;
|
|
69
|
+
constructor(outputLen: number);
|
|
70
|
+
protected get(): [
|
|
71
|
+
number,
|
|
72
|
+
number,
|
|
73
|
+
number,
|
|
74
|
+
number,
|
|
75
|
+
number,
|
|
76
|
+
number,
|
|
77
|
+
number,
|
|
78
|
+
number,
|
|
79
|
+
number,
|
|
80
|
+
number,
|
|
81
|
+
number,
|
|
82
|
+
number,
|
|
83
|
+
number,
|
|
84
|
+
number,
|
|
85
|
+
number,
|
|
86
|
+
number
|
|
87
|
+
];
|
|
88
|
+
protected set(Ah: number, Al: number, Bh: number, Bl: number, Ch: number, Cl: number, Dh: number, Dl: number, Eh: number, El: number, Fh: number, Fl: number, Gh: number, Gl: number, Hh: number, Hl: number): void;
|
|
89
|
+
protected process(view: DataView, offset: number): void;
|
|
90
|
+
protected roundClean(): void;
|
|
91
|
+
destroy(): void;
|
|
92
|
+
}
|
|
93
|
+
/** Internal SHA-512 hash class grounded in RFC 6234 §6.3 and §6.4. */
|
|
94
|
+
export declare class _SHA512 extends SHA2_64B<_SHA512> {
|
|
95
|
+
protected Ah: number;
|
|
96
|
+
protected Al: number;
|
|
97
|
+
protected Bh: number;
|
|
98
|
+
protected Bl: number;
|
|
99
|
+
protected Ch: number;
|
|
100
|
+
protected Cl: number;
|
|
101
|
+
protected Dh: number;
|
|
102
|
+
protected Dl: number;
|
|
103
|
+
protected Eh: number;
|
|
104
|
+
protected El: number;
|
|
105
|
+
protected Fh: number;
|
|
106
|
+
protected Fl: number;
|
|
107
|
+
protected Gh: number;
|
|
108
|
+
protected Gl: number;
|
|
109
|
+
protected Hh: number;
|
|
110
|
+
protected Hl: number;
|
|
111
|
+
constructor();
|
|
112
|
+
}
|
|
113
|
+
/** Internal SHA-384 hash class grounded in RFC 6234 §6.3 and §6.4. */
|
|
114
|
+
export declare class _SHA384 extends SHA2_64B<_SHA384> {
|
|
115
|
+
protected Ah: number;
|
|
116
|
+
protected Al: number;
|
|
117
|
+
protected Bh: number;
|
|
118
|
+
protected Bl: number;
|
|
119
|
+
protected Ch: number;
|
|
120
|
+
protected Cl: number;
|
|
121
|
+
protected Dh: number;
|
|
122
|
+
protected Dl: number;
|
|
123
|
+
protected Eh: number;
|
|
124
|
+
protected El: number;
|
|
125
|
+
protected Fh: number;
|
|
126
|
+
protected Fl: number;
|
|
127
|
+
protected Gh: number;
|
|
128
|
+
protected Gl: number;
|
|
129
|
+
protected Hh: number;
|
|
130
|
+
protected Hl: number;
|
|
131
|
+
constructor();
|
|
132
|
+
}
|
|
133
|
+
/** Internal SHA-512/224 hash class using the derived `T224_IV` and the shared
|
|
134
|
+
* RFC 6234 §6.4 compression engine. */
|
|
135
|
+
export declare class _SHA512_224 extends SHA2_64B<_SHA512_224> {
|
|
136
|
+
protected Ah: number;
|
|
137
|
+
protected Al: number;
|
|
138
|
+
protected Bh: number;
|
|
139
|
+
protected Bl: number;
|
|
140
|
+
protected Ch: number;
|
|
141
|
+
protected Cl: number;
|
|
142
|
+
protected Dh: number;
|
|
143
|
+
protected Dl: number;
|
|
144
|
+
protected Eh: number;
|
|
145
|
+
protected El: number;
|
|
146
|
+
protected Fh: number;
|
|
147
|
+
protected Fl: number;
|
|
148
|
+
protected Gh: number;
|
|
149
|
+
protected Gl: number;
|
|
150
|
+
protected Hh: number;
|
|
151
|
+
protected Hl: number;
|
|
152
|
+
constructor();
|
|
153
|
+
}
|
|
154
|
+
/** Internal SHA-512/256 hash class using the derived `T256_IV` and the shared
|
|
155
|
+
* RFC 6234 §6.4 compression engine. */
|
|
156
|
+
export declare class _SHA512_256 extends SHA2_64B<_SHA512_256> {
|
|
157
|
+
protected Ah: number;
|
|
158
|
+
protected Al: number;
|
|
159
|
+
protected Bh: number;
|
|
160
|
+
protected Bl: number;
|
|
161
|
+
protected Ch: number;
|
|
162
|
+
protected Cl: number;
|
|
163
|
+
protected Dh: number;
|
|
164
|
+
protected Dl: number;
|
|
165
|
+
protected Eh: number;
|
|
166
|
+
protected El: number;
|
|
167
|
+
protected Fh: number;
|
|
168
|
+
protected Fl: number;
|
|
169
|
+
protected Gh: number;
|
|
170
|
+
protected Gl: number;
|
|
171
|
+
protected Hh: number;
|
|
172
|
+
protected Hl: number;
|
|
173
|
+
constructor();
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* SHA2-256 hash function from RFC 4634. In JS it's the fastest: even faster than Blake3. Some info:
|
|
177
|
+
*
|
|
178
|
+
* - Trying 2^128 hashes would get 50% chance of collision, using birthday attack.
|
|
179
|
+
* - BTC network is doing 2^70 hashes/sec (2^95 hashes/year) as per 2025.
|
|
180
|
+
* - Each sha256 hash is executing 2^18 bit operations.
|
|
181
|
+
* - Good 2024 ASICs can do 200Th/sec with 3500 watts of power, corresponding to 2^36 hashes/joule.
|
|
182
|
+
* @param msg - message bytes to hash
|
|
183
|
+
* @returns Digest bytes.
|
|
184
|
+
* @example
|
|
185
|
+
* Hash a message with SHA2-256.
|
|
186
|
+
* ```ts
|
|
187
|
+
* sha256(new Uint8Array([97, 98, 99]));
|
|
188
|
+
* ```
|
|
189
|
+
*/
|
|
190
|
+
export declare const sha256: TRet<CHash<_SHA256>>;
|
|
191
|
+
/**
|
|
192
|
+
* SHA2-224 hash function from RFC 4634.
|
|
193
|
+
* @param msg - message bytes to hash
|
|
194
|
+
* @returns Digest bytes.
|
|
195
|
+
* @example
|
|
196
|
+
* Hash a message with SHA2-224.
|
|
197
|
+
* ```ts
|
|
198
|
+
* sha224(new Uint8Array([97, 98, 99]));
|
|
199
|
+
* ```
|
|
200
|
+
*/
|
|
201
|
+
export declare const sha224: TRet<CHash<_SHA224>>;
|
|
202
|
+
/**
|
|
203
|
+
* SHA2-512 hash function from RFC 4634.
|
|
204
|
+
* @param msg - message bytes to hash
|
|
205
|
+
* @returns Digest bytes.
|
|
206
|
+
* @example
|
|
207
|
+
* Hash a message with SHA2-512.
|
|
208
|
+
* ```ts
|
|
209
|
+
* sha512(new Uint8Array([97, 98, 99]));
|
|
210
|
+
* ```
|
|
211
|
+
*/
|
|
212
|
+
export declare const sha512: TRet<CHash<_SHA512>>;
|
|
213
|
+
/**
|
|
214
|
+
* SHA2-384 hash function from RFC 4634.
|
|
215
|
+
* @param msg - message bytes to hash
|
|
216
|
+
* @returns Digest bytes.
|
|
217
|
+
* @example
|
|
218
|
+
* Hash a message with SHA2-384.
|
|
219
|
+
* ```ts
|
|
220
|
+
* sha384(new Uint8Array([97, 98, 99]));
|
|
221
|
+
* ```
|
|
222
|
+
*/
|
|
223
|
+
export declare const sha384: TRet<CHash<_SHA384>>;
|
|
224
|
+
/**
|
|
225
|
+
* SHA2-512/256 "truncated" hash function, with improved resistance to length extension attacks.
|
|
226
|
+
* See the paper on {@link https://eprint.iacr.org/2010/548.pdf | truncated SHA512}.
|
|
227
|
+
* @param msg - message bytes to hash
|
|
228
|
+
* @returns Digest bytes.
|
|
229
|
+
* @example
|
|
230
|
+
* Hash a message with SHA2-512/256.
|
|
231
|
+
* ```ts
|
|
232
|
+
* sha512_256(new Uint8Array([97, 98, 99]));
|
|
233
|
+
* ```
|
|
234
|
+
*/
|
|
235
|
+
export declare const sha512_256: TRet<CHash<_SHA512_256>>;
|
|
236
|
+
/**
|
|
237
|
+
* SHA2-512/224 "truncated" hash function, with improved resistance to length extension attacks.
|
|
238
|
+
* See the paper on {@link https://eprint.iacr.org/2010/548.pdf | truncated SHA512}.
|
|
239
|
+
* @param msg - message bytes to hash
|
|
240
|
+
* @returns Digest bytes.
|
|
241
|
+
* @example
|
|
242
|
+
* Hash a message with SHA2-512/224.
|
|
243
|
+
* ```ts
|
|
244
|
+
* sha512_224(new Uint8Array([97, 98, 99]));
|
|
245
|
+
* ```
|
|
246
|
+
*/
|
|
247
|
+
export declare const sha512_224: TRet<CHash<_SHA512_224>>;
|
|
248
|
+
export {};
|