@gjsify/webcrypto 0.55.0 → 0.57.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/lib/esm/cfrg.js +1 -1
- package/package.json +4 -4
package/lib/esm/cfrg.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"./_virtual/_rolldown/runtime.js";import{CryptoKey as e}from"./crypto-key.js";import{base64urlDecode as t,toUint8Array as n}from"./util.js";import{DOMException as r}from"@gjsify/dom-exception";const i={Ed25519:{public:[`verify`],private:[`sign`]},X25519:{public:[],private:[`deriveKey`,`deriveBits`]}},a={Ed25519:[48,42,48,5,6,3,43,101,112,3,33,0],X25519:[48,42,48,5,6,3,43,101,110,3,33,0]};function cfrgName(e){let t=e.toUpperCase();if(t===`ED25519`)return`Ed25519`;if(t===`X25519`)return`X25519`}function assertUsages(e,t){for(let n of e)if(!t.includes(n))throw new r(`Unsupported key usage: ${n}`,`SyntaxError`)}function toArrayBuffer(e){return e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)}function keyObjectOf(e){return e._handle.keyObject}function cfrgGenerateKey(t,n,a,o){assertUsages(o,[...i[n].public,...i[n].private]);let s=o.filter(e=>i[n].private.includes(e));if(s.length===0)throw new r(`Usages cannot be empty when creating a key.`,`SyntaxError`);let c=o.filter(e=>i[n].public.includes(e)),{publicKey:l,privateKey:u}=t.generateKeyPairSync(n.toLowerCase());return{publicKey:new e(`public`,!0,{name:n},c,{keyObject:l}),privateKey:new e(`private`,a,{name:n},s,{keyObject:u})}}function validateJwk(e,t,n,i){if(typeof e!=`object`||!e)throw new r(`Invalid keyData`,`DataError`);if(e.kty!==`OKP`)throw new r(`Invalid JWK "kty" Parameter`,`DataError`);if(e.crv!==t)throw new r(`JWK "crv" Parameter and algorithm name mismatch`,`DataError`);if(t===`Ed25519`&&e.alg!==void 0&&e.alg!==`Ed25519`&&e.alg!==`EdDSA`)throw new r(`JWK "alg" does not match the requested algorithm`,`DataError`);let a=t===`X25519`?`enc`:`sig`;if(i.length>0&&e.use!==void 0&&e.use!==a)throw new r(`Invalid JWK "use" Parameter`,`DataError`);if(e.key_ops!==void 0){if(!Array.isArray(e.key_ops))throw new r(`Invalid JWK "key_ops" Parameter`,`DataError`);for(let t of i)if(!e.key_ops.includes(t))throw new r(`Key operations and usage mismatch`,`DataError`)}if(e.ext===!1&&n)throw new r(`JWK "ext" Parameter and extractable mismatch`,`DataError`);if(typeof e.x!=`string`||e.d!==void 0&&typeof e.d!=`string`)throw new r(`Invalid keyData`,`DataError`)}function cfrgImportKey(o,s,c,l,u,d){let f,p,importOrDataError=e=>{try{return e()}catch(e){throw new r(`Invalid keyData: ${e.message}`,`DataError`)}};switch(s){case`raw`:{assertUsages(d,i[l].public);let e=n(c);if(e.length!==32)throw new r(`Invalid keyData`,`DataError`);let t=new Uint8Array(44);t.set(a[l],0),t.set(e,12),f=importOrDataError(()=>o.createPublicKey({key:t,format:`der`,type:`spki`})),p=`public`;break}case`spki`:{assertUsages(d,i[l].public);let e=n(c);f=importOrDataError(()=>o.createPublicKey({key:e,format:`der`,type:`spki`})),p=`public`;break}case`pkcs8`:{assertUsages(d,i[l].private);let e=n(c);f=importOrDataError(()=>o.createPrivateKey({key:e,format:`der`,type:`pkcs8`})),p=`private`;break}case`jwk`:{let e=c;if(validateJwk(e,l,u,d),p=e.d===void 0?`public`:`private`,assertUsages(d,i[l][p]),p===`private`){if(f=importOrDataError(()=>o.createPrivateKey({key:e,format:`jwk`})),o.createPublicKey(f).export({format:`jwk`}).x!==e.x)throw new r(`Invalid JWK: "x" does not match "d"`,`DataError`)}else{if(t(e.x).length!==32)throw new r(`Invalid keyData`,`DataError`);f=importOrDataError(()=>o.createPublicKey({key:e,format:`jwk`}))}break}default:throw new r(`Unsupported format: ${s}`,`NotSupportedError`)}if(f.asymmetricKeyType!==l.toLowerCase())throw new r(`Invalid key type`,`DataError`);if(p===`private`&&d.length===0)throw new r(`Usages cannot be empty when importing a private key.`,`SyntaxError`);return new e(p,u,{name:l},d,{keyObject:f})}function wrongKindForFormat(e,t){return new r(`Unable to export ${e.algorithm.name} ${e.type} key using ${t} format`,`InvalidAccessError`)}function cfrgExportKey(e,n){let i=keyObjectOf(n);switch(e){case`raw`:{if(n.type!==`public`)throw
|
|
1
|
+
import"./_virtual/_rolldown/runtime.js";import{CryptoKey as e}from"./crypto-key.js";import{base64urlDecode as t,toUint8Array as n}from"./util.js";import{DOMException as r}from"@gjsify/dom-exception";const i={Ed25519:{public:[`verify`],private:[`sign`]},X25519:{public:[],private:[`deriveKey`,`deriveBits`]}},a={Ed25519:[48,42,48,5,6,3,43,101,112,3,33,0],X25519:[48,42,48,5,6,3,43,101,110,3,33,0]};function cfrgName(e){let t=e.toUpperCase();if(t===`ED25519`)return`Ed25519`;if(t===`X25519`)return`X25519`}function assertUsages(e,t){for(let n of e)if(!t.includes(n))throw new r(`Unsupported key usage: ${n}`,`SyntaxError`)}function toArrayBuffer(e){return e.buffer.slice(e.byteOffset,e.byteOffset+e.byteLength)}function keyObjectOf(e){return e._handle.keyObject}function cfrgGenerateKey(t,n,a,o){assertUsages(o,[...i[n].public,...i[n].private]);let s=o.filter(e=>i[n].private.includes(e));if(s.length===0)throw new r(`Usages cannot be empty when creating a key.`,`SyntaxError`);let c=o.filter(e=>i[n].public.includes(e)),{publicKey:l,privateKey:u}=t.generateKeyPairSync(n.toLowerCase());return{publicKey:new e(`public`,!0,{name:n},c,{keyObject:l}),privateKey:new e(`private`,a,{name:n},s,{keyObject:u})}}function validateJwk(e,t,n,i){if(typeof e!=`object`||!e)throw new r(`Invalid keyData`,`DataError`);if(e.kty!==`OKP`)throw new r(`Invalid JWK "kty" Parameter`,`DataError`);if(e.crv!==t)throw new r(`JWK "crv" Parameter and algorithm name mismatch`,`DataError`);if(t===`Ed25519`&&e.alg!==void 0&&e.alg!==`Ed25519`&&e.alg!==`EdDSA`)throw new r(`JWK "alg" does not match the requested algorithm`,`DataError`);let a=t===`X25519`?`enc`:`sig`;if(i.length>0&&e.use!==void 0&&e.use!==a)throw new r(`Invalid JWK "use" Parameter`,`DataError`);if(e.key_ops!==void 0){if(!Array.isArray(e.key_ops))throw new r(`Invalid JWK "key_ops" Parameter`,`DataError`);for(let t of i)if(!e.key_ops.includes(t))throw new r(`Key operations and usage mismatch`,`DataError`)}if(e.ext===!1&&n)throw new r(`JWK "ext" Parameter and extractable mismatch`,`DataError`);if(typeof e.x!=`string`||e.d!==void 0&&typeof e.d!=`string`)throw new r(`Invalid keyData`,`DataError`)}function cfrgImportKey(o,s,c,l,u,d){let f,p,importOrDataError=e=>{try{return e()}catch(e){throw new r(`Invalid keyData: ${e.message}`,`DataError`)}};switch(s){case`raw`:{assertUsages(d,i[l].public);let e=n(c);if(e.length!==32)throw new r(`Invalid keyData`,`DataError`);let t=new Uint8Array(44);t.set(a[l],0),t.set(e,12),f=importOrDataError(()=>o.createPublicKey({key:t,format:`der`,type:`spki`})),p=`public`;break}case`spki`:{assertUsages(d,i[l].public);let e=n(c);f=importOrDataError(()=>o.createPublicKey({key:e,format:`der`,type:`spki`})),p=`public`;break}case`pkcs8`:{assertUsages(d,i[l].private);let e=n(c);f=importOrDataError(()=>o.createPrivateKey({key:e,format:`der`,type:`pkcs8`})),p=`private`;break}case`jwk`:{let e=c;if(validateJwk(e,l,u,d),p=e.d===void 0?`public`:`private`,assertUsages(d,i[l][p]),p===`private`){if(f=importOrDataError(()=>o.createPrivateKey({key:e,format:`jwk`})),o.createPublicKey(f).export({format:`jwk`}).x!==e.x)throw new r(`Invalid JWK: "x" does not match "d"`,`DataError`)}else{if(t(e.x).length!==32)throw new r(`Invalid keyData`,`DataError`);f=importOrDataError(()=>o.createPublicKey({key:e,format:`jwk`}))}break}default:throw new r(`Unsupported format: ${s}`,`NotSupportedError`)}if(f.asymmetricKeyType!==l.toLowerCase())throw new r(`Invalid key type`,`DataError`);if(p===`private`&&d.length===0)throw new r(`Usages cannot be empty when importing a private key.`,`SyntaxError`);return new e(p,u,{name:l},d,{keyObject:f})}function wrongKindForFormat(e,t){return new r(`Unable to export ${e.algorithm.name} ${e.type} key using ${t} format`,`InvalidAccessError`)}function cfrgExportKey(e,n){let i=keyObjectOf(n);switch(e){case`raw`:{if(n.type!==`public`)throw wrongKindForFormat(n,e);let r=i.export({format:`jwk`}).x;return toArrayBuffer(t(r))}case`spki`:if(n.type!==`public`)throw wrongKindForFormat(n,e);return toArrayBuffer(i.export({format:`der`,type:`spki`}));case`pkcs8`:if(n.type!==`private`)throw wrongKindForFormat(n,e);return toArrayBuffer(i.export({format:`der`,type:`pkcs8`}));case`jwk`:{let e=i.export({format:`jwk`}),t={kty:`OKP`,crv:n.algorithm.name,x:e.x};return n.type===`private`&&(t.d=e.d),n.algorithm.name===`Ed25519`&&(t.alg=`Ed25519`),t.key_ops=[...n.usages],t.ext=n.extractable,t}default:throw new r(`Unsupported export format: ${e}`,`NotSupportedError`)}}function ed25519Sign(e,t,n){if(t.type!==`private`)throw new r(`Key must be a private key`,`InvalidAccessError`);return toArrayBuffer(e.sign(null,n,keyObjectOf(t)))}function ed25519Verify(e,t,n,i){if(t.type!==`public`)throw new r(`Key must be a public key`,`InvalidAccessError`);return e.verify(null,i,keyObjectOf(t),n)}function x25519DeriveBits(t,n,i,a){if(n.type!==`private`)throw new r(`baseKey must be a private key`,`InvalidAccessError`);if(!(i instanceof e)||i.type!==`public`)throw new r(`algorithm.public must be a public key`,`InvalidAccessError`);if(i.algorithm.name!==n.algorithm.name)throw new r(`The public and private keys must be of the same type`,`InvalidAccessError`);let o;try{o=new Uint8Array(t.diffieHellman({privateKey:keyObjectOf(n),publicKey:keyObjectOf(i)}))}catch(e){throw new r(`Deriving bits failed: ${e.message}`,`OperationError`)}if(a==null)return toArrayBuffer(o);if(a>o.length*8)throw new r(`derived bit length is too small`,`OperationError`);let s=o.slice(0,Math.ceil(a/8));return a%8!=0&&(s[s.length-1]&=255<<8-a%8),toArrayBuffer(s)}export{cfrgExportKey,cfrgGenerateKey,cfrgImportKey,cfrgName,ed25519Sign,ed25519Verify,x25519DeriveBits};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gjsify/webcrypto",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.57.0",
|
|
4
4
|
"description": "W3C WebCrypto API (SubtleCrypto) for GJS using @gjsify/crypto primitives",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "lib/esm/index.js",
|
|
@@ -49,11 +49,11 @@
|
|
|
49
49
|
"crypto"
|
|
50
50
|
],
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@gjsify/dom-exception": "^0.
|
|
52
|
+
"@gjsify/dom-exception": "^0.57.0"
|
|
53
53
|
},
|
|
54
54
|
"devDependencies": {
|
|
55
|
-
"@gjsify/cli": "^0.
|
|
56
|
-
"@gjsify/unit": "^0.
|
|
55
|
+
"@gjsify/cli": "^0.57.0",
|
|
56
|
+
"@gjsify/unit": "^0.57.0",
|
|
57
57
|
"@types/node": "^25.9.2",
|
|
58
58
|
"typescript": "^6.0.3"
|
|
59
59
|
},
|