@gjsify/webcrypto 0.1.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/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # @gjsify/webcrypto
2
+
3
+ GJS implementation of the Web Crypto API using GLib.
4
+
5
+ Part of the [gjsify](https://github.com/gjsify/gjsify) project — Node.js and Web APIs for GJS (GNOME JavaScript).
6
+
7
+ ## Installation
8
+
9
+ ```bash
10
+ npm install @gjsify/webcrypto
11
+ # or
12
+ yarn add @gjsify/webcrypto
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ ```typescript
18
+ import { crypto } from '@gjsify/webcrypto';
19
+
20
+ const array = new Uint8Array(16);
21
+ crypto.getRandomValues(array);
22
+
23
+ const uuid = crypto.randomUUID();
24
+ ```
25
+
26
+ ## License
27
+
28
+ MIT
package/globals.mjs ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Re-exports native WebCrypto globals for use in Node.js builds.
3
+ * On Node.js, crypto.subtle is a native global.
4
+ */
5
+ export default globalThis.crypto;
6
+ export const subtle = globalThis.crypto.subtle;
@@ -0,0 +1,21 @@
1
+ class CryptoKey {
2
+ type;
3
+ extractable;
4
+ algorithm;
5
+ usages;
6
+ /** @internal Raw key material — not visible to user code */
7
+ _handle;
8
+ constructor(type, extractable, algorithm, usages, handle) {
9
+ this.type = type;
10
+ this.extractable = extractable;
11
+ this.algorithm = Object.freeze({ ...algorithm });
12
+ this.usages = Object.freeze([...usages]);
13
+ this._handle = handle;
14
+ }
15
+ get [Symbol.toStringTag]() {
16
+ return "CryptoKey";
17
+ }
18
+ }
19
+ export {
20
+ CryptoKey
21
+ };
@@ -0,0 +1,49 @@
1
+ import { SubtleCrypto } from "./subtle.js";
2
+ import { CryptoKey } from "./crypto-key.js";
3
+ const _nativeCrypto = typeof globalThis.crypto !== "undefined" ? globalThis.crypto : null;
4
+ class CryptoPolyfill {
5
+ subtle = new SubtleCrypto();
6
+ getRandomValues(array) {
7
+ 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)) {
8
+ 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");
9
+ }
10
+ if (array.byteLength > 65536) {
11
+ throw new DOMException("The ArrayBufferView's byte length exceeds the number of bytes of entropy available via this API (65536)", "QuotaExceededError");
12
+ }
13
+ const bytes = new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
14
+ if (_nativeCrypto && typeof _nativeCrypto.getRandomValues === "function") {
15
+ _nativeCrypto.getRandomValues(bytes);
16
+ } else {
17
+ for (let i = 0; i < bytes.length; i++) {
18
+ bytes[i] = Math.floor(Math.random() * 256);
19
+ }
20
+ }
21
+ return array;
22
+ }
23
+ randomUUID() {
24
+ if (_nativeCrypto && typeof _nativeCrypto.randomUUID === "function") {
25
+ return _nativeCrypto.randomUUID();
26
+ }
27
+ const bytes = new Uint8Array(16);
28
+ this.getRandomValues(bytes);
29
+ bytes[6] = bytes[6] & 15 | 64;
30
+ bytes[8] = bytes[8] & 63 | 128;
31
+ const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
32
+ return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
33
+ }
34
+ }
35
+ const hasNativeSubtle = _nativeCrypto !== null && typeof _nativeCrypto.subtle !== "undefined" && typeof _nativeCrypto.subtle.digest === "function";
36
+ const cryptoInstance = hasNativeSubtle ? _nativeCrypto : new CryptoPolyfill();
37
+ const subtleInstance = hasNativeSubtle ? _nativeCrypto.subtle : new SubtleCrypto();
38
+ if (typeof globalThis.crypto === "undefined" || typeof globalThis.crypto.subtle === "undefined") {
39
+ globalThis.crypto = cryptoInstance;
40
+ }
41
+ var index_default = cryptoInstance;
42
+ export {
43
+ CryptoPolyfill as Crypto,
44
+ CryptoKey,
45
+ SubtleCrypto,
46
+ cryptoInstance as crypto,
47
+ index_default as default,
48
+ subtleInstance as subtle
49
+ };