@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/legacy.js
ADDED
|
@@ -0,0 +1,332 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ripemd160 = exports._RIPEMD160 = exports.md5 = exports._MD5 = exports.sha1 = exports._SHA1 = void 0;
|
|
4
|
+
/**
|
|
5
|
+
|
|
6
|
+
SHA1 (RFC 3174), MD5 (RFC 1321), and RIPEMD160 legacy, weak hash functions.
|
|
7
|
+
RFC 2286 only covers HMAC-RIPEMD160 wrapper material and test vectors,
|
|
8
|
+
not the base RIPEMD-160 compression spec.
|
|
9
|
+
Don't use them in a new protocol. What "weak" means:
|
|
10
|
+
|
|
11
|
+
- Collisions can be made with 2^18 effort in MD5, 2^60 in SHA1, 2^80 in RIPEMD160.
|
|
12
|
+
- No practical pre-image attacks (only theoretical, 2^123.4)
|
|
13
|
+
- HMAC seems kinda ok: https://www.rfc-editor.org/rfc/rfc6151
|
|
14
|
+
* @module
|
|
15
|
+
*/
|
|
16
|
+
const _md_1 = require("./_md");
|
|
17
|
+
const utils_1 = require("./utils");
|
|
18
|
+
/** Initial SHA-1 state from RFC 3174 §6.1. */
|
|
19
|
+
const SHA1_IV = /* @__PURE__ */ Uint32Array.from([
|
|
20
|
+
0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0,
|
|
21
|
+
]);
|
|
22
|
+
// Reusable 80-word SHA-1 message schedule buffer.
|
|
23
|
+
const SHA1_W = /* @__PURE__ */ new Uint32Array(80);
|
|
24
|
+
/** Internal SHA1 legacy hash class. */
|
|
25
|
+
class _SHA1 extends _md_1.HashMD {
|
|
26
|
+
A = SHA1_IV[0] | 0;
|
|
27
|
+
B = SHA1_IV[1] | 0;
|
|
28
|
+
C = SHA1_IV[2] | 0;
|
|
29
|
+
D = SHA1_IV[3] | 0;
|
|
30
|
+
E = SHA1_IV[4] | 0;
|
|
31
|
+
constructor() {
|
|
32
|
+
super(64, 20, 8, false);
|
|
33
|
+
}
|
|
34
|
+
get() {
|
|
35
|
+
const { A, B, C, D, E } = this;
|
|
36
|
+
return [A, B, C, D, E];
|
|
37
|
+
}
|
|
38
|
+
set(A, B, C, D, E) {
|
|
39
|
+
this.A = A | 0;
|
|
40
|
+
this.B = B | 0;
|
|
41
|
+
this.C = C | 0;
|
|
42
|
+
this.D = D | 0;
|
|
43
|
+
this.E = E | 0;
|
|
44
|
+
}
|
|
45
|
+
process(view, offset) {
|
|
46
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
|
47
|
+
SHA1_W[i] = view.getUint32(offset, false);
|
|
48
|
+
for (let i = 16; i < 80; i++)
|
|
49
|
+
SHA1_W[i] = (0, utils_1.rotl)(SHA1_W[i - 3] ^ SHA1_W[i - 8] ^ SHA1_W[i - 14] ^ SHA1_W[i - 16], 1);
|
|
50
|
+
// Compression function main loop, 80 rounds
|
|
51
|
+
let { A, B, C, D, E } = this;
|
|
52
|
+
for (let i = 0; i < 80; i++) {
|
|
53
|
+
let F, K;
|
|
54
|
+
if (i < 20) {
|
|
55
|
+
F = (0, _md_1.Chi)(B, C, D);
|
|
56
|
+
K = 0x5a827999;
|
|
57
|
+
}
|
|
58
|
+
else if (i < 40) {
|
|
59
|
+
F = B ^ C ^ D;
|
|
60
|
+
K = 0x6ed9eba1;
|
|
61
|
+
}
|
|
62
|
+
else if (i < 60) {
|
|
63
|
+
F = (0, _md_1.Maj)(B, C, D);
|
|
64
|
+
K = 0x8f1bbcdc;
|
|
65
|
+
}
|
|
66
|
+
else {
|
|
67
|
+
F = B ^ C ^ D;
|
|
68
|
+
K = 0xca62c1d6;
|
|
69
|
+
}
|
|
70
|
+
const T = ((0, utils_1.rotl)(A, 5) + F + E + K + SHA1_W[i]) | 0;
|
|
71
|
+
E = D;
|
|
72
|
+
D = C;
|
|
73
|
+
C = (0, utils_1.rotl)(B, 30);
|
|
74
|
+
B = A;
|
|
75
|
+
A = T;
|
|
76
|
+
}
|
|
77
|
+
// Add the compressed chunk to the current hash value
|
|
78
|
+
A = (A + this.A) | 0;
|
|
79
|
+
B = (B + this.B) | 0;
|
|
80
|
+
C = (C + this.C) | 0;
|
|
81
|
+
D = (D + this.D) | 0;
|
|
82
|
+
E = (E + this.E) | 0;
|
|
83
|
+
this.set(A, B, C, D, E);
|
|
84
|
+
}
|
|
85
|
+
roundClean() {
|
|
86
|
+
(0, utils_1.clean)(SHA1_W);
|
|
87
|
+
}
|
|
88
|
+
destroy() {
|
|
89
|
+
// HashMD callers route post-destroy usability through `destroyed`; zeroizing alone still leaves
|
|
90
|
+
// update()/digest() callable on reused instances.
|
|
91
|
+
this.destroyed = true;
|
|
92
|
+
this.set(0, 0, 0, 0, 0);
|
|
93
|
+
(0, utils_1.clean)(this.buffer);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
exports._SHA1 = _SHA1;
|
|
97
|
+
/**
|
|
98
|
+
* SHA1 (RFC 3174) legacy hash function. It was cryptographically broken.
|
|
99
|
+
* @param msg - message bytes to hash
|
|
100
|
+
* @returns Digest bytes.
|
|
101
|
+
* @example
|
|
102
|
+
* Hash a message with SHA1.
|
|
103
|
+
* ```ts
|
|
104
|
+
* sha1(new Uint8Array([97, 98, 99]));
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
exports.sha1 = (0, utils_1.createHasher)(() => new _SHA1());
|
|
108
|
+
/** RFC 1321 `T[i]` uses `floor(2^32 * abs(sin(i)))`; this is the shared `2^32` scale factor. */
|
|
109
|
+
const p32 = /* @__PURE__ */ Math.pow(2, 32);
|
|
110
|
+
/** RFC 1321 `T[1..64]` table. */
|
|
111
|
+
const K = /* @__PURE__ */ Array.from({ length: 64 }, (_, i) => Math.floor(p32 * Math.abs(Math.sin(i + 1))));
|
|
112
|
+
/** MD5 initial state from RFC 1321, stored as 4 u32 words. */
|
|
113
|
+
const MD5_IV = /* @__PURE__ */ SHA1_IV.slice(0, 4);
|
|
114
|
+
// Reusable 16-word MD5 message block buffer.
|
|
115
|
+
const MD5_W = /* @__PURE__ */ new Uint32Array(16);
|
|
116
|
+
/** Internal MD5 legacy hash class. */
|
|
117
|
+
class _MD5 extends _md_1.HashMD {
|
|
118
|
+
A = MD5_IV[0] | 0;
|
|
119
|
+
B = MD5_IV[1] | 0;
|
|
120
|
+
C = MD5_IV[2] | 0;
|
|
121
|
+
D = MD5_IV[3] | 0;
|
|
122
|
+
constructor() {
|
|
123
|
+
super(64, 16, 8, true);
|
|
124
|
+
}
|
|
125
|
+
get() {
|
|
126
|
+
const { A, B, C, D } = this;
|
|
127
|
+
return [A, B, C, D];
|
|
128
|
+
}
|
|
129
|
+
set(A, B, C, D) {
|
|
130
|
+
this.A = A | 0;
|
|
131
|
+
this.B = B | 0;
|
|
132
|
+
this.C = C | 0;
|
|
133
|
+
this.D = D | 0;
|
|
134
|
+
}
|
|
135
|
+
process(view, offset) {
|
|
136
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
|
137
|
+
MD5_W[i] = view.getUint32(offset, true);
|
|
138
|
+
// Compression function main loop, 64 rounds
|
|
139
|
+
let { A, B, C, D } = this;
|
|
140
|
+
for (let i = 0; i < 64; i++) {
|
|
141
|
+
let F, g, s;
|
|
142
|
+
if (i < 16) {
|
|
143
|
+
F = (0, _md_1.Chi)(B, C, D);
|
|
144
|
+
g = i;
|
|
145
|
+
s = [7, 12, 17, 22];
|
|
146
|
+
}
|
|
147
|
+
else if (i < 32) {
|
|
148
|
+
// RFC 1321 round 2 uses G(B,C,D) = (B & D) | (C & ~D), which is `Chi(D, B, C)`.
|
|
149
|
+
F = (0, _md_1.Chi)(D, B, C);
|
|
150
|
+
g = (5 * i + 1) % 16;
|
|
151
|
+
s = [5, 9, 14, 20];
|
|
152
|
+
}
|
|
153
|
+
else if (i < 48) {
|
|
154
|
+
F = B ^ C ^ D;
|
|
155
|
+
g = (3 * i + 5) % 16;
|
|
156
|
+
s = [4, 11, 16, 23];
|
|
157
|
+
}
|
|
158
|
+
else {
|
|
159
|
+
F = C ^ (B | ~D);
|
|
160
|
+
g = (7 * i) % 16;
|
|
161
|
+
s = [6, 10, 15, 21];
|
|
162
|
+
}
|
|
163
|
+
F = F + A + K[i] + MD5_W[g];
|
|
164
|
+
A = D;
|
|
165
|
+
D = C;
|
|
166
|
+
C = B;
|
|
167
|
+
B = B + (0, utils_1.rotl)(F, s[i % 4]);
|
|
168
|
+
}
|
|
169
|
+
// Add the compressed chunk to the current hash value
|
|
170
|
+
A = (A + this.A) | 0;
|
|
171
|
+
B = (B + this.B) | 0;
|
|
172
|
+
C = (C + this.C) | 0;
|
|
173
|
+
D = (D + this.D) | 0;
|
|
174
|
+
this.set(A, B, C, D);
|
|
175
|
+
}
|
|
176
|
+
roundClean() {
|
|
177
|
+
(0, utils_1.clean)(MD5_W);
|
|
178
|
+
}
|
|
179
|
+
destroy() {
|
|
180
|
+
// HashMD callers route post-destroy usability through `destroyed`; zeroizing alone still leaves
|
|
181
|
+
// update()/digest() callable on reused instances.
|
|
182
|
+
this.destroyed = true;
|
|
183
|
+
this.set(0, 0, 0, 0);
|
|
184
|
+
(0, utils_1.clean)(this.buffer);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
exports._MD5 = _MD5;
|
|
188
|
+
/**
|
|
189
|
+
* MD5 (RFC 1321) legacy hash function. It was cryptographically broken.
|
|
190
|
+
* MD5 architecture is similar to SHA1, with some differences:
|
|
191
|
+
* - Reduced output length: 16 bytes (128 bit) instead of 20
|
|
192
|
+
* - 64 rounds, instead of 80
|
|
193
|
+
* - Little-endian: could be faster, but will require more code
|
|
194
|
+
* - Non-linear index selection: huge speed-up for unroll
|
|
195
|
+
* - Per round constants: more memory accesses, additional speed-up for unroll
|
|
196
|
+
* @param msg - message bytes to hash
|
|
197
|
+
* @returns Digest bytes.
|
|
198
|
+
* @example
|
|
199
|
+
* Hash a message with MD5.
|
|
200
|
+
* ```ts
|
|
201
|
+
* md5(new Uint8Array([97, 98, 99]));
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
exports.md5 = (0, utils_1.createHasher)(() => new _MD5());
|
|
205
|
+
// RIPEMD-160
|
|
206
|
+
// Permutation repeatedly applied to derive the later RIPEMD-160 message-order tables.
|
|
207
|
+
const Rho160 = /* @__PURE__ */ Uint8Array.from([
|
|
208
|
+
7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
|
|
209
|
+
]);
|
|
210
|
+
const Id160 = /* @__PURE__ */ (() => Uint8Array.from(new Array(16).fill(0).map((_, i) => i)))();
|
|
211
|
+
const Pi160 = /* @__PURE__ */ (() => Id160.map((i) => (9 * i + 5) % 16))();
|
|
212
|
+
// Five left/right message-word orderings for the RIPEMD-160 dual-lane rounds.
|
|
213
|
+
const idxLR = /* @__PURE__ */ (() => {
|
|
214
|
+
const L = [Id160];
|
|
215
|
+
const R = [Pi160];
|
|
216
|
+
const res = [L, R];
|
|
217
|
+
for (let i = 0; i < 4; i++)
|
|
218
|
+
for (let j of res)
|
|
219
|
+
j.push(j[i].map((k) => Rho160[k]));
|
|
220
|
+
return res;
|
|
221
|
+
})();
|
|
222
|
+
const idxL = /* @__PURE__ */ (() => idxLR[0])();
|
|
223
|
+
const idxR = /* @__PURE__ */ (() => idxLR[1])();
|
|
224
|
+
// const [idxL, idxR] = idxLR;
|
|
225
|
+
// Base per-group shift table before the left/right message-order permutations are applied.
|
|
226
|
+
const shifts160 = /* @__PURE__ */ [
|
|
227
|
+
[11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8],
|
|
228
|
+
[12, 13, 11, 15, 6, 9, 9, 7, 12, 15, 11, 13, 7, 8, 7, 7],
|
|
229
|
+
[13, 15, 14, 11, 7, 7, 6, 8, 13, 14, 13, 12, 5, 5, 6, 9],
|
|
230
|
+
[14, 11, 12, 14, 8, 6, 5, 5, 15, 12, 15, 14, 9, 9, 8, 6],
|
|
231
|
+
[15, 12, 13, 13, 9, 5, 8, 6, 14, 11, 12, 11, 8, 6, 5, 5],
|
|
232
|
+
].map((i) => Uint8Array.from(i));
|
|
233
|
+
const shiftsL160 = /* @__PURE__ */ idxL.map((idx, i) => idx.map((j) => shifts160[i][j]));
|
|
234
|
+
const shiftsR160 = /* @__PURE__ */ idxR.map((idx, i) => idx.map((j) => shifts160[i][j]));
|
|
235
|
+
// Five left-lane additive constants for RIPEMD-160.
|
|
236
|
+
const Kl160 = /* @__PURE__ */ Uint32Array.from([
|
|
237
|
+
0x00000000, 0x5a827999, 0x6ed9eba1, 0x8f1bbcdc, 0xa953fd4e,
|
|
238
|
+
]);
|
|
239
|
+
// Five right-lane additive constants for RIPEMD-160.
|
|
240
|
+
const Kr160 = /* @__PURE__ */ Uint32Array.from([
|
|
241
|
+
0x50a28be6, 0x5c4dd124, 0x6d703ef3, 0x7a6d76e9, 0x00000000,
|
|
242
|
+
]);
|
|
243
|
+
// Called `f()` in the spec; valid `group` values are 0..4, and out-of-range
|
|
244
|
+
// inputs currently fall through to the group-4 branch.
|
|
245
|
+
function ripemd_f(group, x, y, z) {
|
|
246
|
+
if (group === 0)
|
|
247
|
+
return x ^ y ^ z;
|
|
248
|
+
if (group === 1)
|
|
249
|
+
return (x & y) | (~x & z);
|
|
250
|
+
if (group === 2)
|
|
251
|
+
return (x | ~y) ^ z;
|
|
252
|
+
if (group === 3)
|
|
253
|
+
return (x & z) | (y & ~z);
|
|
254
|
+
return x ^ (y | ~z);
|
|
255
|
+
}
|
|
256
|
+
// Reusable 16-word RIPEMD-160 message block buffer.
|
|
257
|
+
const BUF_160 = /* @__PURE__ */ new Uint32Array(16);
|
|
258
|
+
/**
|
|
259
|
+
* Internal RIPEMD-160 legacy hash class.
|
|
260
|
+
* RFC 2286 only adds HMAC-RIPEMD160 material, not the core hash specification.
|
|
261
|
+
*/
|
|
262
|
+
class _RIPEMD160 extends _md_1.HashMD {
|
|
263
|
+
h0 = 0x67452301 | 0;
|
|
264
|
+
h1 = 0xefcdab89 | 0;
|
|
265
|
+
h2 = 0x98badcfe | 0;
|
|
266
|
+
h3 = 0x10325476 | 0;
|
|
267
|
+
h4 = 0xc3d2e1f0 | 0;
|
|
268
|
+
constructor() {
|
|
269
|
+
super(64, 20, 8, true);
|
|
270
|
+
}
|
|
271
|
+
get() {
|
|
272
|
+
const { h0, h1, h2, h3, h4 } = this;
|
|
273
|
+
return [h0, h1, h2, h3, h4];
|
|
274
|
+
}
|
|
275
|
+
set(h0, h1, h2, h3, h4) {
|
|
276
|
+
this.h0 = h0 | 0;
|
|
277
|
+
this.h1 = h1 | 0;
|
|
278
|
+
this.h2 = h2 | 0;
|
|
279
|
+
this.h3 = h3 | 0;
|
|
280
|
+
this.h4 = h4 | 0;
|
|
281
|
+
}
|
|
282
|
+
process(view, offset) {
|
|
283
|
+
for (let i = 0; i < 16; i++, offset += 4)
|
|
284
|
+
BUF_160[i] = view.getUint32(offset, true);
|
|
285
|
+
// prettier-ignore
|
|
286
|
+
let al = this.h0 | 0, ar = al, bl = this.h1 | 0, br = bl, cl = this.h2 | 0, cr = cl, dl = this.h3 | 0, dr = dl, el = this.h4 | 0, er = el;
|
|
287
|
+
// Instead of iterating 0 to 80, we split it into 5 groups
|
|
288
|
+
// And use the groups in constants, functions, etc. Much simpler
|
|
289
|
+
for (let group = 0; group < 5; group++) {
|
|
290
|
+
const rGroup = 4 - group;
|
|
291
|
+
const hbl = Kl160[group], hbr = Kr160[group]; // prettier-ignore
|
|
292
|
+
const rl = idxL[group], rr = idxR[group]; // prettier-ignore
|
|
293
|
+
const sl = shiftsL160[group], sr = shiftsR160[group]; // prettier-ignore
|
|
294
|
+
for (let i = 0; i < 16; i++) {
|
|
295
|
+
const tl = ((0, utils_1.rotl)(al + ripemd_f(group, bl, cl, dl) + BUF_160[rl[i]] + hbl, sl[i]) + el) | 0;
|
|
296
|
+
al = el, el = dl, dl = (0, utils_1.rotl)(cl, 10) | 0, cl = bl, bl = tl; // prettier-ignore
|
|
297
|
+
}
|
|
298
|
+
// 2 loops are 10% faster
|
|
299
|
+
for (let i = 0; i < 16; i++) {
|
|
300
|
+
const tr = ((0, utils_1.rotl)(ar + ripemd_f(rGroup, br, cr, dr) + BUF_160[rr[i]] + hbr, sr[i]) + er) | 0;
|
|
301
|
+
ar = er, er = dr, dr = (0, utils_1.rotl)(cr, 10) | 0, cr = br, br = tr; // prettier-ignore
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
// Add the compressed chunk to the current hash value
|
|
305
|
+
// Final recombination cross-adds the left/right lane accumulators into the next h0..h4 order.
|
|
306
|
+
this.set((this.h1 + cl + dr) | 0, (this.h2 + dl + er) | 0, (this.h3 + el + ar) | 0, (this.h4 + al + br) | 0, (this.h0 + bl + cr) | 0);
|
|
307
|
+
}
|
|
308
|
+
roundClean() {
|
|
309
|
+
(0, utils_1.clean)(BUF_160);
|
|
310
|
+
}
|
|
311
|
+
destroy() {
|
|
312
|
+
this.destroyed = true;
|
|
313
|
+
(0, utils_1.clean)(this.buffer);
|
|
314
|
+
this.set(0, 0, 0, 0, 0);
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
exports._RIPEMD160 = _RIPEMD160;
|
|
318
|
+
/**
|
|
319
|
+
* RIPEMD-160 - a legacy hash function from 1990s.
|
|
320
|
+
* RFC 2286 only covers HMAC-RIPEMD160 test material; the links below point
|
|
321
|
+
* at the base RIPEMD-160 references.
|
|
322
|
+
* * {@link https://homes.esat.kuleuven.be/~bosselae/ripemd160.html}
|
|
323
|
+
* * {@link https://homes.esat.kuleuven.be/~bosselae/ripemd160/pdf/AB-9601/AB-9601.pdf}
|
|
324
|
+
* @param msg - message bytes to hash
|
|
325
|
+
* @returns Digest bytes.
|
|
326
|
+
* @example
|
|
327
|
+
* Hash a message with RIPEMD-160.
|
|
328
|
+
* ```ts
|
|
329
|
+
* ripemd160(new Uint8Array([97, 98, 99]));
|
|
330
|
+
* ```
|
|
331
|
+
*/
|
|
332
|
+
exports.ripemd160 = (0, utils_1.createHasher)(() => new _RIPEMD160());
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@decryption/hashes",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "Audited & minimal 0-dependency JS implementation of SHA, RIPEMD, BLAKE, HMAC, HKDF, PBKDF2, Scrypt & Argon2. Dual CJS+ESM fork of @noble/hashes.",
|
|
5
|
+
"author": "Constructive <developers@constructive.io>",
|
|
6
|
+
"main": "index.js",
|
|
7
|
+
"module": "esm/index.js",
|
|
8
|
+
"types": "index.d.ts",
|
|
9
|
+
"homepage": "https://github.com/constructive-io/decryption",
|
|
10
|
+
"license": "MIT",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/constructive-io/decryption"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/constructive-io/decryption/issues"
|
|
21
|
+
},
|
|
22
|
+
"scripts": {
|
|
23
|
+
"copy": "makage assets",
|
|
24
|
+
"clean": "makage clean",
|
|
25
|
+
"prepublishOnly": "npm run build",
|
|
26
|
+
"build": "makage build && cp LICENSE dist/LICENSE",
|
|
27
|
+
"lint": "echo \"vendored fork — linted upstream\" && exit 0",
|
|
28
|
+
"test": "jest",
|
|
29
|
+
"test:watch": "jest --watch"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"sha256",
|
|
33
|
+
"sha512",
|
|
34
|
+
"sha3",
|
|
35
|
+
"blake3",
|
|
36
|
+
"hmac",
|
|
37
|
+
"hkdf",
|
|
38
|
+
"pbkdf2",
|
|
39
|
+
"scrypt",
|
|
40
|
+
"argon2",
|
|
41
|
+
"hash"
|
|
42
|
+
],
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"makage": "0.3.0"
|
|
45
|
+
},
|
|
46
|
+
"gitHead": "8d85fbfdbb1d57da93e99904a03e6e8d8531d281"
|
|
47
|
+
}
|
package/pbkdf2.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { type CHash, type KDFInput, type TArg, type TRet } from './utils';
|
|
2
|
+
/**
|
|
3
|
+
* PBKDF2 options:
|
|
4
|
+
* * c: iterations, should probably be higher than 100_000
|
|
5
|
+
* * dkLen: desired length of derived key in bytes, must be `>= 1` per RFC 8018 §5.2
|
|
6
|
+
* * asyncTick: max time in ms for which async function can block execution
|
|
7
|
+
*/
|
|
8
|
+
export type Pbkdf2Opt = {
|
|
9
|
+
/** Iteration count. Higher values increase CPU cost. */
|
|
10
|
+
c: number;
|
|
11
|
+
/** Desired derived key length in bytes, must be `>= 1` per RFC 8018 §5.2. */
|
|
12
|
+
dkLen?: number;
|
|
13
|
+
/** Max scheduler block time in milliseconds for the async variant. */
|
|
14
|
+
asyncTick?: number;
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* PBKDF2-HMAC: RFC 8018 key derivation function.
|
|
18
|
+
* @param hash - hash function that would be used e.g. sha256
|
|
19
|
+
* @param password - password from which a derived key is generated;
|
|
20
|
+
* JS string inputs are UTF-8 encoded first
|
|
21
|
+
* @param salt - cryptographic salt; JS string inputs are UTF-8 encoded first
|
|
22
|
+
* @param opts - PBKDF2 work factor and output settings. `dkLen`, if provided,
|
|
23
|
+
* must be `>= 1` per RFC 8018 §5.2. See {@link Pbkdf2Opt}.
|
|
24
|
+
* @returns Derived key bytes.
|
|
25
|
+
* @throws If the PBKDF2 iteration count or derived-key settings are invalid. {@link Error}
|
|
26
|
+
* @example
|
|
27
|
+
* PBKDF2-HMAC: RFC 2898 key derivation function.
|
|
28
|
+
* ```ts
|
|
29
|
+
* import { pbkdf2 } from '@decryption/hashes/pbkdf2.js';
|
|
30
|
+
* import { sha256 } from '@decryption/hashes/sha2.js';
|
|
31
|
+
* const key = pbkdf2(sha256, 'password', 'salt', { dkLen: 32, c: Math.pow(2, 18) });
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
export declare function pbkdf2(hash: TArg<CHash>, password: TArg<KDFInput>, salt: TArg<KDFInput>, opts: TArg<Pbkdf2Opt>): TRet<Uint8Array>;
|
|
35
|
+
/**
|
|
36
|
+
* PBKDF2-HMAC: RFC 8018 key derivation function. Async version.
|
|
37
|
+
* @param hash - hash function that would be used e.g. sha256
|
|
38
|
+
* @param password - password from which a derived key is generated;
|
|
39
|
+
* JS string inputs are UTF-8 encoded first
|
|
40
|
+
* @param salt - cryptographic salt; JS string inputs are UTF-8 encoded first
|
|
41
|
+
* @param opts - PBKDF2 work factor and output settings. `dkLen`, if provided,
|
|
42
|
+
* must be `>= 1` per RFC 8018 §5.2. `asyncTick` is only a local
|
|
43
|
+
* scheduler-yield knob for this JS wrapper, not part of RFC 8018.
|
|
44
|
+
* See {@link Pbkdf2Opt}.
|
|
45
|
+
* @returns Promise resolving to derived key bytes.
|
|
46
|
+
* @throws If the PBKDF2 iteration count or derived-key settings are invalid. {@link Error}
|
|
47
|
+
* @example
|
|
48
|
+
* PBKDF2-HMAC: RFC 2898 key derivation function.
|
|
49
|
+
* ```ts
|
|
50
|
+
* import { pbkdf2Async } from '@decryption/hashes/pbkdf2.js';
|
|
51
|
+
* import { sha256 } from '@decryption/hashes/sha2.js';
|
|
52
|
+
* const key = await pbkdf2Async(sha256, 'password', 'salt', { dkLen: 32, c: 500_000 });
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
export declare function pbkdf2Async(hash: TArg<CHash>, password: TArg<KDFInput>, salt: TArg<KDFInput>, opts: TArg<Pbkdf2Opt>): Promise<TRet<Uint8Array>>;
|
package/pbkdf2.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.pbkdf2 = pbkdf2;
|
|
4
|
+
exports.pbkdf2Async = pbkdf2Async;
|
|
5
|
+
/**
|
|
6
|
+
* PBKDF (RFC 2898). Can be used to create a key from password and salt.
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
const hmac_1 = require("./hmac");
|
|
10
|
+
// prettier-ignore
|
|
11
|
+
const utils_1 = require("./utils");
|
|
12
|
+
// Common start and end for sync/async functions
|
|
13
|
+
function pbkdf2Init(hash, _password, _salt, _opts) {
|
|
14
|
+
(0, utils_1.ahash)(hash);
|
|
15
|
+
const opts = (0, utils_1.checkOpts)({ dkLen: 32, asyncTick: 10 }, _opts);
|
|
16
|
+
const { c, dkLen, asyncTick } = opts;
|
|
17
|
+
(0, utils_1.anumber)(c, 'c');
|
|
18
|
+
(0, utils_1.anumber)(dkLen, 'dkLen');
|
|
19
|
+
(0, utils_1.anumber)(asyncTick, 'asyncTick');
|
|
20
|
+
if (c < 1)
|
|
21
|
+
throw new Error('iterations (c) must be >= 1');
|
|
22
|
+
// RFC 8018 §5.2 defines `dkLen` as "a positive integer".
|
|
23
|
+
if (dkLen < 1)
|
|
24
|
+
throw new Error('"dkLen" must be >= 1');
|
|
25
|
+
// RFC 8018 §5.2 step 1 requires rejecting oversize `dkLen`
|
|
26
|
+
// before allocating the destination buffer.
|
|
27
|
+
if (dkLen > (2 ** 32 - 1) * hash.outputLen)
|
|
28
|
+
throw new Error('derived key too long');
|
|
29
|
+
const password = (0, utils_1.kdfInputToBytes)(_password, 'password');
|
|
30
|
+
const salt = (0, utils_1.kdfInputToBytes)(_salt, 'salt');
|
|
31
|
+
// DK = PBKDF2(PRF, Password, Salt, c, dkLen);
|
|
32
|
+
const DK = new Uint8Array(dkLen);
|
|
33
|
+
// U1 = PRF(Password, Salt + INT_32_BE(i))
|
|
34
|
+
const PRF = hmac_1.hmac.create(hash, password);
|
|
35
|
+
// Cache PRF(P, S || ...) prefix state so each block only appends INT_32_BE(i).
|
|
36
|
+
const PRFSalt = PRF._cloneInto().update(salt);
|
|
37
|
+
return { c, dkLen, asyncTick, DK, PRF, PRFSalt };
|
|
38
|
+
}
|
|
39
|
+
function pbkdf2Output(PRF, PRFSalt, DK, prfW, u) {
|
|
40
|
+
// Shared sync/async cleanup point: wipe transient PRF state
|
|
41
|
+
// while preserving the derived key buffer.
|
|
42
|
+
PRF.destroy();
|
|
43
|
+
PRFSalt.destroy();
|
|
44
|
+
if (prfW)
|
|
45
|
+
prfW.destroy();
|
|
46
|
+
(0, utils_1.clean)(u);
|
|
47
|
+
return DK;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* PBKDF2-HMAC: RFC 8018 key derivation function.
|
|
51
|
+
* @param hash - hash function that would be used e.g. sha256
|
|
52
|
+
* @param password - password from which a derived key is generated;
|
|
53
|
+
* JS string inputs are UTF-8 encoded first
|
|
54
|
+
* @param salt - cryptographic salt; JS string inputs are UTF-8 encoded first
|
|
55
|
+
* @param opts - PBKDF2 work factor and output settings. `dkLen`, if provided,
|
|
56
|
+
* must be `>= 1` per RFC 8018 §5.2. See {@link Pbkdf2Opt}.
|
|
57
|
+
* @returns Derived key bytes.
|
|
58
|
+
* @throws If the PBKDF2 iteration count or derived-key settings are invalid. {@link Error}
|
|
59
|
+
* @example
|
|
60
|
+
* PBKDF2-HMAC: RFC 2898 key derivation function.
|
|
61
|
+
* ```ts
|
|
62
|
+
* import { pbkdf2 } from '@decryption/hashes/pbkdf2.js';
|
|
63
|
+
* import { sha256 } from '@decryption/hashes/sha2.js';
|
|
64
|
+
* const key = pbkdf2(sha256, 'password', 'salt', { dkLen: 32, c: Math.pow(2, 18) });
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
function pbkdf2(hash, password, salt, opts) {
|
|
68
|
+
const { c, dkLen, DK, PRF, PRFSalt } = pbkdf2Init(hash, password, salt, opts);
|
|
69
|
+
let prfW; // Working copy
|
|
70
|
+
const arr = new Uint8Array(4);
|
|
71
|
+
const view = (0, utils_1.createView)(arr);
|
|
72
|
+
const u = new Uint8Array(PRF.outputLen);
|
|
73
|
+
// DK = T1 + T2 + ⋯ + Tdklen/hlen
|
|
74
|
+
for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += PRF.outputLen) {
|
|
75
|
+
// Ti = F(Password, Salt, c, i)
|
|
76
|
+
// The last Ti view can be shorter than hLen, which applies
|
|
77
|
+
// RFC 8018 §5.2 step 4's T_l<0..r-1> truncation without extra copies.
|
|
78
|
+
const Ti = DK.subarray(pos, pos + PRF.outputLen);
|
|
79
|
+
view.setInt32(0, ti, false);
|
|
80
|
+
// F(Password, Salt, c, i) = U1 ^ U2 ^ ⋯ ^ Uc
|
|
81
|
+
// U1 = PRF(Password, Salt + INT_32_BE(i))
|
|
82
|
+
(prfW = PRFSalt._cloneInto(prfW)).update(arr).digestInto(u);
|
|
83
|
+
Ti.set(u.subarray(0, Ti.length));
|
|
84
|
+
for (let ui = 1; ui < c; ui++) {
|
|
85
|
+
// Uc = PRF(Password, Uc−1)
|
|
86
|
+
PRF._cloneInto(prfW).update(u).digestInto(u);
|
|
87
|
+
for (let i = 0; i < Ti.length; i++)
|
|
88
|
+
Ti[i] ^= u[i];
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
return pbkdf2Output(PRF, PRFSalt, DK, prfW, u);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* PBKDF2-HMAC: RFC 8018 key derivation function. Async version.
|
|
95
|
+
* @param hash - hash function that would be used e.g. sha256
|
|
96
|
+
* @param password - password from which a derived key is generated;
|
|
97
|
+
* JS string inputs are UTF-8 encoded first
|
|
98
|
+
* @param salt - cryptographic salt; JS string inputs are UTF-8 encoded first
|
|
99
|
+
* @param opts - PBKDF2 work factor and output settings. `dkLen`, if provided,
|
|
100
|
+
* must be `>= 1` per RFC 8018 §5.2. `asyncTick` is only a local
|
|
101
|
+
* scheduler-yield knob for this JS wrapper, not part of RFC 8018.
|
|
102
|
+
* See {@link Pbkdf2Opt}.
|
|
103
|
+
* @returns Promise resolving to derived key bytes.
|
|
104
|
+
* @throws If the PBKDF2 iteration count or derived-key settings are invalid. {@link Error}
|
|
105
|
+
* @example
|
|
106
|
+
* PBKDF2-HMAC: RFC 2898 key derivation function.
|
|
107
|
+
* ```ts
|
|
108
|
+
* import { pbkdf2Async } from '@decryption/hashes/pbkdf2.js';
|
|
109
|
+
* import { sha256 } from '@decryption/hashes/sha2.js';
|
|
110
|
+
* const key = await pbkdf2Async(sha256, 'password', 'salt', { dkLen: 32, c: 500_000 });
|
|
111
|
+
* ```
|
|
112
|
+
*/
|
|
113
|
+
async function pbkdf2Async(hash, password, salt, opts) {
|
|
114
|
+
const { c, dkLen, asyncTick, DK, PRF, PRFSalt } = pbkdf2Init(hash, password, salt, opts);
|
|
115
|
+
let prfW; // Working copy
|
|
116
|
+
const arr = new Uint8Array(4);
|
|
117
|
+
const view = (0, utils_1.createView)(arr);
|
|
118
|
+
const u = new Uint8Array(PRF.outputLen);
|
|
119
|
+
// DK = T1 + T2 + ⋯ + Tdklen/hlen
|
|
120
|
+
for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += PRF.outputLen) {
|
|
121
|
+
// Ti = F(Password, Salt, c, i)
|
|
122
|
+
// The last Ti view can be shorter than hLen, which applies
|
|
123
|
+
// RFC 8018 §5.2 step 4's T_l<0..r-1> truncation without extra copies.
|
|
124
|
+
const Ti = DK.subarray(pos, pos + PRF.outputLen);
|
|
125
|
+
view.setInt32(0, ti, false);
|
|
126
|
+
// F(Password, Salt, c, i) = U1 ^ U2 ^ ⋯ ^ Uc
|
|
127
|
+
// U1 = PRF(Password, Salt + INT_32_BE(i))
|
|
128
|
+
(prfW = PRFSalt._cloneInto(prfW)).update(arr).digestInto(u);
|
|
129
|
+
Ti.set(u.subarray(0, Ti.length));
|
|
130
|
+
await (0, utils_1.asyncLoop)(c - 1, asyncTick, () => {
|
|
131
|
+
// Uc = PRF(Password, Uc−1)
|
|
132
|
+
PRF._cloneInto(prfW).update(u).digestInto(u);
|
|
133
|
+
for (let i = 0; i < Ti.length; i++)
|
|
134
|
+
Ti[i] ^= u[i];
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
return pbkdf2Output(PRF, PRFSalt, DK, prfW, u);
|
|
138
|
+
}
|
package/scrypt.d.ts
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { type KDFInput, type TArg, type TRet } from './utils';
|
|
2
|
+
/**
|
|
3
|
+
* Scrypt options:
|
|
4
|
+
* - `N` is cpu/mem work factor (power of 2 e.g. `2**18`)
|
|
5
|
+
* - `r` is block size (8 is common), fine-tunes sequential memory read size and performance
|
|
6
|
+
* - `p` is parallelization factor (1 is common)
|
|
7
|
+
* - `dkLen` is output key length in bytes e.g. 32, and must be `>= 1` per RFC 7914 §2.
|
|
8
|
+
* - `asyncTick` - (default: 10) max time in ms for which async function can block execution
|
|
9
|
+
* - `maxmem` - (default: `1024 ** 3 + 1024` aka 1GB+1KB). A limit that the app could use for scrypt
|
|
10
|
+
* - `onProgress` - callback function that would be executed for progress report
|
|
11
|
+
*/
|
|
12
|
+
export type ScryptOpts = {
|
|
13
|
+
/** CPU and memory work factor. Must be a power of two. */
|
|
14
|
+
N: number;
|
|
15
|
+
/** Block size parameter. */
|
|
16
|
+
r: number;
|
|
17
|
+
/** Parallelization factor. */
|
|
18
|
+
p: number;
|
|
19
|
+
/** Desired derived key length in bytes, must be `>= 1` per RFC 7914 §2. */
|
|
20
|
+
dkLen?: number;
|
|
21
|
+
/** Max scheduler block time in milliseconds for the async variant. */
|
|
22
|
+
asyncTick?: number;
|
|
23
|
+
/** Maximum temporary memory budget in bytes. */
|
|
24
|
+
maxmem?: number;
|
|
25
|
+
/**
|
|
26
|
+
* Optional progress callback invoked during long-running derivations.
|
|
27
|
+
* param progress - completion fraction in the `0..1` range
|
|
28
|
+
*/
|
|
29
|
+
onProgress?: (progress: number) => void;
|
|
30
|
+
};
|
|
31
|
+
/**
|
|
32
|
+
* Scrypt KDF from RFC 7914. See {@link ScryptOpts}.
|
|
33
|
+
* @param password - password or key material to derive from;
|
|
34
|
+
* JS string inputs are UTF-8 encoded first
|
|
35
|
+
* @param salt - unique salt bytes or string; JS string inputs are UTF-8 encoded first
|
|
36
|
+
* @param opts - Scrypt cost and memory parameters. `dkLen`, if provided,
|
|
37
|
+
* must be `>= 1` per RFC 7914 §2. See {@link ScryptOpts}.
|
|
38
|
+
* @returns Derived key bytes.
|
|
39
|
+
* @throws If the Scrypt cost, memory, or callback options are invalid. {@link Error}
|
|
40
|
+
* @example
|
|
41
|
+
* Derive a key with scrypt.
|
|
42
|
+
* ```ts
|
|
43
|
+
* scrypt('password', 'salt', { N: 2**18, r: 8, p: 1, dkLen: 32 });
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare function scrypt(password: TArg<KDFInput>, salt: TArg<KDFInput>, opts: TArg<ScryptOpts>): TRet<Uint8Array>;
|
|
47
|
+
/**
|
|
48
|
+
* Scrypt KDF from RFC 7914. Async version. See {@link ScryptOpts}.
|
|
49
|
+
* @param password - password or key material to derive from;
|
|
50
|
+
* JS string inputs are UTF-8 encoded first
|
|
51
|
+
* @param salt - unique salt bytes or string; JS string inputs are UTF-8 encoded first
|
|
52
|
+
* @param opts - Scrypt cost and memory parameters. `dkLen`, if provided,
|
|
53
|
+
* must be `>= 1` per RFC 7914 §2. `asyncTick` is only a local
|
|
54
|
+
* scheduler-yield control for this JS wrapper, not part of RFC 7914.
|
|
55
|
+
* See {@link ScryptOpts}.
|
|
56
|
+
* @returns Promise resolving to derived key bytes.
|
|
57
|
+
* @throws If the Scrypt cost, memory, or callback options are invalid. {@link Error}
|
|
58
|
+
* @example
|
|
59
|
+
* Derive a key with scrypt asynchronously.
|
|
60
|
+
* ```ts
|
|
61
|
+
* await scryptAsync('password', 'salt', { N: 2**18, r: 8, p: 1, dkLen: 32 });
|
|
62
|
+
* ```
|
|
63
|
+
*/
|
|
64
|
+
export declare function scryptAsync(password: TArg<KDFInput>, salt: TArg<KDFInput>, opts: TArg<ScryptOpts>): Promise<TRet<Uint8Array>>;
|