@gjsify/webcrypto 0.24.1 → 0.25.1
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/index.js +1 -1
- package/lib/esm/random.js +1 -0
- package/lib/types/random.d.ts +34 -0
- package/lib/types/random.spec.d.ts +2 -0
- package/package.json +8 -4
package/lib/esm/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"./_virtual/_rolldown/runtime.js";import{CryptoKey as e}from"./crypto-key.js";import{SubtleCrypto as t}from"./subtle.js";const
|
|
1
|
+
import"./_virtual/_rolldown/runtime.js";import{CryptoKey as e}from"./crypto-key.js";import{SubtleCrypto as t}from"./subtle.js";import{fillRandomBytes as n}from"./random.js";const r=globalThis.crypto===void 0?null:globalThis.crypto;var CryptoPolyfill=class{subtle=new t;getRandomValues(e){if(!(e instanceof Int8Array||e instanceof Uint8Array||e instanceof Int16Array||e instanceof Uint16Array||e instanceof Int32Array||e instanceof Uint32Array||e instanceof Uint8ClampedArray||e instanceof BigInt64Array||e instanceof BigUint64Array))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`);if(e.byteLength>65536)throw new DOMException(`The ArrayBufferView's byte length exceeds the number of bytes of entropy available via this API (65536)`,`QuotaExceededError`);return n(new Uint8Array(e.buffer,e.byteOffset,e.byteLength),{webcrypto:r?.getRandomValues?r:null}),e}randomUUID(){if(r&&typeof r.randomUUID==`function`)return r.randomUUID();let e=new Uint8Array(16);this.getRandomValues(e),e[6]=e[6]&15|64,e[8]=e[8]&63|128;let t=Array.from(e,e=>e.toString(16).padStart(2,`0`)).join(``);return`${t.slice(0,8)}-${t.slice(8,12)}-${t.slice(12,16)}-${t.slice(16,20)}-${t.slice(20)}`}};const i=r!==null&&r.subtle!==void 0&&typeof r.subtle.digest==`function`,a=i?r:new CryptoPolyfill,o=i?r.subtle:new t;export{CryptoPolyfill as Crypto,e as CryptoKey,t as SubtleCrypto,a as crypto,a as default,o as subtle};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import"./_virtual/_rolldown/runtime.js";const e=65536,t=[`webcrypto`,`urandom`];let n=!1,r=null;function gjsGi(){return globalThis.imports?.gi}function isSecureRandomSource(e){return t.includes(e)}function fillFromWebCrypto(t,n){for(let r=0;r<t.length;r+=e){let i=Math.min(t.length-r,e);n.getRandomValues(new Uint8Array(t.buffer,t.byteOffset+r,i))}}function openUrandom(){if(r!==null)return r;let e=gjsGi()?.Gio;if(!e)return r=!1,!1;try{r=e.File.new_for_path(`/dev/urandom`).read(null)}catch{r=!1}return r}function fillFromUrandom(e){let t=openUrandom();if(!t)return!1;try{let n=0;for(;n<e.length;){let r=t.read_bytes(e.length-n,null).toArray();if(r.length===0)return!1;e.set(r,n),n+=r.length}return!0}catch{return!1}}function fillFromGLib(e){let t=gjsGi()?.GLib;if(!t?.random_int_range)return!1;for(let n=0;n<e.length;n++)e[n]=t.random_int_range(0,256);return!0}function fillFromMath(e){for(let t=0;t<e.length;t++)e[t]=Math.floor(Math.random()*256)}function warnInsecureOnce(e){n||(n=!0,console.warn(`[@gjsify/utils] No cryptographically secure random source available — falling back to "${e}". Neither GLib.Random nor Math.random is suitable for keys, tokens or IVs. Expected sources: WebCrypto (globalThis.crypto) or /dev/urandom via Gio on GJS.`))}function fillRandomBytes(e,t={}){if(e.length===0)return`webcrypto`;let n=t.webcrypto===void 0?globalThis.crypto??null:t.webcrypto;return n&&typeof n.getRandomValues==`function`?(fillFromWebCrypto(e,n),`webcrypto`):fillFromUrandom(e)?`urandom`:fillFromGLib(e)?(warnInsecureOnce(`glib`),`glib`):(fillFromMath(e),warnInsecureOnce(`math`),`math`)}function _resetRandomSourceCache(){r=null,n=!1}export{_resetRandomSourceCache,fillRandomBytes,isSecureRandomSource};
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/** Which source filled the buffer. Tiers 3–4 are NOT cryptographically secure. */
|
|
2
|
+
export type RandomSource = 'webcrypto' | 'urandom' | 'glib' | 'math';
|
|
3
|
+
/**
|
|
4
|
+
* The minimal WebCrypto surface this module needs.
|
|
5
|
+
*
|
|
6
|
+
* Deliberately NARROWER than the DOM `Crypto` interface — it takes the one
|
|
7
|
+
* concrete view type the chain ever passes, so a real `Crypto` (whose
|
|
8
|
+
* `getRandomValues` is generic over `Exclude<BufferSource, ArrayBuffer>`)
|
|
9
|
+
* satisfies it by instantiation, and a hand-rolled source does not have to
|
|
10
|
+
* reproduce that generic signature to be usable.
|
|
11
|
+
*/
|
|
12
|
+
export interface WebCryptoRandomSource {
|
|
13
|
+
getRandomValues(array: Uint8Array<ArrayBuffer>): unknown;
|
|
14
|
+
}
|
|
15
|
+
export interface FillRandomBytesOptions {
|
|
16
|
+
/**
|
|
17
|
+
* WebCrypto source for tier 1. Defaults to `globalThis.crypto`.
|
|
18
|
+
*
|
|
19
|
+
* Pass `null` to SKIP the tier — that is how `@gjsify/webcrypto`'s own
|
|
20
|
+
* polyfill uses this without recursing into itself.
|
|
21
|
+
*/
|
|
22
|
+
webcrypto?: WebCryptoRandomSource | null;
|
|
23
|
+
}
|
|
24
|
+
/** Whether bytes from `source` are suitable for cryptographic use. */
|
|
25
|
+
export declare function isSecureRandomSource(source: RandomSource): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Fill `view` with random bytes from the best available source.
|
|
28
|
+
*
|
|
29
|
+
* @returns the source that produced the bytes — check it with
|
|
30
|
+
* {@link isSecureRandomSource} when the caller needs a CSPRNG.
|
|
31
|
+
*/
|
|
32
|
+
export declare function fillRandomBytes(view: Uint8Array, options?: FillRandomBytesOptions): RandomSource;
|
|
33
|
+
/** Reset the cached probes — tests only. */
|
|
34
|
+
export declare function _resetRandomSourceCache(): void;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gjsify/webcrypto",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.25.1",
|
|
4
4
|
"description": "W3C WebCrypto API (SubtleCrypto) for GJS using @gjsify/crypto primitives",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "lib/esm/index.js",
|
|
@@ -10,6 +10,10 @@
|
|
|
10
10
|
"types": "./lib/types/index.d.ts",
|
|
11
11
|
"default": "./lib/esm/index.js"
|
|
12
12
|
},
|
|
13
|
+
"./random": {
|
|
14
|
+
"types": "./lib/types/random.d.ts",
|
|
15
|
+
"default": "./lib/esm/random.js"
|
|
16
|
+
},
|
|
13
17
|
"./register": {
|
|
14
18
|
"types": "./lib/types/register.d.ts",
|
|
15
19
|
"default": "./lib/esm/register.js"
|
|
@@ -45,11 +49,11 @@
|
|
|
45
49
|
"crypto"
|
|
46
50
|
],
|
|
47
51
|
"dependencies": {
|
|
48
|
-
"@gjsify/dom-exception": "^0.
|
|
52
|
+
"@gjsify/dom-exception": "^0.25.1"
|
|
49
53
|
},
|
|
50
54
|
"devDependencies": {
|
|
51
|
-
"@gjsify/cli": "^0.
|
|
52
|
-
"@gjsify/unit": "^0.
|
|
55
|
+
"@gjsify/cli": "^0.25.1",
|
|
56
|
+
"@gjsify/unit": "^0.25.1",
|
|
53
57
|
"@types/node": "^25.9.2",
|
|
54
58
|
"typescript": "^6.0.3"
|
|
55
59
|
},
|