@gjsify/crypto 0.4.30 → 0.4.31

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/ecdh.js CHANGED
@@ -1 +1 @@
1
- import"./_virtual/_rolldown/runtime.js";import{modPow as e}from"./bigint-math.js";import{randomBytes as t}from"./random.js";import{Buffer as n}from"node:buffer";const r={secp256k1:{p:115792089237316195423570985008687907853269984665640564039457584007908834671663n,a:0n,b:7n,Gx:55066263022277343669578718895168534326250603453777594175500187360389116729240n,Gy:32670510020758816978083085130507043184471273380659243275938904335757337482424n,n:115792089237316195423570985008687907852837564279074904382605163141518161494337n,byteLength:32},prime256v1:{p:115792089210356248762697446949407573530086143415290314195533631308867097853951n,a:115792089210356248762697446949407573530086143415290314195533631308867097853951n-3n,b:41058363725152142129326129780047268409114441015993725554835256314039467401291n,Gx:48439561293906451759052585252797914202762949526041747995844080717082404635286n,Gy:36134250956749795798585127919587881956611106672985015071877198253568414405109n,n:115792089210356248762697446949407573529996955224135760342422259061068512044369n,byteLength:32},secp384r1:{p:39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319n,a:39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319n-3n,b:27580193559959705877849011840389048093056905856361568521428707301988689241309860865136260764883745107765439761230575n,Gx:26247035095799689268623156744566981891852923491109213387815615900925518854738050089022388053975719786650872476732087n,Gy:8325710961489029985546751289520108179287853048861315594709205902480503199884419224438643760392947333078086511627871n,n:39402006196394479212279040100143613805079739270465446667946905279627659399113263569398956308152294913554433653942643n,byteLength:48},secp521r1:{p:6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151n,a:6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151n-3n,b:1093849038073734274511112390766805569936207598951683748994586394495953116150735016013708737573759623248592132296706313309438452531591012912142327488478985984n,Gx:2661740802050217063228768716723360960729859168756973147706671368418802944996427808491545080627771902352094241225065558662157113545570916814161637315895999846n,Gy:3757180025770020463545507224491183603594455134769762486694567779615544477440556316691234405012945539562144444537289428522585666729196580810124344277578376784n,n:6864797660130609714981900799081393217269435300143305409394463459185543183397655394245057746333217197532963996371363321113864768612440380340372808892707005449n,byteLength:66}},i={secp256k1:`secp256k1`,prime256v1:`prime256v1`,secp256r1:`prime256v1`,"p-256":`prime256v1`,p256:`prime256v1`,secp384r1:`secp384r1`,"p-384":`secp384r1`,p384:`secp384r1`,secp521r1:`secp521r1`,"p-521":`secp521r1`,p521:`secp521r1`};function mod(e,t){let n=e%t;return n<0n?n+t:n}function modInverse(e,t){if(e=mod(e,t),e===0n)throw Error(`No modular inverse for zero`);let[n,r]=[e,t],[i,a]=[1n,0n];for(;r!==0n;){let e=n/r;[n,r]=[r,n-e*r],[i,a]=[a,i-e*a]}if(n!==1n)throw Error(`Modular inverse does not exist`);return mod(i,t)}function pointAdd(e,t,n){if(e===null)return t;if(t===null)return e;let{p:r}=n;if(e.x===t.x)return mod(e.y+t.y,r)===0n?null:pointDouble(e,n);let i=mod(t.x-e.x,r),a=mod(mod(t.y-e.y,r)*modInverse(i,r),r),o=mod(a*a-e.x-t.x,r);return{x:o,y:mod(a*(e.x-o)-e.y,r)}}function pointDouble(e,t){if(e===null)return null;let{a:n,p:r}=t;if(e.y===0n)return null;let i=mod(mod(3n*e.x*e.x+n,r)*modInverse(mod(2n*e.y,r),r),r),a=mod(i*i-2n*e.x,r);return{x:a,y:mod(i*(e.x-a)-e.y,r)}}function scalarMul(e,t,n){if(t===null||e===0n)return null;let{n:r}=n;if(e=mod(e,r),e===0n)return null;let i=null,a=t,o=e.toString(2);for(let e=0;e<o.length;e++)o[e]===`1`?(i=pointAdd(i,a,n),a=pointDouble(a,n)):(a=pointAdd(i,a,n),i=pointDouble(i,n));return i}function bigintToBuffer(e,t){let r=e.toString(16).padStart(t*2,`0`);return n.from(r,`hex`)}function bufferToBigint(e){let t=n.from(e).toString(`hex`);return t.length===0?0n:BigInt(`0x`+t)}function encodePublicKey(e,t,r=`uncompressed`){if(e===null)throw Error(`Cannot encode the point at infinity`);let i=bigintToBuffer(e.x,t);if(r===`compressed`){let t=e.y%2n==0n?2:3;return n.concat([n.from([t]),i])}let a=bigintToBuffer(e.y,t);if(r===`hybrid`){let t=e.y%2n==0n?6:7;return n.concat([n.from([t]),i,a])}return n.concat([n.from([4]),i,a])}function decodePublicKey(e,t){let r=n.from(e);if(r.length===0)throw Error(`Invalid public key: empty buffer`);let i=r[0],{p:a,a:o,b:s,byteLength:c}=t;if(i===4||i===6||i===7){if(r.length!==1+2*c)throw Error(`Invalid public key length: expected ${1+2*c} bytes, got ${r.length}`);let e=bufferToBigint(r.subarray(1,1+c)),t=bufferToBigint(r.subarray(1+c));if(mod(t*t,a)!==mod(e*e*e+o*e+s,a))throw Error(`Invalid public key: point is not on the curve`);return{x:e,y:t}}if(i===2||i===3){if(r.length!==1+c)throw Error(`Invalid public key length: expected ${1+c} bytes, got ${r.length}`);let e=bufferToBigint(r.subarray(1,1+c)),t=sqrtMod(mod(e*e*e+o*e+s,a),a);if(t===null)throw Error(`Invalid public key: no valid y coordinate for the given x`);let n=i===3;return{x:e,y:t%2n!=0n===n?t:mod(a-t,a)}}throw Error(`Invalid public key prefix: 0x${i.toString(16).padStart(2,`0`)}`)}function sqrtMod(t,n){if(t=mod(t,n),t===0n)return 0n;if(e(t,(n-1n)/2n,n)!==1n)return null;if(mod(n,4n)===3n)return e(t,(n+1n)/4n,n);let r=0n,i=n-1n;for(;mod(i,2n)===0n;)i/=2n,r++;let a=2n;for(;e(a,(n-1n)/2n,n)!==n-1n;)a++;let o=r,s=e(a,i,n),c=e(t,i,n),l=e(t,(i+1n)/2n,n);for(;;){if(c===0n)return 0n;if(c===1n)return l;let t=1n,r=mod(c*c,n);for(;r!==1n;)r=mod(r*r,n),t++;let i=e(s,e(2n,o-t-1n,n-1n),n);o=t,s=mod(i*i,n),c=mod(c*s,n),l=mod(l*i,n)}}function toBuffer(e,t){if(n.isBuffer(e))return e;if(e instanceof Uint8Array)return n.from(e);if(typeof e==`string`)return n.from(e,t||`utf8`);if(ArrayBuffer.isView(e))return n.from(e.buffer,e.byteOffset,e.byteLength);throw TypeError(`The "key" argument must be of type string or an instance of Buffer, TypedArray, or DataView.`)}function formatReturnValue(e,t){return t?e.toString(t):e}function resolveCurve(e){let t=i[e.toLowerCase()];if(!t)throw Error(`Unsupported curve: ${e}`);let n=r[t];if(!n)throw Error(`Unsupported curve: ${e}`);return{canonical:t,params:n}}var ECDH=class{_curve;_curveName;_privateKey=null;_publicKey=null;constructor(e){let t=resolveCurve(e);this._curve=t.params,this._curveName=t.canonical}generateKeys(e,n){let{n:r,byteLength:i}=this._curve,a;do a=bufferToBigint(t(i));while(a===0n||a>=r);this._privateKey=a;let o={x:this._curve.Gx,y:this._curve.Gy};return this._publicKey=scalarMul(a,o,this._curve),this.getPublicKey(e,n)}computeSecret(e,t,n){if(this._privateKey===null)throw Error(`ECDH key not generated. Call generateKeys() or setPrivateKey() first.`);let r=decodePublicKey(toBuffer(e,t),this._curve);if(r===null)throw Error(`Invalid public key: point at infinity`);let i=scalarMul(this._privateKey,r,this._curve);if(i===null)throw Error(`Shared secret computation resulted in the point at infinity`);return formatReturnValue(bigintToBuffer(i.x,this._curve.byteLength),n)}getPublicKey(e,t){if(this._publicKey===null)throw Error(`ECDH key not generated. Call generateKeys() or setPrivateKey() first.`);return formatReturnValue(encodePublicKey(this._publicKey,this._curve.byteLength,t||`uncompressed`),e)}getPrivateKey(e){if(this._privateKey===null)throw Error(`ECDH key not generated. Call generateKeys() or setPrivateKey() first.`);return formatReturnValue(bigintToBuffer(this._privateKey,this._curve.byteLength),e)}setPublicKey(e,t){let n=toBuffer(e,t);this._publicKey=decodePublicKey(n,this._curve)}setPrivateKey(e,t){let n=bufferToBigint(toBuffer(e,t));if(n===0n||n>=this._curve.n)throw Error(`Private key is out of range [1, n-1]`);this._privateKey=n;let r={x:this._curve.Gx,y:this._curve.Gy};this._publicKey=scalarMul(n,r,this._curve)}};function createECDH(e){return new ECDH(e)}function getCurves(){return[`secp256k1`,`prime256v1`,`secp256r1`,`secp384r1`,`secp521r1`]}export{r as CURVES,i as CURVE_ALIASES,createECDH,getCurves,mod,modInverse,pointAdd,scalarMul};
1
+ import"./_virtual/_rolldown/runtime.js";import{modPow as e}from"./bigint-math.js";import{randomBytes as t}from"./random.js";import{Buffer as n}from"node:buffer";const r={secp256k1:{p:115792089237316195423570985008687907853269984665640564039457584007908834671663n,a:0n,b:7n,Gx:55066263022277343669578718895168534326250603453777594175500187360389116729240n,Gy:32670510020758816978083085130507043184471273380659243275938904335757337482424n,n:115792089237316195423570985008687907852837564279074904382605163141518161494337n,byteLength:32},prime256v1:{p:115792089210356248762697446949407573530086143415290314195533631308867097853951n,a:115792089210356248762697446949407573530086143415290314195533631308867097853951n-3n,b:41058363725152142129326129780047268409114441015993725554835256314039467401291n,Gx:48439561293906451759052585252797914202762949526041747995844080717082404635286n,Gy:36134250956749795798585127919587881956611106672985015071877198253568414405109n,n:115792089210356248762697446949407573529996955224135760342422259061068512044369n,byteLength:32},secp384r1:{p:39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319n,a:39402006196394479212279040100143613805079739270465446667948293404245721771496870329047266088258938001861606973112319n-3n,b:27580193559959705877849011840389048093056905856361568521428707301988689241309860865136260764883745107765439761230575n,Gx:26247035095799689268623156744566981891852923491109213387815615900925518854738050089022388053975719786650872476732087n,Gy:8325710961489029985546751289520108179287853048861315594709205902480503199884419224438643760392947333078086511627871n,n:39402006196394479212279040100143613805079739270465446667946905279627659399113263569398956308152294913554433653942643n,byteLength:48},secp521r1:{p:6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151n,a:6864797660130609714981900799081393217269435300143305409394463459185543183397656052122559640661454554977296311391480858037121987999716643812574028291115057151n-3n,b:1093849038073734274511112390766805569936207598951683748994586394495953116150735016013708737573759623248592132296706313309438452531591012912142327488478985984n,Gx:2661740802050217063228768716723360960729859168756973147706671368418802944996427808491545080627771902352094241225065558662157113545570916814161637315895999846n,Gy:3757180025770020463545507224491183603594455134769762486694567779615544477440556316691234405012945539562144444537289428522585666729196580810124344277578376784n,n:6864797660130609714981900799081393217269435300143305409394463459185543183397655394245057746333217197532963996371363321113864768612440380340372808892707005449n,byteLength:66}},i={secp256k1:`secp256k1`,prime256v1:`prime256v1`,secp256r1:`prime256v1`,"p-256":`prime256v1`,p256:`prime256v1`,secp384r1:`secp384r1`,"p-384":`secp384r1`,p384:`secp384r1`,secp521r1:`secp521r1`,"p-521":`secp521r1`,p521:`secp521r1`};function mod(e,t){let n=e%t;return n<0n?n+t:n}function modInverse(e,t){if(e=mod(e,t),e===0n)throw Error(`No modular inverse for zero`);let[n,r]=[e,t],[i,a]=[1n,0n];for(;r!==0n;){let e=n/r;[n,r]=[r,n-e*r],[i,a]=[a,i-e*a]}if(n!==1n)throw Error(`Modular inverse does not exist`);return mod(i,t)}function pointAdd(e,t,n){if(e===null)return t;if(t===null)return e;let{p:r}=n;if(e.x===t.x)return mod(e.y+t.y,r)===0n?null:pointDouble(e,n);let i=mod(t.x-e.x,r),a=mod(mod(t.y-e.y,r)*modInverse(i,r),r),o=mod(a*a-e.x-t.x,r);return{x:o,y:mod(a*(e.x-o)-e.y,r)}}function pointDouble(e,t){if(e===null)return null;let{a:n,p:r}=t;if(e.y===0n)return null;let i=mod(mod(3n*e.x*e.x+n,r)*modInverse(mod(2n*e.y,r),r),r),a=mod(i*i-2n*e.x,r);return{x:a,y:mod(i*(e.x-a)-e.y,r)}}function scalarMul(e,t,n){if(t===null||e===0n)return null;let{n:r}=n;if(e=mod(e,r),e===0n)return null;let i=null,a=t,o=e.toString(2);for(let e=0;e<o.length;e++)o[e]===`1`?(i=pointAdd(i,a,n),a=pointDouble(a,n)):(a=pointAdd(i,a,n),i=pointDouble(i,n));return i}function bigintToBuffer(e,t){let r=e.toString(16).padStart(t*2,`0`);return n.from(r,`hex`)}function bufferToBigint(e){let t=n.from(e).toString(`hex`);return t.length===0?0n:BigInt(`0x`+t)}function encodePublicKey(e,t,r=`uncompressed`){if(e===null)throw Error(`Cannot encode the point at infinity`);let i=bigintToBuffer(e.x,t);if(r===`compressed`){let t=e.y%2n==0n?2:3;return n.concat([n.from([t]),i])}let a=bigintToBuffer(e.y,t);if(r===`hybrid`){let t=e.y%2n==0n?6:7;return n.concat([n.from([t]),i,a])}return n.concat([n.from([4]),i,a])}function decodePublicKey(e,t){let r=n.from(e);if(r.length===0)throw Error(`Invalid public key: empty buffer`);let i=r[0],{p:a,a:o,b:s,byteLength:c}=t;if(i===4||i===6||i===7){if(r.length!==1+2*c)throw Error(`Invalid public key length: expected ${1+2*c} bytes, got ${r.length}`);let e=bufferToBigint(r.subarray(1,1+c)),t=bufferToBigint(r.subarray(1+c));if(mod(t*t,a)!==mod(e*e*e+o*e+s,a))throw Error(`Invalid public key: point is not on the curve`);return{x:e,y:t}}if(i===2||i===3){if(r.length!==1+c)throw Error(`Invalid public key length: expected ${1+c} bytes, got ${r.length}`);let e=bufferToBigint(r.subarray(1,1+c)),t=sqrtMod(mod(e*e*e+o*e+s,a),a);if(t===null)throw Error(`Invalid public key: no valid y coordinate for the given x`);let n=i===3;return{x:e,y:t%2n!=0n===n?t:mod(a-t,a)}}throw Error(`Invalid public key prefix: 0x${i.toString(16).padStart(2,`0`)}`)}function sqrtMod(t,n){if(t=mod(t,n),t===0n)return 0n;if(e(t,(n-1n)/2n,n)!==1n)return null;if(mod(n,4n)===3n)return e(t,(n+1n)/4n,n);let r=0n,i=n-1n;for(;mod(i,2n)===0n;)i/=2n,r++;let a=2n;for(;e(a,(n-1n)/2n,n)!==n-1n;)a++;let o=r,s=e(a,i,n),c=e(t,i,n),l=e(t,(i+1n)/2n,n);for(;;){if(c===0n)return 0n;if(c===1n)return l;let t=1n,r=mod(c*c,n);for(;r!==1n;)r=mod(r*r,n),t++;let i=e(s,e(2n,o-t-1n,n-1n),n);o=t,s=mod(i*i,n),c=mod(c*s,n),l=mod(l*i,n)}}function toBuffer(e,t){if(n.isBuffer(e))return e;if(e instanceof Uint8Array)return n.from(e);if(typeof e==`string`)return n.from(e,t||`utf8`);if(ArrayBuffer.isView(e))return n.from(e.buffer,e.byteOffset,e.byteLength);throw TypeError(`The "key" argument must be of type string or an instance of Buffer, TypedArray, or DataView.`)}function formatReturnValue(e,t){return t?e.toString(t):e}function resolveCurve(e){let t=i[e.toLowerCase()];if(!t)throw Error(`Unsupported curve: ${e}`);let n=r[t];if(!n)throw Error(`Unsupported curve: ${e}`);return{canonical:t,params:n}}var ECDH=class{_curve;_curveName;_privateKey=null;_publicKey=null;constructor(e){let t=resolveCurve(e);this._curve=t.params,this._curveName=t.canonical}generateKeys(e,n){let{n:r,byteLength:i}=this._curve,a;do a=bufferToBigint(t(i));while(a===0n||a>=r);this._privateKey=a;let o={x:this._curve.Gx,y:this._curve.Gy};return this._publicKey=scalarMul(a,o,this._curve),e===void 0?this.getPublicKey():this.getPublicKey(e,n)}computeSecret(e,t,n){if(this._privateKey===null)throw Error(`ECDH key not generated. Call generateKeys() or setPrivateKey() first.`);let r=decodePublicKey(toBuffer(e,t),this._curve);if(r===null)throw Error(`Invalid public key: point at infinity`);let i=scalarMul(this._privateKey,r,this._curve);if(i===null)throw Error(`Shared secret computation resulted in the point at infinity`);return formatReturnValue(bigintToBuffer(i.x,this._curve.byteLength),n)}getPublicKey(e,t){if(this._publicKey===null)throw Error(`ECDH key not generated. Call generateKeys() or setPrivateKey() first.`);return formatReturnValue(encodePublicKey(this._publicKey,this._curve.byteLength,t||`uncompressed`),e)}getPrivateKey(e){if(this._privateKey===null)throw Error(`ECDH key not generated. Call generateKeys() or setPrivateKey() first.`);return formatReturnValue(bigintToBuffer(this._privateKey,this._curve.byteLength),e)}setPublicKey(e,t){let n=toBuffer(e,t);this._publicKey=decodePublicKey(n,this._curve)}setPrivateKey(e,t){let n=bufferToBigint(toBuffer(e,t));if(n===0n||n>=this._curve.n)throw Error(`Private key is out of range [1, n-1]`);this._privateKey=n;let r={x:this._curve.Gx,y:this._curve.Gy};this._publicKey=scalarMul(n,r,this._curve)}};function createECDH(e){return new ECDH(e)}function getCurves(){return[`secp256k1`,`prime256v1`,`secp256r1`,`secp384r1`,`secp521r1`]}export{r as CURVES,i as CURVE_ALIASES,createECDH,getCurves,mod,modInverse,pointAdd,scalarMul};
@@ -16,7 +16,7 @@ export declare class Hash extends Transform {
16
16
  digest(encoding?: BufferEncoding): Buffer | string;
17
17
  /** Copy this hash to a new Hash object. */
18
18
  copy(): Hash;
19
- _transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback): void;
19
+ _transform(chunk: string | Buffer | Uint8Array, encoding: BufferEncoding, callback: TransformCallback): void;
20
20
  _flush(callback: TransformCallback): void;
