@gjsify/crypto 0.3.12 → 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/bigint-math.js
CHANGED
|
@@ -1,34 +1,43 @@
|
|
|
1
|
+
//#region src/bigint-math.ts
|
|
2
|
+
/**
|
|
3
|
+
* Modular exponentiation using square-and-multiply (BigInt).
|
|
4
|
+
* Returns (base ** exp) % mod.
|
|
5
|
+
*/
|
|
1
6
|
function modPow(base, exp, mod) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
if (mod === 1n) return 0n;
|
|
8
|
+
base = (base % mod + mod) % mod;
|
|
9
|
+
let result = 1n;
|
|
10
|
+
while (exp > 0n) {
|
|
11
|
+
if (exp & 1n) {
|
|
12
|
+
result = result * base % mod;
|
|
13
|
+
}
|
|
14
|
+
exp >>= 1n;
|
|
15
|
+
base = base * base % mod;
|
|
16
|
+
}
|
|
17
|
+
return result;
|
|
13
18
|
}
|
|
19
|
+
/**
|
|
20
|
+
* Convert a BigInt to a big-endian Uint8Array of exactly `length` bytes.
|
|
21
|
+
*/
|
|
14
22
|
function bigIntToBytes(value, length) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
23
|
+
const result = new Uint8Array(length);
|
|
24
|
+
let v = value;
|
|
25
|
+
for (let i = length - 1; i >= 0; i--) {
|
|
26
|
+
result[i] = Number(v & 255n);
|
|
27
|
+
v >>= 8n;
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
22
30
|
}
|
|
31
|
+
/**
|
|
32
|
+
* Convert a Uint8Array (big-endian) to a non-negative BigInt.
|
|
33
|
+
*/
|
|
23
34
|
function bytesToBigInt(bytes) {
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
35
|
+
let result = 0n;
|
|
36
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
37
|
+
result = result << 8n | BigInt(bytes[i]);
|
|
38
|
+
}
|
|
39
|
+
return result;
|
|
29
40
|
}
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
modPow
|
|
34
|
-
};
|
|
41
|
+
|
|
42
|
+
//#endregion
|
|
43
|
+
export { bigIntToBytes, bytesToBigInt, modPow };
|