@gjsify/webcrypto 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/crypto-key.js +26 -20
- package/lib/esm/index.js +41 -41
- package/lib/esm/register.js +5 -1
- package/lib/esm/subtle.js +639 -580
- package/lib/esm/util.js +95 -101
- package/package.json +4 -4
package/lib/esm/crypto-key.js
CHANGED
|
@@ -1,21 +1,27 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
//#region src/crypto-key.ts
|
|
2
|
+
/**
|
|
3
|
+
* CryptoKey represents a cryptographic key obtained from SubtleCrypto methods.
|
|
4
|
+
* It stores algorithm metadata, key type, extractability, and usage flags
|
|
5
|
+
* alongside the raw key material (internal handle).
|
|
6
|
+
*/
|
|
7
|
+
var CryptoKey = class {
|
|
8
|
+
type;
|
|
9
|
+
extractable;
|
|
10
|
+
algorithm;
|
|
11
|
+
usages;
|
|
12
|
+
/** @internal Raw key material — not visible to user code */
|
|
13
|
+
_handle;
|
|
14
|
+
constructor(type, extractable, algorithm, usages, handle) {
|
|
15
|
+
this.type = type;
|
|
16
|
+
this.extractable = extractable;
|
|
17
|
+
this.algorithm = Object.freeze({ ...algorithm });
|
|
18
|
+
this.usages = Object.freeze([...usages]);
|
|
19
|
+
this._handle = handle;
|
|
20
|
+
}
|
|
21
|
+
get [Symbol.toStringTag]() {
|
|
22
|
+
return "CryptoKey";
|
|
23
|
+
}
|
|
21
24
|
};
|
|
25
|
+
|
|
26
|
+
//#endregion
|
|
27
|
+
export { CryptoKey };
|
package/lib/esm/index.js
CHANGED
|
@@ -1,46 +1,46 @@
|
|
|
1
|
-
import { SubtleCrypto } from "./subtle.js";
|
|
2
1
|
import { CryptoKey } from "./crypto-key.js";
|
|
2
|
+
import { SubtleCrypto } from "./subtle.js";
|
|
3
|
+
|
|
4
|
+
//#region src/index.ts
|
|
3
5
|
const _nativeCrypto = typeof globalThis.crypto !== "undefined" ? globalThis.crypto : null;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
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
|
-
|
|
6
|
+
/**
|
|
7
|
+
* Crypto object per the W3C WebCrypto API.
|
|
8
|
+
* Provides getRandomValues(), randomUUID(), and subtle (SubtleCrypto).
|
|
9
|
+
*/
|
|
10
|
+
var CryptoPolyfill = class {
|
|
11
|
+
subtle = new SubtleCrypto();
|
|
12
|
+
getRandomValues(array) {
|
|
13
|
+
if (!(array instanceof Int8Array || array instanceof Uint8Array || array instanceof Int16Array || array instanceof Uint16Array || array instanceof Int32Array || array instanceof Uint32Array || array instanceof Uint8ClampedArray || array instanceof BigInt64Array || array instanceof BigUint64Array)) {
|
|
14
|
+
throw new DOMException("The provided value is not of type '(Int8Array or Int16Array or Int32Array or Uint8Array or Uint8ClampedArray or Uint16Array or Uint32Array or BigInt64Array or BigUint64Array)'", "TypeMismatchError");
|
|
15
|
+
}
|
|
16
|
+
if (array.byteLength > 65536) {
|
|
17
|
+
throw new DOMException("The ArrayBufferView's byte length exceeds the number of bytes of entropy available via this API (65536)", "QuotaExceededError");
|
|
18
|
+
}
|
|
19
|
+
const bytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
|
|
20
|
+
if (_nativeCrypto && typeof _nativeCrypto.getRandomValues === "function") {
|
|
21
|
+
_nativeCrypto.getRandomValues(bytes);
|
|
22
|
+
} else {
|
|
23
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
24
|
+
bytes[i] = Math.floor(Math.random() * 256);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return array;
|
|
28
|
+
}
|
|
29
|
+
randomUUID() {
|
|
30
|
+
if (_nativeCrypto && typeof _nativeCrypto.randomUUID === "function") {
|
|
31
|
+
return _nativeCrypto.randomUUID();
|
|
32
|
+
}
|
|
33
|
+
const bytes = new Uint8Array(16);
|
|
34
|
+
this.getRandomValues(bytes);
|
|
35
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
36
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
37
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
38
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
35
41
|
const hasNativeSubtle = _nativeCrypto !== null && typeof _nativeCrypto.subtle !== "undefined" && typeof _nativeCrypto.subtle.digest === "function";
|
|
36
42
|
const cryptoInstance = hasNativeSubtle ? _nativeCrypto : new CryptoPolyfill();
|
|
37
43
|
const subtleInstance = hasNativeSubtle ? _nativeCrypto.subtle : new SubtleCrypto();
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
CryptoKey,
|
|
42
|
-
SubtleCrypto,
|
|
43
|
-
cryptoInstance as crypto,
|
|
44
|
-
index_default as default,
|
|
45
|
-
subtleInstance as subtle
|
|
46
|
-
};
|
|
44
|
+
|
|
45
|
+
//#endregion
|
|
46
|
+
export { CryptoPolyfill as Crypto, CryptoKey, SubtleCrypto, cryptoInstance as crypto, cryptoInstance as default, subtleInstance as subtle };
|
package/lib/esm/register.js
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
import { crypto as cryptoInstance } from "./index.js";
|
|
2
|
+
|
|
3
|
+
//#region src/register.ts
|
|
2
4
|
if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle === "undefined") {
|
|
3
|
-
|
|
5
|
+
globalThis.crypto = cryptoInstance;
|
|
4
6
|
}
|
|
7
|
+
|
|
8
|
+
//#endregion
|