21
21
  }
22
22
  /** Get the list of supported hash algorithms. */
@@ -15,6 +15,6 @@ export declare class Hmac extends Transform {
15
15
  update(data: string | Buffer | Uint8Array, inputEncoding?: BufferEncoding): this;
16
16
  /** Calculate the HMAC digest. */
17
17
  digest(encoding?: BufferEncoding): Buffer | string;
18
- _transform(chunk: any, encoding: BufferEncoding, callback: TransformCallback): void;
18
+ _transform(chunk: string | Buffer | Uint8Array, encoding: BufferEncoding, callback: TransformCallback): void;
19
19
  _flush(callback: TransformCallback): void;
20
20
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gjsify/crypto",
3
- "version": "0.4.30",
3
+ "version": "0.4.31",
4
4
  "description": "Node.js crypto module for Gjs",
5
5
  "type": "module",
6
6
  "module": "lib/esm/index.js",
@@ -33,16 +33,16 @@
33
33
  "crypto"
34
34
  ],
35
35
  "devDependencies": {
36
- "@gjsify/cli": "^0.4.30",
37
- "@gjsify/unit": "^0.4.30",
36
+ "@gjsify/cli": "^0.4.31",
37
+ "@gjsify/unit": "^0.4.31",
38
38
  "@types/diffie-hellman": "^5.0.3",
39
39
  "@types/node": "^25.9.1",
40
40
  "typescript": "^6.0.3"
41
41
  },
42
42
  "dependencies": {
43
43
  "@girs/glib-2.0": "2.88.0-4.0.1",
44
- "@gjsify/buffer": "^0.4.30",
45
- "@gjsify/stream": "^0.4.30",
46
- "@gjsify/utils": "^0.4.30"
44
+ "@gjsify/buffer": "^0.4.31",
45
+ "@gjsify/stream": "^0.4.31",
46
+ "@gjsify/utils": "^0.4.31"
47
47
  }
48
48
  }