@gjsify/crypto 0.3.13 → 0.3.14
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/lib/esm/asn1.js +576 -450
- package/lib/esm/bigint-math.js +37 -28
- package/lib/esm/cipher.js +1252 -1229
- package/lib/esm/constants.js +12 -13
- package/lib/esm/crypto-utils.js +54 -36
- package/lib/esm/dh.js +408 -368
- package/lib/esm/ecdh.js +403 -321
- package/lib/esm/ecdsa.js +138 -111
- package/lib/esm/hash.js +100 -89
- package/lib/esm/hkdf.js +65 -47
- package/lib/esm/hmac.js +95 -90
- package/lib/esm/index.js +75 -148
- package/lib/esm/key-object.js +348 -307
- package/lib/esm/mgf1.js +30 -24
- package/lib/esm/pbkdf2.js +66 -59
- package/lib/esm/public-encrypt.js +203 -156
- package/lib/esm/random.js +137 -124
- package/lib/esm/rsa-oaep.js +94 -87
- package/lib/esm/rsa-pss.js +95 -88
- package/lib/esm/scrypt.js +116 -115
- package/lib/esm/sign.js +267 -237
- package/lib/esm/timing-safe-equal.js +16 -11
- package/lib/esm/x509.js +215 -206
- package/package.json +7 -7
package/lib/esm/scrypt.js
CHANGED
|
@@ -1,134 +1,135 @@
|
|
|
1
|
-
import { Buffer } from "node:buffer";
|
|
2
1
|
import { pbkdf2Sync } from "./pbkdf2.js";
|
|
2
|
+
import { Buffer } from "node:buffer";
|
|
3
|
+
|
|
4
|
+
//#region src/scrypt.ts
|
|
3
5
|
function R(a, b) {
|
|
4
|
-
|
|
6
|
+
return (a << b | a >>> 32 - b) >>> 0;
|
|
5
7
|
}
|
|
6
8
|
function salsa20_8(B) {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
9
|
+
const x = new Uint32Array(16);
|
|
10
|
+
for (let i = 0; i < 16; i++) x[i] = B[i];
|
|
11
|
+
for (let i = 0; i < 4; i++) {
|
|
12
|
+
x[4] ^= R(x[0] + x[12], 7);
|
|
13
|
+
x[8] ^= R(x[4] + x[0], 9);
|
|
14
|
+
x[12] ^= R(x[8] + x[4], 13);
|
|
15
|
+
x[0] ^= R(x[12] + x[8], 18);
|
|
16
|
+
x[9] ^= R(x[5] + x[1], 7);
|
|
17
|
+
x[13] ^= R(x[9] + x[5], 9);
|
|
18
|
+
x[1] ^= R(x[13] + x[9], 13);
|
|
19
|
+
x[5] ^= R(x[1] + x[13], 18);
|
|
20
|
+
x[14] ^= R(x[10] + x[6], 7);
|
|
21
|
+
x[2] ^= R(x[14] + x[10], 9);
|
|
22
|
+
x[6] ^= R(x[2] + x[14], 13);
|
|
23
|
+
x[10] ^= R(x[6] + x[2], 18);
|
|
24
|
+
x[3] ^= R(x[15] + x[11], 7);
|
|
25
|
+
x[7] ^= R(x[3] + x[15], 9);
|
|
26
|
+
x[11] ^= R(x[7] + x[3], 13);
|
|
27
|
+
x[15] ^= R(x[11] + x[7], 18);
|
|
28
|
+
x[1] ^= R(x[0] + x[3], 7);
|
|
29
|
+
x[2] ^= R(x[1] + x[0], 9);
|
|
30
|
+
x[3] ^= R(x[2] + x[1], 13);
|
|
31
|
+
x[0] ^= R(x[3] + x[2], 18);
|
|
32
|
+
x[6] ^= R(x[5] + x[4], 7);
|
|
33
|
+
x[7] ^= R(x[6] + x[5], 9);
|
|
34
|
+
x[4] ^= R(x[7] + x[6], 13);
|
|
35
|
+
x[5] ^= R(x[4] + x[7], 18);
|
|
36
|
+
x[11] ^= R(x[10] + x[9], 7);
|
|
37
|
+
x[8] ^= R(x[11] + x[10], 9);
|
|
38
|
+
x[9] ^= R(x[8] + x[11], 13);
|
|
39
|
+
x[10] ^= R(x[9] + x[8], 18);
|
|
40
|
+
x[12] ^= R(x[15] + x[14], 7);
|
|
41
|
+
x[13] ^= R(x[12] + x[15], 9);
|
|
42
|
+
x[14] ^= R(x[13] + x[12], 13);
|
|
43
|
+
x[15] ^= R(x[14] + x[13], 18);
|
|
44
|
+
}
|
|
45
|
+
for (let i = 0; i < 16; i++) B[i] = B[i] + x[i] >>> 0;
|
|
44
46
|
}
|
|
45
47
|
function blockMix(B, r) {
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
48
|
+
const blockWords = 2 * r * 16;
|
|
49
|
+
const X = new Uint32Array(16);
|
|
50
|
+
for (let i = 0; i < 16; i++) X[i] = B[blockWords - 16 + i];
|
|
51
|
+
const Y = new Uint32Array(blockWords);
|
|
52
|
+
for (let i = 0; i < 2 * r; i++) {
|
|
53
|
+
for (let j = 0; j < 16; j++) X[j] ^= B[i * 16 + j];
|
|
54
|
+
salsa20_8(X);
|
|
55
|
+
for (let j = 0; j < 16; j++) Y[i * 16 + j] = X[j];
|
|
56
|
+
}
|
|
57
|
+
for (let i = 0; i < r; i++) {
|
|
58
|
+
for (let j = 0; j < 16; j++) B[i * 16 + j] = Y[2 * i * 16 + j];
|
|
59
|
+
}
|
|
60
|
+
for (let i = 0; i < r; i++) {
|
|
61
|
+
for (let j = 0; j < 16; j++) B[(r + i) * 16 + j] = Y[(2 * i + 1) * 16 + j];
|
|
62
|
+
}
|
|
61
63
|
}
|
|
62
64
|
function roMix(B, N, r) {
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
65
|
+
const blockWords = 2 * r * 16;
|
|
66
|
+
const V = new Array(N);
|
|
67
|
+
for (let i = 0; i < N; i++) {
|
|
68
|
+
V[i] = new Uint32Array(B);
|
|
69
|
+
blockMix(B, r);
|
|
70
|
+
}
|
|
71
|
+
for (let i = 0; i < N; i++) {
|
|
72
|
+
const j = B[blockWords - 16] & N - 1;
|
|
73
|
+
for (let k = 0; k < blockWords; k++) B[k] ^= V[j][k];
|
|
74
|
+
blockMix(B, r);
|
|
75
|
+
}
|
|
74
76
|
}
|
|
75
77
|
function bytesToWords(bytes) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
78
|
+
const words = new Uint32Array(bytes.length / 4);
|
|
79
|
+
for (let i = 0; i < words.length; i++) {
|
|
80
|
+
words[i] = bytes[i * 4] | bytes[i * 4 + 1] << 8 | bytes[i * 4 + 2] << 16 | bytes[i * 4 + 3] << 24;
|
|
81
|
+
}
|
|
82
|
+
return words;
|
|
81
83
|
}
|
|
82
84
|
function wordsToBytes(words) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
85
|
+
const bytes = new Uint8Array(words.length * 4);
|
|
86
|
+
for (let i = 0; i < words.length; i++) {
|
|
87
|
+
bytes[i * 4] = words[i] & 255;
|
|
88
|
+
bytes[i * 4 + 1] = words[i] >> 8 & 255;
|
|
89
|
+
bytes[i * 4 + 2] = words[i] >> 16 & 255;
|
|
90
|
+
bytes[i * 4 + 3] = words[i] >> 24 & 255;
|
|
91
|
+
}
|
|
92
|
+
return bytes;
|
|
91
93
|
}
|
|
92
94
|
function scryptCore(password, salt, N, r, p, keyLen) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
95
|
+
const blockSize = 128 * r;
|
|
96
|
+
const B = pbkdf2Sync(password, salt, 1, p * blockSize, "sha256");
|
|
97
|
+
for (let i = 0; i < p; i++) {
|
|
98
|
+
const blockBytes = new Uint8Array(B.buffer, B.byteOffset + i * blockSize, blockSize);
|
|
99
|
+
const blockWords = bytesToWords(blockBytes);
|
|
100
|
+
roMix(blockWords, N, r);
|
|
101
|
+
const result = wordsToBytes(blockWords);
|
|
102
|
+
blockBytes.set(result);
|
|
103
|
+
}
|
|
104
|
+
return pbkdf2Sync(password, B, 1, keyLen, "sha256");
|
|
103
105
|
}
|
|
104
106
|
function scryptSync(password, salt, keylen, options) {
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
107
|
+
const pwd = typeof password === "string" ? Buffer.from(password) : Buffer.from(password);
|
|
108
|
+
const slt = typeof salt === "string" ? Buffer.from(salt) : Buffer.from(salt);
|
|
109
|
+
const N = options?.N ?? 16384;
|
|
110
|
+
const r = options?.r ?? 8;
|
|
111
|
+
const p = options?.p ?? 1;
|
|
112
|
+
if (N <= 0 || (N & N - 1) !== 0) {
|
|
113
|
+
throw new Error("N must be a positive power of 2");
|
|
114
|
+
}
|
|
115
|
+
return scryptCore(pwd, slt, N, r, p, keylen);
|
|
114
116
|
}
|
|
115
117
|
function scrypt(password, salt, keylen, optionsOrCallback, callback) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
118
|
+
let options = {};
|
|
119
|
+
let cb;
|
|
120
|
+
if (typeof optionsOrCallback === "function") {
|
|
121
|
+
cb = optionsOrCallback;
|
|
122
|
+
} else {
|
|
123
|
+
options = optionsOrCallback;
|
|
124
|
+
cb = callback;
|
|
125
|
+
}
|
|
126
|
+
try {
|
|
127
|
+
const result = scryptSync(password, salt, keylen, options);
|
|
128
|
+
setTimeout(() => cb(null, result), 0);
|
|
129
|
+
} catch (err) {
|
|
130
|
+
setTimeout(() => cb(err instanceof Error ? err : new Error(String(err)), Buffer.alloc(0)), 0);
|
|
131
|
+
}
|
|
130
132
|
}
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
};
|
|
133
|
+
|
|
134
|
+
//#endregion
|
|
135
|
+
export { scrypt, scryptSync };
|