@gjsify/crypto 0.4.21 → 0.4.22
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/aes-primitives.js +1 -0
- package/lib/esm/cipher-utils.js +1 -0
- package/lib/esm/cipher.js +1 -1
- package/lib/esm/dh.js +1 -1
- package/lib/esm/hash.js +1 -1
- package/lib/esm/hmac.js +1 -1
- package/lib/types/aes-primitives.d.ts +15 -0
- package/lib/types/cipher-utils.d.ts +16 -0
- package/lib/types/cipher.d.ts +1 -5
- package/package.json +8 -8
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import"./_virtual/_rolldown/runtime.js";const e=new Uint8Array([99,124,119,123,242,107,111,197,48,1,103,43,254,215,171,118,202,130,201,125,250,89,71,240,173,212,162,175,156,164,114,192,183,253,147,38,54,63,247,204,52,165,229,241,113,216,49,21,4,199,35,195,24,150,5,154,7,18,128,226,235,39,178,117,9,131,44,26,27,110,90,160,82,59,214,179,41,227,47,132,83,209,0,237,32,252,177,91,106,203,190,57,74,76,88,207,208,239,170,251,67,77,51,133,69,249,2,127,80,60,159,168,81,163,64,143,146,157,56,245,188,182,218,33,16,255,243,210,205,12,19,236,95,151,68,23,196,167,126,61,100,93,25,115,96,129,79,220,34,42,144,136,70,238,184,20,222,94,11,219,224,50,58,10,73,6,36,92,194,211,172,98,145,149,228,121,231,200,55,109,141,213,78,169,108,86,244,234,101,122,174,8,186,120,37,46,28,166,180,198,232,221,116,31,75,189,139,138,112,62,181,102,72,3,246,14,97,53,87,185,134,193,29,158,225,248,152,17,105,217,142,148,155,30,135,233,206,85,40,223,140,161,137,13,191,230,66,104,65,153,45,15,176,84,187,22]),t=new Uint8Array([82,9,106,213,48,54,165,56,191,64,163,158,129,243,215,251,124,227,57,130,155,47,255,135,52,142,67,68,196,222,233,203,84,123,148,50,166,194,35,61,238,76,149,11,66,250,195,78,8,46,161,102,40,217,36,178,118,91,162,73,109,139,209,37,114,248,246,100,134,104,152,22,212,164,92,204,93,101,182,146,108,112,72,80,253,237,185,218,94,21,70,87,167,141,157,132,144,216,171,0,140,188,211,10,247,228,88,5,184,179,69,6,208,44,30,143,202,63,15,2,193,175,189,3,1,19,138,107,58,145,17,65,79,103,220,234,151,242,207,206,240,180,230,115,150,172,116,34,231,173,53,133,226,249,55,232,28,117,223,110,71,241,26,113,29,41,197,137,111,183,98,14,170,24,190,27,252,86,62,75,198,210,121,32,154,219,192,254,120,205,90,244,31,221,168,51,136,7,199,49,177,18,16,89,39,128,236,95,96,81,127,169,25,181,74,13,45,229,122,159,147,201,156,239,160,224,59,77,174,42,245,176,200,235,187,60,131,83,153,97,23,43,4,126,186,119,214,38,225,105,20,99,85,33,12,125]),n=[1,2,4,8,16,32,64,128,27,54];function gmul(e,t){let n=0;for(let r=0;r<8;r++){t&1&&(n^=e);let r=e&128;e=e<<1&255,r&&(e^=27),t>>=1}return n}function keyExpansion(t){let r=t.length/4,i=r+6,a=4*(i+1),o=Array(a);for(let e=0;e<r;e++)o[e]=new Uint8Array([t[4*e],t[4*e+1],t[4*e+2],t[4*e+3]]);for(let t=r;t<a;t++){let i=new Uint8Array(o[t-1]);t%r===0?i=new Uint8Array([e[i[1]]^n[t/r-1],e[i[2]],e[i[3]],e[i[0]]]):r>6&&t%r===4&&(i=new Uint8Array([e[i[0]],e[i[1]],e[i[2]],e[i[3]]])),o[t]=new Uint8Array(4);for(let e=0;e<4;e++)o[t][e]=o[t-r][e]^i[e]}let s=[];for(let e=0;e<=i;e++){let t=new Uint8Array(16);for(let n=0;n<4;n++)t[4*n]=o[4*e+n][0],t[4*n+1]=o[4*e+n][1],t[4*n+2]=o[4*e+n][2],t[4*n+3]=o[4*e+n][3];s.push(t)}return s}function aesEncryptBlock(t,n){let r=new Uint8Array(t),i=n.length-1;for(let e=0;e<16;e++)r[e]^=n[0][e];for(let t=1;t<i;t++){for(let t=0;t<16;t++)r[t]=e[r[t]];let i=r[1];r[1]=r[5],r[5]=r[9],r[9]=r[13],r[13]=i;let a=r[2],o=r[6];r[2]=r[10],r[6]=r[14],r[10]=a,r[14]=o;let s=r[15];r[15]=r[11],r[11]=r[7],r[7]=r[3],r[3]=s;for(let e=0;e<4;e++){let t=e*4,n=r[t],i=r[t+1],a=r[t+2],o=r[t+3];r[t]=gmul(2,n)^gmul(3,i)^a^o,r[t+1]=n^gmul(2,i)^gmul(3,a)^o,r[t+2]=n^i^gmul(2,a)^gmul(3,o),r[t+3]=gmul(3,n)^i^a^gmul(2,o)}for(let e=0;e<16;e++)r[e]^=n[t][e]}for(let t=0;t<16;t++)r[t]=e[r[t]];let a=r[1];r[1]=r[5],r[5]=r[9],r[9]=r[13],r[13]=a;let o=r[2],s=r[6];r[2]=r[10],r[6]=r[14],r[10]=o,r[14]=s;let c=r[15];r[15]=r[11],r[11]=r[7],r[7]=r[3],r[3]=c;for(let e=0;e<16;e++)r[e]^=n[i][e];return r}function aesDecryptBlock(e,n){let r=new Uint8Array(e),i=n.length-1;for(let e=0;e<16;e++)r[e]^=n[i][e];for(let e=i-1;e>0;e--){let i=r[13];r[13]=r[9],r[9]=r[5],r[5]=r[1],r[1]=i;let a=r[10],o=r[14];r[10]=r[2],r[14]=r[6],r[2]=a,r[6]=o;let s=r[3];r[3]=r[7],r[7]=r[11],r[11]=r[15],r[15]=s;for(let e=0;e<16;e++)r[e]=t[r[e]];for(let t=0;t<16;t++)r[t]^=n[e][t];for(let e=0;e<4;e++){let t=e*4,n=r[t],i=r[t+1],a=r[t+2],o=r[t+3];r[t]=gmul(14,n)^gmul(11,i)^gmul(13,a)^gmul(9,o),r[t+1]=gmul(9,n)^gmul(14,i)^gmul(11,a)^gmul(13,o),r[t+2]=gmul(13,n)^gmul(9,i)^gmul(14,a)^gmul(11,o),r[t+3]=gmul(11,n)^gmul(13,i)^gmul(9,a)^gmul(14,o)}}let a=r[13];r[13]=r[9],r[9]=r[5],r[5]=r[1],r[1]=a;let o=r[10],s=r[14];r[10]=r[2],r[14]=r[6],r[2]=o,r[6]=s;let c=r[3];r[3]=r[7],r[7]=r[11],r[11]=r[15],r[15]=c;for(let e=0;e<16;e++)r[e]=t[r[e]];for(let e=0;e<16;e++)r[e]^=n[0][e];return r}function incrementCounter(e){for(let t=15;t>=0&&++e[t]===0;t--);}function gcmIncrementCounter(e){for(let t=15;t>=12&&++e[t]===0;t--);}function gfMul(e,t){let n=new Uint8Array(16),r=new Uint8Array(e);for(let e=0;e<128;e++){if(t[e>>>3]&1<<7-(e&7))for(let e=0;e<16;e++)n[e]^=r[e];let i=r[15]&1;for(let e=15;e>0;e--)r[e]=r[e]>>>1|(r[e-1]&1)<<7;r[0]>>>=1,i&&(r[0]^=225)}return n}function ghash(e,t,n){let r=new Uint8Array(16),i=Math.ceil(t.length/16)||0;for(let n=0;n<i;n++){let i=n*16,a=Math.min(i+16,t.length);for(let e=0;e<16;e++){let n=i+e;n<a&&(r[e]^=t[n])}let o=gfMul(r,e);r.set(o)}let a=Math.ceil(n.length/16)||0;for(let t=0;t<a;t++){let i=t*16,a=Math.min(i+16,n.length);for(let e=0;e<16;e++){let t=i+e;t<a&&(r[e]^=n[t])}let o=gfMul(r,e);r.set(o)}let o=new Uint8Array(16),s=t.length*8,c=n.length*8,l=Math.floor(s/4294967296),u=s>>>0;o[0]=l>>>24&255,o[1]=l>>>16&255,o[2]=l>>>8&255,o[3]=l&255,o[4]=u>>>24&255,o[5]=u>>>16&255,o[6]=u>>>8&255,o[7]=u&255;let d=Math.floor(c/4294967296),f=c>>>0;o[8]=d>>>24&255,o[9]=d>>>16&255,o[10]=d>>>8&255,o[11]=d&255,o[12]=f>>>24&255,o[13]=f>>>16&255,o[14]=f>>>8&255,o[15]=f&255;for(let e=0;e<16;e++)r[e]^=o[e];let p=gfMul(r,e);return r.set(p),r}export{aesDecryptBlock,aesEncryptBlock,gcmIncrementCounter,ghash,incrementCounter,keyExpansion};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import"./_virtual/_rolldown/runtime.js";import{Buffer as e}from"node:buffer";function parseAlgorithm(e){let t=e.toLowerCase().match(/^aes-(128|192|256)-(cbc|ctr|ecb|cfb|ofb|gcm)$/);if(!t)throw Error(`Unsupported cipher algorithm: ${e}`);let n=parseInt(t[1]),r=t[2];return{keySize:n/8,ivSize:r===`ecb`?0:r===`gcm`?12:16,mode:r}}function toBuffer(t,n){return typeof t==`string`?e.from(t,n||`utf8`):e.from(t)}function encodeOutput(t,n){return n?e.from(t).toString(n):e.from(t)}function incompleteUtf8Tail(e){if(e.length===0)return 0;let t=e.length;for(let n=1;n<=Math.min(4,t);n++){let r=e[t-n];if(!(r&128))return 0;if((r&192)==128)continue;let i;if((r&224)==192)i=2;else if((r&240)==224)i=3;else if((r&248)==240)i=4;else return 0;return n<i?n:0}return 0}function pkcs7Pad(e){let t=16-e.length%16,n=new Uint8Array(e.length+t);n.set(e);for(let r=e.length;r<n.length;r++)n[r]=t;return n}function pkcs7Unpad(e){if(e.length===0||e.length%16!=0)throw Error(`bad decrypt`);let t=e[e.length-1];if(t===0||t>16)throw Error(`bad decrypt`);for(let n=e.length-t;n<e.length;n++)if(e[n]!==t)throw Error(`bad decrypt`);return new Uint8Array(e.slice(0,e.length-t))}export{encodeOutput,incompleteUtf8Tail,parseAlgorithm,pkcs7Pad,pkcs7Unpad,toBuffer};
|
package/lib/esm/cipher.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"./_virtual/_rolldown/runtime.js";import{Buffer as e}from"node:buffer";const t=new Uint8Array([99,124,119,123,242,107,111,197,48,1,103,43,254,215,171,118,202,130,201,125,250,89,71,240,173,212,162,175,156,164,114,192,183,253,147,38,54,63,247,204,52,165,229,241,113,216,49,21,4,199,35,195,24,150,5,154,7,18,128,226,235,39,178,117,9,131,44,26,27,110,90,160,82,59,214,179,41,227,47,132,83,209,0,237,32,252,177,91,106,203,190,57,74,76,88,207,208,239,170,251,67,77,51,133,69,249,2,127,80,60,159,168,81,163,64,143,146,157,56,245,188,182,218,33,16,255,243,210,205,12,19,236,95,151,68,23,196,167,126,61,100,93,25,115,96,129,79,220,34,42,144,136,70,238,184,20,222,94,11,219,224,50,58,10,73,6,36,92,194,211,172,98,145,149,228,121,231,200,55,109,141,213,78,169,108,86,244,234,101,122,174,8,186,120,37,46,28,166,180,198,232,221,116,31,75,189,139,138,112,62,181,102,72,3,246,14,97,53,87,185,134,193,29,158,225,248,152,17,105,217,142,148,155,30,135,233,206,85,40,223,140,161,137,13,191,230,66,104,65,153,45,15,176,84,187,22]),n=new Uint8Array([82,9,106,213,48,54,165,56,191,64,163,158,129,243,215,251,124,227,57,130,155,47,255,135,52,142,67,68,196,222,233,203,84,123,148,50,166,194,35,61,238,76,149,11,66,250,195,78,8,46,161,102,40,217,36,178,118,91,162,73,109,139,209,37,114,248,246,100,134,104,152,22,212,164,92,204,93,101,182,146,108,112,72,80,253,237,185,218,94,21,70,87,167,141,157,132,144,216,171,0,140,188,211,10,247,228,88,5,184,179,69,6,208,44,30,143,202,63,15,2,193,175,189,3,1,19,138,107,58,145,17,65,79,103,220,234,151,242,207,206,240,180,230,115,150,172,116,34,231,173,53,133,226,249,55,232,28,117,223,110,71,241,26,113,29,41,197,137,111,183,98,14,170,24,190,27,252,86,62,75,198,210,121,32,154,219,192,254,120,205,90,244,31,221,168,51,136,7,199,49,177,18,16,89,39,128,236,95,96,81,127,169,25,181,74,13,45,229,122,159,147,201,156,239,160,224,59,77,174,42,245,176,200,235,187,60,131,83,153,97,23,43,4,126,186,119,214,38,225,105,20,99,85,33,12,125]),r=[1,2,4,8,16,32,64,128,27,54];function gmul(e,t){let n=0;for(let r=0;r<8;r++){t&1&&(n^=e);let r=e&128;e=e<<1&255,r&&(e^=27),t>>=1}return n}function keyExpansion(e){let n=e.length/4,i=n+6,a=4*(i+1),o=Array(a);for(let t=0;t<n;t++)o[t]=new Uint8Array([e[4*t],e[4*t+1],e[4*t+2],e[4*t+3]]);for(let e=n;e<a;e++){let i=new Uint8Array(o[e-1]);e%n===0?i=new Uint8Array([t[i[1]]^r[e/n-1],t[i[2]],t[i[3]],t[i[0]]]):n>6&&e%n===4&&(i=new Uint8Array([t[i[0]],t[i[1]],t[i[2]],t[i[3]]])),o[e]=new Uint8Array(4);for(let t=0;t<4;t++)o[e][t]=o[e-n][t]^i[t]}let s=[];for(let e=0;e<=i;e++){let t=new Uint8Array(16);for(let n=0;n<4;n++)t[4*n]=o[4*e+n][0],t[4*n+1]=o[4*e+n][1],t[4*n+2]=o[4*e+n][2],t[4*n+3]=o[4*e+n][3];s.push(t)}return s}function aesEncryptBlock(e,n){let r=new Uint8Array(e),i=n.length-1;for(let e=0;e<16;e++)r[e]^=n[0][e];for(let e=1;e<i;e++){for(let e=0;e<16;e++)r[e]=t[r[e]];let i=r[1];r[1]=r[5],r[5]=r[9],r[9]=r[13],r[13]=i;let a=r[2],o=r[6];r[2]=r[10],r[6]=r[14],r[10]=a,r[14]=o;let s=r[15];r[15]=r[11],r[11]=r[7],r[7]=r[3],r[3]=s;for(let e=0;e<4;e++){let t=e*4,n=r[t],i=r[t+1],a=r[t+2],o=r[t+3];r[t]=gmul(2,n)^gmul(3,i)^a^o,r[t+1]=n^gmul(2,i)^gmul(3,a)^o,r[t+2]=n^i^gmul(2,a)^gmul(3,o),r[t+3]=gmul(3,n)^i^a^gmul(2,o)}for(let t=0;t<16;t++)r[t]^=n[e][t]}for(let e=0;e<16;e++)r[e]=t[r[e]];let a=r[1];r[1]=r[5],r[5]=r[9],r[9]=r[13],r[13]=a;let o=r[2],s=r[6];r[2]=r[10],r[6]=r[14],r[10]=o,r[14]=s;let c=r[15];r[15]=r[11],r[11]=r[7],r[7]=r[3],r[3]=c;for(let e=0;e<16;e++)r[e]^=n[i][e];return r}function aesDecryptBlock(e,t){let r=new Uint8Array(e),i=t.length-1;for(let e=0;e<16;e++)r[e]^=t[i][e];for(let e=i-1;e>0;e--){let i=r[13];r[13]=r[9],r[9]=r[5],r[5]=r[1],r[1]=i;let a=r[10],o=r[14];r[10]=r[2],r[14]=r[6],r[2]=a,r[6]=o;let s=r[3];r[3]=r[7],r[7]=r[11],r[11]=r[15],r[15]=s;for(let e=0;e<16;e++)r[e]=n[r[e]];for(let n=0;n<16;n++)r[n]^=t[e][n];for(let e=0;e<4;e++){let t=e*4,n=r[t],i=r[t+1],a=r[t+2],o=r[t+3];r[t]=gmul(14,n)^gmul(11,i)^gmul(13,a)^gmul(9,o),r[t+1]=gmul(9,n)^gmul(14,i)^gmul(11,a)^gmul(13,o),r[t+2]=gmul(13,n)^gmul(9,i)^gmul(14,a)^gmul(11,o),r[t+3]=gmul(11,n)^gmul(13,i)^gmul(9,a)^gmul(14,o)}}let a=r[13];r[13]=r[9],r[9]=r[5],r[5]=r[1],r[1]=a;let o=r[10],s=r[14];r[10]=r[2],r[14]=r[6],r[2]=o,r[6]=s;let c=r[3];r[3]=r[7],r[7]=r[11],r[11]=r[15],r[15]=c;for(let e=0;e<16;e++)r[e]=n[r[e]];for(let e=0;e<16;e++)r[e]^=t[0][e];return r}function incrementCounter(e){for(let t=15;t>=0&&++e[t]===0;t--);}function gcmIncrementCounter(e){for(let t=15;t>=12&&++e[t]===0;t--);}function gfMul(e,t){let n=new Uint8Array(16),r=new Uint8Array(e);for(let e=0;e<128;e++){if(t[e>>>3]&1<<7-(e&7))for(let e=0;e<16;e++)n[e]^=r[e];let i=r[15]&1;for(let e=15;e>0;e--)r[e]=r[e]>>>1|(r[e-1]&1)<<7;r[0]>>>=1,i&&(r[0]^=225)}return n}function ghash(e,t,n){let r=new Uint8Array(16),i=Math.ceil(t.length/16)||0;for(let n=0;n<i;n++){let i=n*16,a=Math.min(i+16,t.length);for(let e=0;e<16;e++){let n=i+e;n<a&&(r[e]^=t[n])}let o=gfMul(r,e);r.set(o)}let a=Math.ceil(n.length/16)||0;for(let t=0;t<a;t++){let i=t*16,a=Math.min(i+16,n.length);for(let e=0;e<16;e++){let t=i+e;t<a&&(r[e]^=n[t])}let o=gfMul(r,e);r.set(o)}let o=new Uint8Array(16),s=t.length*8,c=n.length*8,l=Math.floor(s/4294967296),u=s>>>0;o[0]=l>>>24&255,o[1]=l>>>16&255,o[2]=l>>>8&255,o[3]=l&255,o[4]=u>>>24&255,o[5]=u>>>16&255,o[6]=u>>>8&255,o[7]=u&255;let d=Math.floor(c/4294967296),f=c>>>0;o[8]=d>>>24&255,o[9]=d>>>16&255,o[10]=d>>>8&255,o[11]=d&255,o[12]=f>>>24&255,o[13]=f>>>16&255,o[14]=f>>>8&255,o[15]=f&255;for(let e=0;e<16;e++)r[e]^=o[e];let p=gfMul(r,e);return r.set(p),r}function parseAlgorithm(e){let t=e.toLowerCase().match(/^aes-(128|192|256)-(cbc|ctr|ecb|cfb|ofb|gcm)$/);if(!t)throw Error(`Unsupported cipher algorithm: ${e}`);let n=parseInt(t[1]),r=t[2];return{keySize:n/8,ivSize:r===`ecb`?0:r===`gcm`?12:16,mode:r}}function toBuffer(t,n){return typeof t==`string`?e.from(t,n||`utf8`):e.from(t)}function encodeOutput(t,n){return n?e.from(t).toString(n):e.from(t)}function incompleteUtf8Tail(e){if(e.length===0)return 0;let t=e.length;for(let n=1;n<=Math.min(4,t);n++){let r=e[t-n];if(!(r&128))return 0;if((r&192)==128)continue;let i;if((r&224)==192)i=2;else if((r&240)==224)i=3;else if((r&248)==240)i=4;else return 0;return n<i?n:0}return 0}function pkcs7Pad(e){let t=16-e.length%16,n=new Uint8Array(e.length+t);n.set(e);for(let r=e.length;r<n.length;r++)n[r]=t;return n}function pkcs7Unpad(e){if(e.length===0||e.length%16!=0)throw Error(`bad decrypt`);let t=e[e.length-1];if(t===0||t>16)throw Error(`bad decrypt`);for(let n=e.length-t;n<e.length;n++)if(e[n]!==t)throw Error(`bad decrypt`);return new Uint8Array(e.slice(0,e.length-t))}var CipherBase=class{_roundKeys;_iv;_mode;_buffer=new Uint8Array;_autoPadding=!0;_finalized=!1;constructor(e,t,n){let r=parseAlgorithm(e);if(t.length!==r.keySize)throw Error(`Invalid key length ${t.length}, expected ${r.keySize} for ${e}`);if(r.ivSize>0&&(!n||n.length!==r.ivSize))throw Error(`Invalid IV length ${n?.length??0}, expected ${r.ivSize} for ${e}`);this._roundKeys=keyExpansion(t),this._iv=n?new Uint8Array(n):new Uint8Array(16),this._mode=r.mode}setAutoPadding(e){return this._autoPadding=e,this}},Cipher=class extends CipherBase{_prevBlock;_counter;_gcmH=null;_gcmJ0=null;_gcmAAD=new Uint8Array;_gcmCiphertext=[];_gcmCiphertextLen=0;_gcmAuthTag=null;_gcmAADSet=!1;constructor(e,t,n){super(e,t,n),this._prevBlock=new Uint8Array(this._iv),this._mode===`gcm`?(this._gcmH=aesEncryptBlock(new Uint8Array(16),this._roundKeys),this._gcmJ0=new Uint8Array(16),this._gcmJ0.set(this._iv.subarray(0,12)),this._gcmJ0[15]=1,this._counter=new Uint8Array(this._gcmJ0),gcmIncrementCounter(this._counter)):this._counter=new Uint8Array(this._iv)}setAAD(e){if(this._mode!==`gcm`)throw Error(`setAAD is only supported in GCM mode`);if(this._gcmCiphertextLen>0)throw Error(`setAAD must be called before update()`);return this._gcmAAD=new Uint8Array(e),this._gcmAADSet=!0,this}getAuthTag(){if(this._mode!==`gcm`)throw Error(`getAuthTag is only supported in GCM mode`);if(!this._gcmAuthTag)throw Error(`getAuthTag must be called after final()`);return e.from(this._gcmAuthTag)}update(e,t,n){let r=toBuffer(e,t),i=new Uint8Array(this._buffer.length+r.length);if(i.set(this._buffer),i.set(r,this._buffer.length),this._mode===`gcm`){let e=this._processGcmEncrypt(i);return this._buffer=new Uint8Array,encodeOutput(e,n)}if(this._mode===`ctr`||this._mode===`cfb`||this._mode===`ofb`){let e=this._processStream(i);return this._buffer=new Uint8Array,encodeOutput(e,n)}let a=Math.floor(i.length/16)*16,o=[];for(let e=0;e<a;e+=16){let t=i.slice(e,e+16);o.push(this._encryptBlock(t))}this._buffer=i.slice(a);let s=new Uint8Array(o.length*16);for(let e=0;e<o.length;e++)s.set(o[e],e*16);return encodeOutput(s,n)}final(t){if(this._finalized)throw Error(`Cipher already finalized`);if(this._finalized=!0,this._mode===`gcm`){let n=new Uint8Array;this._buffer.length>0&&(n=this._processGcmEncrypt(this._buffer),this._buffer=new Uint8Array);let r=new Uint8Array(this._gcmCiphertextLen),i=0;for(let e of this._gcmCiphertext)r.set(e,i),i+=e.length;let a=ghash(this._gcmH,this._gcmAAD,r),o=aesEncryptBlock(this._gcmJ0,this._roundKeys),s=new Uint8Array(16);for(let e=0;e<16;e++)s[e]=a[e]^o[e];return this._gcmAuthTag=e.from(s),encodeOutput(n,t)}if(this._mode===`ctr`||this._mode===`cfb`||this._mode===`ofb`){if(this._buffer.length>0){let e=this._processStream(this._buffer);return this._buffer=new Uint8Array,encodeOutput(e,t)}return encodeOutput(new Uint8Array,t)}let n=this._buffer;if(this._autoPadding)n=pkcs7Pad(n);else if(n.length%16!=0)throw Error(`data not multiple of block size`);let r=[];for(let e=0;e<n.length;e+=16)r.push(this._encryptBlock(n.slice(e,e+16)));if(this._buffer=new Uint8Array,r.length===0)return encodeOutput(new Uint8Array,t);let i=new Uint8Array(r.length*16);for(let e=0;e<r.length;e++)i.set(r[e],e*16);return encodeOutput(i,t)}_encryptBlock(e){if(this._mode===`cbc`){let t=new Uint8Array(16);for(let n=0;n<16;n++)t[n]=e[n]^this._prevBlock[n];let n=aesEncryptBlock(t,this._roundKeys);return this._prevBlock=n,n}else if(this._mode===`ecb`)return aesEncryptBlock(e,this._roundKeys);throw Error(`Block encryption not supported for mode: ${this._mode}`)}_processStream(e){let t=new Uint8Array(e.length);for(let n=0;n<e.length;n+=16){let r=aesEncryptBlock(this._counter,this._roundKeys),i=Math.min(16,e.length-n);for(let a=0;a<i;a++)t[n+a]=e[n+a]^r[a];incrementCounter(this._counter)}return t}_processGcmEncrypt(e){let t=new Uint8Array(e.length);for(let n=0;n<e.length;n+=16){let r=aesEncryptBlock(this._counter,this._roundKeys),i=Math.min(16,e.length-n);for(let a=0;a<i;a++)t[n+a]=e[n+a]^r[a];gcmIncrementCounter(this._counter)}return this._gcmCiphertext.push(new Uint8Array(t)),this._gcmCiphertextLen+=t.length,t}},Decipher=class extends CipherBase{_prevBlock;_counter;_pendingUtf8=new Uint8Array;_gcmH=null;_gcmJ0=null;_gcmAAD=new Uint8Array;_gcmCiphertext=[];_gcmCiphertextLen=0;_gcmExpectedTag=null;_gcmAADSet=!1;constructor(e,t,n){super(e,t,n),this._prevBlock=new Uint8Array(this._iv),this._mode===`gcm`?(this._gcmH=aesEncryptBlock(new Uint8Array(16),this._roundKeys),this._gcmJ0=new Uint8Array(16),this._gcmJ0.set(this._iv.subarray(0,12)),this._gcmJ0[15]=1,this._counter=new Uint8Array(this._gcmJ0),gcmIncrementCounter(this._counter)):this._counter=new Uint8Array(this._iv)}setAAD(e){if(this._mode!==`gcm`)throw Error(`setAAD is only supported in GCM mode`);if(this._gcmCiphertextLen>0)throw Error(`setAAD must be called before update()`);return this._gcmAAD=new Uint8Array(e),this._gcmAADSet=!0,this}setAuthTag(t){if(this._mode!==`gcm`)throw Error(`setAuthTag is only supported in GCM mode`);return this._gcmExpectedTag=e.from(t),this}_encodeWithUtf8Handling(t,n,r){if(!n||n!==`utf8`&&n!==`utf-8`)return encodeOutput(t,n);let i;if(this._pendingUtf8.length>0?(i=new Uint8Array(this._pendingUtf8.length+t.length),i.set(this._pendingUtf8),i.set(t,this._pendingUtf8.length),this._pendingUtf8=new Uint8Array):i=t,!r){let e=incompleteUtf8Tail(i);e>0&&(this._pendingUtf8=new Uint8Array(i.slice(i.length-e)),i=new Uint8Array(i.slice(0,i.length-e)))}return e.from(i).toString(`utf8`)}update(e,t,n){let r=toBuffer(e,t),i=new Uint8Array(this._buffer.length+r.length);if(i.set(this._buffer),i.set(r,this._buffer.length),this._mode===`gcm`){this._gcmCiphertext.push(new Uint8Array(i)),this._gcmCiphertextLen+=i.length;let e=this._processGcmDecrypt(i);return this._buffer=new Uint8Array,this._encodeWithUtf8Handling(e,n,!1)}if(this._mode===`ctr`||this._mode===`cfb`||this._mode===`ofb`){let e=this._processStream(i);return this._buffer=new Uint8Array,this._encodeWithUtf8Handling(e,n,!1)}let a=Math.floor(i.length/16);if(a===0)return this._buffer=i,this._encodeWithUtf8Handling(new Uint8Array,n,!1);let o=(this._autoPadding?a-1:a)*16,s=[];for(let e=0;e<o;e+=16){let t=i.slice(e,e+16);s.push(this._decryptBlock(t))}this._buffer=i.slice(o);let c=new Uint8Array(s.length*16);for(let e=0;e<s.length;e++)c.set(s[e],e*16);return this._encodeWithUtf8Handling(c,n,!1)}final(e){if(this._finalized)throw Error(`Decipher already finalized`);if(this._finalized=!0,this._mode===`gcm`){let t=new Uint8Array;if(this._buffer.length>0&&(this._gcmCiphertext.push(new Uint8Array(this._buffer)),this._gcmCiphertextLen+=this._buffer.length,t=this._processGcmDecrypt(this._buffer),this._buffer=new Uint8Array),!this._gcmExpectedTag)throw Error(`Unsupported state or unable to authenticate data`);let n=new Uint8Array(this._gcmCiphertextLen),r=0;for(let e of this._gcmCiphertext)n.set(e,r),r+=e.length;let i=ghash(this._gcmH,this._gcmAAD,n),a=aesEncryptBlock(this._gcmJ0,this._roundKeys),o=new Uint8Array(16);for(let e=0;e<16;e++)o[e]=i[e]^a[e];let s=this._gcmExpectedTag,c=Math.min(s.length,16),l=0;for(let e=0;e<c;e++)l|=o[e]^s[e];if(l!==0)throw Error(`Unsupported state or unable to authenticate data`);return this._encodeWithUtf8Handling(t,e,!0)}if(this._mode===`ctr`||this._mode===`cfb`||this._mode===`ofb`){if(this._buffer.length>0){let t=this._processStream(this._buffer);return this._buffer=new Uint8Array,this._encodeWithUtf8Handling(t,e,!0)}return this._encodeWithUtf8Handling(new Uint8Array,e,!0)}if(this._buffer.length===0)return this._encodeWithUtf8Handling(new Uint8Array,e,!0);if(this._buffer.length%16!=0)throw Error(`bad decrypt`);let t=[];for(let e=0;e<this._buffer.length;e+=16)t.push(this._decryptBlock(this._buffer.slice(e,e+16)));let n=new Uint8Array(t.length*16);for(let e=0;e<t.length;e++)n.set(t[e],e*16);let r=this._autoPadding?pkcs7Unpad(n):n;return this._buffer=new Uint8Array,this._encodeWithUtf8Handling(r,e,!0)}_decryptBlock(e){if(this._mode===`cbc`){let t=aesDecryptBlock(e,this._roundKeys),n=new Uint8Array(16);for(let e=0;e<16;e++)n[e]=t[e]^this._prevBlock[e];return this._prevBlock=new Uint8Array(e),n}else if(this._mode===`ecb`)return aesDecryptBlock(e,this._roundKeys);throw Error(`Block decryption not supported for mode: ${this._mode}`)}_processStream(e){let t=new Uint8Array(e.length);for(let n=0;n<e.length;n+=16){let r=aesEncryptBlock(this._counter,this._roundKeys),i=Math.min(16,e.length-n);for(let a=0;a<i;a++)t[n+a]=e[n+a]^r[a];incrementCounter(this._counter)}return t}_processGcmDecrypt(e){let t=new Uint8Array(e.length);for(let n=0;n<e.length;n+=16){let r=aesEncryptBlock(this._counter,this._roundKeys),i=Math.min(16,e.length-n);for(let a=0;a<i;a++)t[n+a]=e[n+a]^r[a];gcmIncrementCounter(this._counter)}return t}};function createCipher(e,t){throw Error(`crypto.createCipher() is deprecated. Use createCipheriv() instead.`)}function createCipheriv(t,n,r){return new Cipher(t,typeof n==`string`?e.from(n):new Uint8Array(n),r==null?null:typeof r==`string`?e.from(r):new Uint8Array(r))}function createDecipher(e,t){throw Error(`crypto.createDecipher() is deprecated. Use createDecipheriv() instead.`)}function createDecipheriv(t,n,r){return new Decipher(t,typeof n==`string`?e.from(n):new Uint8Array(n),r==null?null:typeof r==`string`?e.from(r):new Uint8Array(r))}function getCiphers(){return[`aes-128-cbc`,`aes-128-ecb`,`aes-192-cbc`,`aes-192-ecb`,`aes-256-cbc`,`aes-256-ecb`,`aes-128-ctr`,`aes-192-ctr`,`aes-256-ctr`,`aes-128-cfb`,`aes-192-cfb`,`aes-256-cfb`,`aes-128-gcm`,`aes-192-gcm`,`aes-256-gcm`]}export{createCipher,createCipheriv,createDecipher,createDecipheriv,getCiphers};
|
|
1
|
+
import"./_virtual/_rolldown/runtime.js";import{aesDecryptBlock as e,aesEncryptBlock as t,gcmIncrementCounter as n,ghash as r,incrementCounter as i,keyExpansion as a}from"./aes-primitives.js";import{encodeOutput as o,incompleteUtf8Tail as s,parseAlgorithm as c,pkcs7Pad as l,pkcs7Unpad as u,toBuffer as d}from"./cipher-utils.js";import{Buffer as f}from"node:buffer";var CipherBase=class{_roundKeys;_iv;_mode;_buffer=new Uint8Array;_autoPadding=!0;_finalized=!1;constructor(e,t,n){let r=c(e);if(t.length!==r.keySize)throw Error(`Invalid key length ${t.length}, expected ${r.keySize} for ${e}`);if(r.ivSize>0&&(!n||n.length!==r.ivSize))throw Error(`Invalid IV length ${n?.length??0}, expected ${r.ivSize} for ${e}`);this._roundKeys=a(t),this._iv=n?new Uint8Array(n):new Uint8Array(16),this._mode=r.mode}setAutoPadding(e){return this._autoPadding=e,this}},Cipher=class extends CipherBase{_prevBlock;_counter;_gcmH=null;_gcmJ0=null;_gcmAAD=new Uint8Array;_gcmCiphertext=[];_gcmCiphertextLen=0;_gcmAuthTag=null;_gcmAADSet=!1;constructor(e,r,i){super(e,r,i),this._prevBlock=new Uint8Array(this._iv),this._mode===`gcm`?(this._gcmH=t(new Uint8Array(16),this._roundKeys),this._gcmJ0=new Uint8Array(16),this._gcmJ0.set(this._iv.subarray(0,12)),this._gcmJ0[15]=1,this._counter=new Uint8Array(this._gcmJ0),n(this._counter)):this._counter=new Uint8Array(this._iv)}setAAD(e){if(this._mode!==`gcm`)throw Error(`setAAD is only supported in GCM mode`);if(this._gcmCiphertextLen>0)throw Error(`setAAD must be called before update()`);return this._gcmAAD=new Uint8Array(e),this._gcmAADSet=!0,this}getAuthTag(){if(this._mode!==`gcm`)throw Error(`getAuthTag is only supported in GCM mode`);if(!this._gcmAuthTag)throw Error(`getAuthTag must be called after final()`);return f.from(this._gcmAuthTag)}update(e,t,n){let r=d(e,t),i=new Uint8Array(this._buffer.length+r.length);if(i.set(this._buffer),i.set(r,this._buffer.length),this._mode===`gcm`){let e=this._processGcmEncrypt(i);return this._buffer=new Uint8Array,o(e,n)}if(this._mode===`ctr`||this._mode===`cfb`||this._mode===`ofb`){let e=this._processStream(i);return this._buffer=new Uint8Array,o(e,n)}let a=Math.floor(i.length/16)*16,s=[];for(let e=0;e<a;e+=16){let t=i.slice(e,e+16);s.push(this._encryptBlock(t))}this._buffer=i.slice(a);let c=new Uint8Array(s.length*16);for(let e=0;e<s.length;e++)c.set(s[e],e*16);return o(c,n)}final(e){if(this._finalized)throw Error(`Cipher already finalized`);if(this._finalized=!0,this._mode===`gcm`){let n=new Uint8Array;this._buffer.length>0&&(n=this._processGcmEncrypt(this._buffer),this._buffer=new Uint8Array);let i=new Uint8Array(this._gcmCiphertextLen),a=0;for(let e of this._gcmCiphertext)i.set(e,a),a+=e.length;let s=r(this._gcmH,this._gcmAAD,i),c=t(this._gcmJ0,this._roundKeys),l=new Uint8Array(16);for(let e=0;e<16;e++)l[e]=s[e]^c[e];return this._gcmAuthTag=f.from(l),o(n,e)}if(this._mode===`ctr`||this._mode===`cfb`||this._mode===`ofb`){if(this._buffer.length>0){let t=this._processStream(this._buffer);return this._buffer=new Uint8Array,o(t,e)}return o(new Uint8Array,e)}let n=this._buffer;if(this._autoPadding)n=l(n);else if(n.length%16!=0)throw Error(`data not multiple of block size`);let i=[];for(let e=0;e<n.length;e+=16)i.push(this._encryptBlock(n.slice(e,e+16)));if(this._buffer=new Uint8Array,i.length===0)return o(new Uint8Array,e);let a=new Uint8Array(i.length*16);for(let e=0;e<i.length;e++)a.set(i[e],e*16);return o(a,e)}_encryptBlock(e){if(this._mode===`cbc`){let n=new Uint8Array(16);for(let t=0;t<16;t++)n[t]=e[t]^this._prevBlock[t];let r=t(n,this._roundKeys);return this._prevBlock=r,r}else if(this._mode===`ecb`)return t(e,this._roundKeys);throw Error(`Block encryption not supported for mode: ${this._mode}`)}_processStream(e){let n=new Uint8Array(e.length);for(let r=0;r<e.length;r+=16){let a=t(this._counter,this._roundKeys),o=Math.min(16,e.length-r);for(let t=0;t<o;t++)n[r+t]=e[r+t]^a[t];i(this._counter)}return n}_processGcmEncrypt(e){let r=new Uint8Array(e.length);for(let i=0;i<e.length;i+=16){let a=t(this._counter,this._roundKeys),o=Math.min(16,e.length-i);for(let t=0;t<o;t++)r[i+t]=e[i+t]^a[t];n(this._counter)}return this._gcmCiphertext.push(new Uint8Array(r)),this._gcmCiphertextLen+=r.length,r}},Decipher=class extends CipherBase{_prevBlock;_counter;_pendingUtf8=new Uint8Array;_gcmH=null;_gcmJ0=null;_gcmAAD=new Uint8Array;_gcmCiphertext=[];_gcmCiphertextLen=0;_gcmExpectedTag=null;_gcmAADSet=!1;constructor(e,r,i){super(e,r,i),this._prevBlock=new Uint8Array(this._iv),this._mode===`gcm`?(this._gcmH=t(new Uint8Array(16),this._roundKeys),this._gcmJ0=new Uint8Array(16),this._gcmJ0.set(this._iv.subarray(0,12)),this._gcmJ0[15]=1,this._counter=new Uint8Array(this._gcmJ0),n(this._counter)):this._counter=new Uint8Array(this._iv)}setAAD(e){if(this._mode!==`gcm`)throw Error(`setAAD is only supported in GCM mode`);if(this._gcmCiphertextLen>0)throw Error(`setAAD must be called before update()`);return this._gcmAAD=new Uint8Array(e),this._gcmAADSet=!0,this}setAuthTag(e){if(this._mode!==`gcm`)throw Error(`setAuthTag is only supported in GCM mode`);return this._gcmExpectedTag=f.from(e),this}_encodeWithUtf8Handling(e,t,n){if(!t||t!==`utf8`&&t!==`utf-8`)return o(e,t);let r;if(this._pendingUtf8.length>0?(r=new Uint8Array(this._pendingUtf8.length+e.length),r.set(this._pendingUtf8),r.set(e,this._pendingUtf8.length),this._pendingUtf8=new Uint8Array):r=e,!n){let e=s(r);e>0&&(this._pendingUtf8=new Uint8Array(r.slice(r.length-e)),r=new Uint8Array(r.slice(0,r.length-e)))}return f.from(r).toString(`utf8`)}update(e,t,n){let r=d(e,t),i=new Uint8Array(this._buffer.length+r.length);if(i.set(this._buffer),i.set(r,this._buffer.length),this._mode===`gcm`){this._gcmCiphertext.push(new Uint8Array(i)),this._gcmCiphertextLen+=i.length;let e=this._processGcmDecrypt(i);return this._buffer=new Uint8Array,this._encodeWithUtf8Handling(e,n,!1)}if(this._mode===`ctr`||this._mode===`cfb`||this._mode===`ofb`){let e=this._processStream(i);return this._buffer=new Uint8Array,this._encodeWithUtf8Handling(e,n,!1)}let a=Math.floor(i.length/16);if(a===0)return this._buffer=i,this._encodeWithUtf8Handling(new Uint8Array,n,!1);let o=(this._autoPadding?a-1:a)*16,s=[];for(let e=0;e<o;e+=16){let t=i.slice(e,e+16);s.push(this._decryptBlock(t))}this._buffer=i.slice(o);let c=new Uint8Array(s.length*16);for(let e=0;e<s.length;e++)c.set(s[e],e*16);return this._encodeWithUtf8Handling(c,n,!1)}final(e){if(this._finalized)throw Error(`Decipher already finalized`);if(this._finalized=!0,this._mode===`gcm`){let n=new Uint8Array;if(this._buffer.length>0&&(this._gcmCiphertext.push(new Uint8Array(this._buffer)),this._gcmCiphertextLen+=this._buffer.length,n=this._processGcmDecrypt(this._buffer),this._buffer=new Uint8Array),!this._gcmExpectedTag)throw Error(`Unsupported state or unable to authenticate data`);let i=new Uint8Array(this._gcmCiphertextLen),a=0;for(let e of this._gcmCiphertext)i.set(e,a),a+=e.length;let o=r(this._gcmH,this._gcmAAD,i),s=t(this._gcmJ0,this._roundKeys),c=new Uint8Array(16);for(let e=0;e<16;e++)c[e]=o[e]^s[e];let l=this._gcmExpectedTag,u=Math.min(l.length,16),d=0;for(let e=0;e<u;e++)d|=c[e]^l[e];if(d!==0)throw Error(`Unsupported state or unable to authenticate data`);return this._encodeWithUtf8Handling(n,e,!0)}if(this._mode===`ctr`||this._mode===`cfb`||this._mode===`ofb`){if(this._buffer.length>0){let t=this._processStream(this._buffer);return this._buffer=new Uint8Array,this._encodeWithUtf8Handling(t,e,!0)}return this._encodeWithUtf8Handling(new Uint8Array,e,!0)}if(this._buffer.length===0)return this._encodeWithUtf8Handling(new Uint8Array,e,!0);if(this._buffer.length%16!=0)throw Error(`bad decrypt`);let n=[];for(let e=0;e<this._buffer.length;e+=16)n.push(this._decryptBlock(this._buffer.slice(e,e+16)));let i=new Uint8Array(n.length*16);for(let e=0;e<n.length;e++)i.set(n[e],e*16);let a=this._autoPadding?u(i):i;return this._buffer=new Uint8Array,this._encodeWithUtf8Handling(a,e,!0)}_decryptBlock(t){if(this._mode===`cbc`){let n=e(t,this._roundKeys),r=new Uint8Array(16);for(let e=0;e<16;e++)r[e]=n[e]^this._prevBlock[e];return this._prevBlock=new Uint8Array(t),r}else if(this._mode===`ecb`)return e(t,this._roundKeys);throw Error(`Block decryption not supported for mode: ${this._mode}`)}_processStream(e){let n=new Uint8Array(e.length);for(let r=0;r<e.length;r+=16){let a=t(this._counter,this._roundKeys),o=Math.min(16,e.length-r);for(let t=0;t<o;t++)n[r+t]=e[r+t]^a[t];i(this._counter)}return n}_processGcmDecrypt(e){let r=new Uint8Array(e.length);for(let i=0;i<e.length;i+=16){let a=t(this._counter,this._roundKeys),o=Math.min(16,e.length-i);for(let t=0;t<o;t++)r[i+t]=e[i+t]^a[t];n(this._counter)}return r}};function createCipher(e,t){throw Error(`crypto.createCipher() is deprecated. Use createCipheriv() instead.`)}function createCipheriv(e,t,n){return new Cipher(e,typeof t==`string`?f.from(t):new Uint8Array(t),n==null?null:typeof n==`string`?f.from(n):new Uint8Array(n))}function createDecipher(e,t){throw Error(`crypto.createDecipher() is deprecated. Use createDecipheriv() instead.`)}function createDecipheriv(e,t,n){return new Decipher(e,typeof t==`string`?f.from(t):new Uint8Array(t),n==null?null:typeof n==`string`?f.from(n):new Uint8Array(n))}function getCiphers(){return[`aes-128-cbc`,`aes-128-ecb`,`aes-192-cbc`,`aes-192-ecb`,`aes-256-cbc`,`aes-256-ecb`,`aes-128-ctr`,`aes-192-ctr`,`aes-256-ctr`,`aes-128-cfb`,`aes-192-cfb`,`aes-256-cfb`,`aes-128-gcm`,`aes-192-gcm`,`aes-256-gcm`]}export{createCipher,createCipheriv,createDecipher,createDecipheriv,getCiphers};
|
package/lib/esm/dh.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={modp1:{gen:`02`,prime:`ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a63a3620ffffffffffffffff`},modp2:{gen:`02`,prime:`ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece65381ffffffffffffffff`},modp5:{gen:`02`,prime:`ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca237327ffffffffffffffff`},modp14:{gen:`02`,prime:`ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aacaa68ffffffffffffffff`},modp15:{gen:`02`,prime:`ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a93ad2caffffffffffffffff`},modp16:{gen:`02`,prime:`ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c934063199ffffffffffffffff`},modp17:{gen:`02`,prime:`ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c93402849236c3fab4d27c7026c1d4dcb2602646dec9751e763dba37bdf8ff9406ad9e530ee5db382f413001aeb06a53ed9027d831179727b0865a8918da3edbebcf9b14ed44ce6cbaced4bb1bdb7f1447e6cc254b332051512bd7af426fb8f401378cd2bf5983ca01c64b92ecf032ea15d1721d03f482d7ce6e74fef6d55e702f46980c82b5a84031900b1c9e59e7c97fbec7e8f323a97a7e36cc88be0f1d45b7ff585ac54bd407b22b4154aacc8f6d7ebf48e1d814cc5ed20f8037e0a79715eef29be32806a1d58bb7c5da76f550aa3d8a1fbff0eb19ccb1a313d55cda56c9ec2ef29632387fe8d76e3c0468043e8f663f4860ee12bf2d5b0b7474d6e694f91e6dcc4024ffffffffffffffff`},modp18:{gen:`02`,prime:`ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c93402849236c3fab4d27c7026c1d4dcb2602646dec9751e763dba37bdf8ff9406ad9e530ee5db382f413001aeb06a53ed9027d831179727b0865a8918da3edbebcf9b14ed44ce6cbaced4bb1bdb7f1447e6cc254b332051512bd7af426fb8f401378cd2bf5983ca01c64b92ecf032ea15d1721d03f482d7ce6e74fef6d55e702f46980c82b5a84031900b1c9e59e7c97fbec7e8f323a97a7e36cc88be0f1d45b7ff585ac54bd407b22b4154aacc8f6d7ebf48e1d814cc5ed20f8037e0a79715eef29be32806a1d58bb7c5da76f550aa3d8a1fbff0eb19ccb1a313d55cda56c9ec2ef29632387fe8d76e3c0468043e8f663f4860ee12bf2d5b0b7474d6e694f91e6dbe115974a3926f12fee5e438777cb6a932df8cd8bec4d073b931ba3bc832b68d9dd300741fa7bf8afc47ed2576f6936ba424663aab639c5ae4f5683423b4742bf1c978238f16cbe39d652de3fdb8befc848ad922222e04a4037c0713eb57a81a23f0c73473fc646cea306b4bcbc8862f8385ddfa9d4b7fa2c087e879683303ed5bdd3a062b3cf5b3a278a66d2a13f83f44f82ddf310ee074ab6a364597e899a0255dc164f31cc50846851df9ab48195ded7ea1b1d510bd7ee74d73faf36bc31ecfa268359046f4eb879f924009438b481c6cd7889a002ed5ee382bc9190da6fc026e479558e4475677e9aa9e3050e2765694dfc81f56e880b96e7160c980dd98edd3dfffffffffffffffff`}};function bigIntToBuffer(e){if(e===0n)return n.from([0]);let t=e.toString(16);return t.length%2!=0&&(t=`0`+t),n.from(t,`hex`)}function bufferToBigInt(e){if(e.length===0)return 0n;let t=n.from(e).toString(`hex`);return t.length===0?0n:BigInt(`0x`+t)}function bitLength(e){return e===0n?0:e.toString(2).length}const i=[];{let e=1e3,t=new Uint8Array(e+1);for(let n=2;n*n<=e;n++)if(!t[n])for(let r=n*n;r<=e;r+=n)t[r]=1;for(let n=2;n<=e;n++)t[n]||i.push(n)}function simpleSieve(e){for(let t of i){let n=BigInt(t);if(e%n===0n)return e===n}return!0}function fermatTest(t){return e(2n,t-1n,t)===1n}function millerRabinTest(t){if(t<2n)return!1;if(t===2n||t===3n)return!0;if(t%2n==0n)return!1;let n=t-1n,r=0;for(;n%2n==0n;)n/=2n,r++;for(let i of[2n,3n,5n,7n,11n,13n]){if(i>=t)continue;let a=e(i,n,t);if(a===1n||a===t-1n)continue;let o=!0;for(let n=0;n<r-1;n++)if(a=e(a,2n,t),a===t-1n){o=!1;break}if(o)return!1}return!0}function checkPrime(e,t){let n=t.toString(`hex`),r=0;if(e%2n==0n||!simpleSieve(e)||!fermatTest(e)||!millerRabinTest(e))return r+=1,n===`02`||n===`05`?r+=8:r+=4,r;switch(millerRabinTest(e>>1n)||(r+=2),n){case`02`:e%24n!=11n&&(r+=8);break;case`05`:{let t=e%10n;t!==3n&&t!==7n&&(r+=8)}break;default:r+=4}return r}const a={};function getCachedCheckPrime(e,t){let n=t.toString(`hex`)+`_`+e.toString(16);if(n in a)return a[n];let r=checkPrime(e,t);return a[n]=r,r}function formatReturnValue(e,t){let n=bigIntToBuffer(e);return t?n.toString(t):n}var DiffieHellman=class{_prime;_generator;_generatorBuf;_primeByteLength;_pubKey;_privKey;_primeCode;_malleable;constructor(e,t,n=!1){this._prime=bufferToBigInt(e),this._generatorBuf=t,this._generator=bufferToBigInt(t),this._primeByteLength=e.length,this._pubKey=void 0,this._privKey=void 0,this._primeCode=void 0,this._malleable=n,n||(this._primeCode=0,this.setPublicKey=void 0,this.setPrivateKey=void 0)}get verifyError(){return typeof this._primeCode!=`number`&&(this._primeCode=getCachedCheckPrime(this._prime,this._generatorBuf)),this._primeCode}generateKeys(n){if(!this._privKey){let e=t(this._primeByteLength);this._privKey=bufferToBigInt(e)}return this._pubKey=e(this._generator,this._privKey,this._prime),this.getPublicKey(n)}computeSecret(t,r,i){let a;a=typeof t==`string`?n.from(t,r||`utf8`):n.from(t);let o=bufferToBigInt(a);if(!this._privKey)throw Error(`You must generate keys before computing a secret`);if(o<=0n||o>=this._prime)throw Error(`Supplied key is too large`);let s=bigIntToBuffer(e(o,this._privKey,this._prime)),c=this._primeByteLength;if(s.length<c){let e=n.alloc(c);s.copy(e,c-s.length),s=e}return i?s.toString(i):s}getPrime(e){return formatReturnValue(this._prime,e)}getGenerator(e){return formatReturnValue(this._generator,e)}getPublicKey(e){if(!this._pubKey)throw Error(`No public key - call generateKeys() first`);return formatReturnValue(this._pubKey,e)}getPrivateKey(e){if(!this._privKey)throw Error(`No private key - call generateKeys() first`);return formatReturnValue(this._privKey,e)}setPublicKey(e,t){if(!this._malleable)throw Error(`setPublicKey is not available for predefined DH groups`);let r=typeof e==`string`?n.from(e,t||`utf8`):n.from(e);this._pubKey=bufferToBigInt(r)}setPrivateKey(e,t){if(!this._malleable)throw Error(`setPrivateKey is not available for predefined DH groups`);let r=typeof e==`string`?n.from(e,t||`utf8`):n.from(e);this._privKey=bufferToBigInt(r)}};function generatePrime(e,r){let i=bufferToBigInt(r);if(e<16)return i===2n||i===5n?n.from([140,123]):n.from([140,39]);let a=Math.ceil(e/8),o=2n,s=4n;for(;;){let n=bufferToBigInt(t(a));for(;bitLength(n)>e;)n>>=1n;if(n%o===0n&&(n+=1n),n&o||(n+=o),i===o)for(;n%24n!=11n;)n+=s;else if(i===5n)for(;n%10n!=3n;)n+=s;let r=n>>1n;if(simpleSieve(r)&&simpleSieve(n)&&fermatTest(r)&&fermatTest(n)&&millerRabinTest(r)&&millerRabinTest(n))return bigIntToBuffer(n)}}const o={binary:!0,hex:!0,base64:!0,latin1:!0,utf8:!0,"utf-8":!0,base64url:!0,ascii:!0,ucs2:!0,"ucs-2":!0,utf16le:!0,"utf-16le":!0};function createDiffieHellman(e,t,r,i){if(t!==void 0&&typeof t!=`number`&&(n.isBuffer(t)||t instanceof Uint8Array||typeof t==`string`&&!(t in o)))return createDiffieHellman(e,`binary`,t,r);let a=t||`binary`,s=i||`binary`,c=n.from([2]);if(r!==void 0&&(typeof r==`number`?(c=n.alloc(1),c[0]=r):c=n.isBuffer(r)?r:n.from(r,s)),typeof e==`number`)return new DiffieHellman(generatePrime(e,c),c,!0);let l;return l=n.isBuffer(e)?e:e instanceof Uint8Array?n.from(e):n.from(e,a),new DiffieHellman(l,c,!0)}function getDiffieHellman(e){let t=r[e];if(!t)throw Error(`Unknown group: ${e}. Supported groups: ${Object.keys(r).join(`, `)}`);return new DiffieHellman(n.from(t.prime,`hex`),n.from(t.gen,`hex`),!1)}const s=getDiffieHellman,c=getDiffieHellman;export{DiffieHellman,c as DiffieHellmanGroup,createDiffieHellman,s as createDiffieHellmanGroup,getDiffieHellman};
|
|
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={modp1:{gen:`02`,prime:`ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a63a3620ffffffffffffffff`},modp2:{gen:`02`,prime:`ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece65381ffffffffffffffff`},modp5:{gen:`02`,prime:`ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca237327ffffffffffffffff`},modp14:{gen:`02`,prime:`ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aacaa68ffffffffffffffff`},modp15:{gen:`02`,prime:`ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a93ad2caffffffffffffffff`},modp16:{gen:`02`,prime:`ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c934063199ffffffffffffffff`},modp17:{gen:`02`,prime:`ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c93402849236c3fab4d27c7026c1d4dcb2602646dec9751e763dba37bdf8ff9406ad9e530ee5db382f413001aeb06a53ed9027d831179727b0865a8918da3edbebcf9b14ed44ce6cbaced4bb1bdb7f1447e6cc254b332051512bd7af426fb8f401378cd2bf5983ca01c64b92ecf032ea15d1721d03f482d7ce6e74fef6d55e702f46980c82b5a84031900b1c9e59e7c97fbec7e8f323a97a7e36cc88be0f1d45b7ff585ac54bd407b22b4154aacc8f6d7ebf48e1d814cc5ed20f8037e0a79715eef29be32806a1d58bb7c5da76f550aa3d8a1fbff0eb19ccb1a313d55cda56c9ec2ef29632387fe8d76e3c0468043e8f663f4860ee12bf2d5b0b7474d6e694f91e6dcc4024ffffffffffffffff`},modp18:{gen:`02`,prime:`ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a637ed6b0bff5cb6f406b7edee386bfb5a899fa5ae9f24117c4b1fe649286651ece45b3dc2007cb8a163bf0598da48361c55d39a69163fa8fd24cf5f83655d23dca3ad961c62f356208552bb9ed529077096966d670c354e4abc9804f1746c08ca18217c32905e462e36ce3be39e772c180e86039b2783a2ec07a28fb5c55df06f4c52c9de2bcbf6955817183995497cea956ae515d2261898fa051015728e5a8aaac42dad33170d04507a33a85521abdf1cba64ecfb850458dbef0a8aea71575d060c7db3970f85a6e1e4c7abf5ae8cdb0933d71e8c94e04a25619dcee3d2261ad2ee6bf12ffa06d98a0864d87602733ec86a64521f2b18177b200cbbe117577a615d6c770988c0bad946e208e24fa074e5ab3143db5bfce0fd108e4b82d120a92108011a723c12a787e6d788719a10bdba5b2699c327186af4e23c1a946834b6150bda2583e9ca2ad44ce8dbbbc2db04de8ef92e8efc141fbecaa6287c59474e6bc05d99b2964fa090c3a2233ba186515be7ed1f612970cee2d7afb81bdd762170481cd0069127d5b05aa993b4ea988d8fddc186ffb7dc90a6c08f4df435c93402849236c3fab4d27c7026c1d4dcb2602646dec9751e763dba37bdf8ff9406ad9e530ee5db382f413001aeb06a53ed9027d831179727b0865a8918da3edbebcf9b14ed44ce6cbaced4bb1bdb7f1447e6cc254b332051512bd7af426fb8f401378cd2bf5983ca01c64b92ecf032ea15d1721d03f482d7ce6e74fef6d55e702f46980c82b5a84031900b1c9e59e7c97fbec7e8f323a97a7e36cc88be0f1d45b7ff585ac54bd407b22b4154aacc8f6d7ebf48e1d814cc5ed20f8037e0a79715eef29be32806a1d58bb7c5da76f550aa3d8a1fbff0eb19ccb1a313d55cda56c9ec2ef29632387fe8d76e3c0468043e8f663f4860ee12bf2d5b0b7474d6e694f91e6dbe115974a3926f12fee5e438777cb6a932df8cd8bec4d073b931ba3bc832b68d9dd300741fa7bf8afc47ed2576f6936ba424663aab639c5ae4f5683423b4742bf1c978238f16cbe39d652de3fdb8befc848ad922222e04a4037c0713eb57a81a23f0c73473fc646cea306b4bcbc8862f8385ddfa9d4b7fa2c087e879683303ed5bdd3a062b3cf5b3a278a66d2a13f83f44f82ddf310ee074ab6a364597e899a0255dc164f31cc50846851df9ab48195ded7ea1b1d510bd7ee74d73faf36bc31ecfa268359046f4eb879f924009438b481c6cd7889a002ed5ee382bc9190da6fc026e479558e4475677e9aa9e3050e2765694dfc81f56e880b96e7160c980dd98edd3dfffffffffffffffff`}};function bigIntToBuffer(e){if(e===0n)return n.from([0]);let t=e.toString(16);return t.length%2!=0&&(t=`0`+t),n.from(t,`hex`)}function bufferToBigInt(e){if(e.length===0)return 0n;let t=n.from(e).toString(`hex`);return t.length===0?0n:BigInt(`0x`+t)}function bitLength(e){return e===0n?0:e.toString(2).length}const i=[];{let e=1e3,t=new Uint8Array(1001);for(let n=2;n*n<=e;n++)if(!t[n])for(let r=n*n;r<=e;r+=n)t[r]=1;for(let n=2;n<=e;n++)t[n]||i.push(n)}function simpleSieve(e){for(let t of i){let n=BigInt(t);if(e%n===0n)return e===n}return!0}function fermatTest(t){return e(2n,t-1n,t)===1n}function millerRabinTest(t){if(t<2n)return!1;if(t===2n||t===3n)return!0;if(t%2n==0n)return!1;let n=t-1n,r=0;for(;n%2n==0n;)n/=2n,r++;for(let i of[2n,3n,5n,7n,11n,13n]){if(i>=t)continue;let a=e(i,n,t);if(a===1n||a===t-1n)continue;let o=!0;for(let n=0;n<r-1;n++)if(a=e(a,2n,t),a===t-1n){o=!1;break}if(o)return!1}return!0}function checkPrime(e,t){let n=t.toString(`hex`),r=0;if(e%2n==0n||!simpleSieve(e)||!fermatTest(e)||!millerRabinTest(e))return r+=1,n===`02`||n===`05`?r+=8:r+=4,r;switch(millerRabinTest(e>>1n)||(r+=2),n){case`02`:e%24n!=11n&&(r+=8);break;case`05`:{let t=e%10n;t!==3n&&t!==7n&&(r+=8)}break;default:r+=4}return r}const a={};function getCachedCheckPrime(e,t){let n=t.toString(`hex`)+`_`+e.toString(16);if(n in a)return a[n];let r=checkPrime(e,t);return a[n]=r,r}function formatReturnValue(e,t){let n=bigIntToBuffer(e);return t?n.toString(t):n}var DiffieHellman=class{_prime;_generator;_generatorBuf;_primeByteLength;_pubKey;_privKey;_primeCode;_malleable;constructor(e,t,n=!1){this._prime=bufferToBigInt(e),this._generatorBuf=t,this._generator=bufferToBigInt(t),this._primeByteLength=e.length,this._pubKey=void 0,this._privKey=void 0,this._primeCode=void 0,this._malleable=n,n||(this._primeCode=0,this.setPublicKey=void 0,this.setPrivateKey=void 0)}get verifyError(){return typeof this._primeCode!=`number`&&(this._primeCode=getCachedCheckPrime(this._prime,this._generatorBuf)),this._primeCode}generateKeys(n){if(!this._privKey){let e=t(this._primeByteLength);this._privKey=bufferToBigInt(e)}return this._pubKey=e(this._generator,this._privKey,this._prime),this.getPublicKey(n)}computeSecret(t,r,i){let a;a=typeof t==`string`?n.from(t,r||`utf8`):n.from(t);let o=bufferToBigInt(a);if(!this._privKey)throw Error(`You must generate keys before computing a secret`);if(o<=0n||o>=this._prime)throw Error(`Supplied key is too large`);let s=bigIntToBuffer(e(o,this._privKey,this._prime)),c=this._primeByteLength;if(s.length<c){let e=n.alloc(c);s.copy(e,c-s.length),s=e}return i?s.toString(i):s}getPrime(e){return formatReturnValue(this._prime,e)}getGenerator(e){return formatReturnValue(this._generator,e)}getPublicKey(e){if(!this._pubKey)throw Error(`No public key - call generateKeys() first`);return formatReturnValue(this._pubKey,e)}getPrivateKey(e){if(!this._privKey)throw Error(`No private key - call generateKeys() first`);return formatReturnValue(this._privKey,e)}setPublicKey(e,t){if(!this._malleable)throw Error(`setPublicKey is not available for predefined DH groups`);let r=typeof e==`string`?n.from(e,t||`utf8`):n.from(e);this._pubKey=bufferToBigInt(r)}setPrivateKey(e,t){if(!this._malleable)throw Error(`setPrivateKey is not available for predefined DH groups`);let r=typeof e==`string`?n.from(e,t||`utf8`):n.from(e);this._privKey=bufferToBigInt(r)}};function generatePrime(e,r){let i=bufferToBigInt(r);if(e<16)return i===2n||i===5n?n.from([140,123]):n.from([140,39]);let a=Math.ceil(e/8),o=2n,s=4n;for(;;){let n=bufferToBigInt(t(a));for(;bitLength(n)>e;)n>>=1n;if(n%o==0n&&(n+=1n),n&o||(n+=o),i===o)for(;n%24n!=11n;)n+=s;else if(i===5n)for(;n%10n!=3n;)n+=s;let r=n>>1n;if(simpleSieve(r)&&simpleSieve(n)&&fermatTest(r)&&fermatTest(n)&&millerRabinTest(r)&&millerRabinTest(n))return bigIntToBuffer(n)}}const o={binary:!0,hex:!0,base64:!0,latin1:!0,utf8:!0,"utf-8":!0,base64url:!0,ascii:!0,ucs2:!0,"ucs-2":!0,utf16le:!0,"utf-16le":!0};function createDiffieHellman(e,t,r,i){if(t!==void 0&&typeof t!=`number`&&(n.isBuffer(t)||t instanceof Uint8Array||typeof t==`string`&&!(t in o)))return createDiffieHellman(e,`binary`,t,r);let a=t||`binary`,s=i||`binary`,c=n.from([2]);if(r!==void 0&&(typeof r==`number`?(c=n.alloc(1),c[0]=r):c=n.isBuffer(r)?r:n.from(r,s)),typeof e==`number`)return new DiffieHellman(generatePrime(e,c),c,!0);let l;return l=n.isBuffer(e)?e:e instanceof Uint8Array?n.from(e):n.from(e,a),new DiffieHellman(l,c,!0)}function getDiffieHellman(e){let t=r[e];if(!t)throw Error(`Unknown group: ${e}. Supported groups: ${Object.keys(r).join(`, `)}`);return new DiffieHellman(n.from(t.prime,`hex`),n.from(t.gen,`hex`),!1)}const s=getDiffieHellman,c=getDiffieHellman;export{DiffieHellman,c as DiffieHellmanGroup,createDiffieHellman,s as createDiffieHellmanGroup,getDiffieHellman};
|
package/lib/esm/hash.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"./_virtual/_rolldown/runtime.js";import{normalizeAlgorithm as e}from"./crypto-utils.js";import{Buffer as t}from"node:buffer";import n from"@girs/glib-2.0";import{Transform as r}from"node:stream";import{normalizeEncoding as i}from"@gjsify/
|
|
1
|
+
import"./_virtual/_rolldown/runtime.js";import{normalizeAlgorithm as e}from"./crypto-utils.js";import{Buffer as t}from"node:buffer";import n from"@girs/glib-2.0";import{Transform as r}from"node:stream";import{normalizeEncoding as i}from"@gjsify/buffer";const a={md5:n.ChecksumType.MD5,sha1:n.ChecksumType.SHA1,sha256:n.ChecksumType.SHA256,sha384:n.ChecksumType.SHA384,sha512:n.ChecksumType.SHA512};function getChecksumType(t){let n=a[e(t)];if(n===void 0){let e=Error(`Unknown message digest: ${t}`);throw e.code=`ERR_CRYPTO_HASH_UNKNOWN`,e}return n}var o=class Hash extends r{_algorithm;_checksum;_finalized=!1;constructor(t){super();let r=e(t),i=getChecksumType(r);this._algorithm=r,this._checksum=new n.Checksum(i)}update(e,n){if(this._finalized)throw Error(`Digest already called`);let r;if(typeof e==`string`){let a=i(n);r=t.from(e,a)}else r=e instanceof Uint8Array?e:t.from(e);return this._checksum.update(r),this}digest(e){if(this._finalized)throw Error(`Digest already called`);this._finalized=!0;let n=this._checksum.get_string(),r=t.from(n,`hex`);return e?r.toString(e):r}copy(){if(this._finalized)throw Error(`Digest already called`);let e=Object.create(Hash.prototype);return r.call(e),e._algorithm=this._algorithm,e._finalized=!1,e._checksum=this._checksum.copy(),e}_transform(e,t,n){try{this.update(e,t),n()}catch(e){n(e)}}_flush(e){try{this.push(this.digest()),e()}catch(t){e(t)}}};function getHashes(){return[`md5`,`sha1`,`sha256`,`sha384`,`sha512`]}function hash(e,t,n){return new o(e).update(t).digest(n)}export{o as Hash,getHashes,hash};
|
package/lib/esm/hmac.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import"./_virtual/_rolldown/runtime.js";import{BLOCK_SIZES as e,SUPPORTED_ALGORITHMS as t,normalizeAlgorithm as n}from"./crypto-utils.js";import{Hash as r}from"./hash.js";import{Buffer as i}from"node:buffer";import{Transform as a}from"node:stream";import{normalizeEncoding as o}from"@gjsify/
|
|
1
|
+
import"./_virtual/_rolldown/runtime.js";import{BLOCK_SIZES as e,SUPPORTED_ALGORITHMS as t,normalizeAlgorithm as n}from"./crypto-utils.js";import{Hash as r}from"./hash.js";import{Buffer as i}from"node:buffer";import{Transform as a}from"node:stream";import{normalizeEncoding as o}from"@gjsify/buffer";var Hmac=class extends a{_algorithm;_innerHash;_outerKeyPad;_finalized=!1;constructor(a,o){super();let s=n(a);if(!t.has(s)){let e=Error(`Unknown message digest: ${a}`);throw e.code=`ERR_CRYPTO_HASH_UNKNOWN`,e}this._algorithm=s;let c;c=typeof o==`string`?i.from(o,`utf8`):o instanceof Uint8Array?o:i.from(o);let l=e[s];if(c.length>l){let e=new r(s);e.update(c),c=e.digest()}let u=new Uint8Array(l);u.set(c);let d=new Uint8Array(l),f=new Uint8Array(l);for(let e=0;e<l;e++)d[e]=u[e]^54,f[e]=u[e]^92;this._outerKeyPad=f,this._innerHash=new r(s),this._innerHash.update(d)}update(e,t){if(this._finalized)throw Error(`Digest already called`);let n;if(typeof e==`string`){let r=o(t);n=i.from(e,r)}else n=e instanceof Uint8Array?e:i.from(e);return this._innerHash.update(n),this}digest(e){if(this._finalized)throw Error(`Digest already called`);this._finalized=!0;let t=this._innerHash.digest(),n=new r(this._algorithm);n.update(this._outerKeyPad),n.update(t);let i=n.digest();return e?i.toString(e):i}_transform(e,t,n){try{this.update(e,t),n()}catch(e){n(e)}}_flush(e){try{this.push(this.digest()),e()}catch(t){e(t)}}};export{Hmac};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export declare function keyExpansion(key: Uint8Array): Uint8Array[];
|
|
2
|
+
export declare function aesEncryptBlock(block: Uint8Array, roundKeys: Uint8Array[]): Uint8Array;
|
|
3
|
+
export declare function aesDecryptBlock(block: Uint8Array, roundKeys: Uint8Array[]): Uint8Array;
|
|
4
|
+
export declare function incrementCounter(counter: Uint8Array): void;
|
|
5
|
+
export declare function gcmIncrementCounter(counter: Uint8Array): void;
|
|
6
|
+
/**
|
|
7
|
+
* GHASH function per NIST SP 800-38D.
|
|
8
|
+
*
|
|
9
|
+
* H: the hash subkey (AES_K(0^128)), 16 bytes
|
|
10
|
+
* aad: additional authenticated data (arbitrary length)
|
|
11
|
+
* ciphertext: ciphertext (arbitrary length)
|
|
12
|
+
*
|
|
13
|
+
* Returns a 16-byte authentication hash.
|
|
14
|
+
*/
|
|
15
|
+
export declare function ghash(H: Uint8Array, aad: Uint8Array, ciphertext: Uint8Array): Uint8Array;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
export interface AlgorithmInfo {
|
|
3
|
+
keySize: number;
|
|
4
|
+
ivSize: number;
|
|
5
|
+
mode: 'cbc' | 'ctr' | 'ecb' | 'cfb' | 'ofb' | 'gcm';
|
|
6
|
+
}
|
|
7
|
+
export declare function parseAlgorithm(algorithm: string): AlgorithmInfo;
|
|
8
|
+
export declare function toBuffer(data: string | Buffer | Uint8Array, encoding?: string): Buffer;
|
|
9
|
+
export declare function encodeOutput(data: Uint8Array, encoding?: string): string | Buffer;
|
|
10
|
+
/**
|
|
11
|
+
* Count how many trailing bytes at the end of a Uint8Array form an incomplete
|
|
12
|
+
* UTF-8 multibyte sequence. Returns 0 if the last character is complete.
|
|
13
|
+
*/
|
|
14
|
+
export declare function incompleteUtf8Tail(buf: Uint8Array): number;
|
|
15
|
+
export declare function pkcs7Pad(data: Uint8Array): Uint8Array;
|
|
16
|
+
export declare function pkcs7Unpad(data: Uint8Array): Uint8Array;
|
package/lib/types/cipher.d.ts
CHANGED
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
import { Buffer } from 'node:buffer';
|
|
2
|
-
|
|
3
|
-
keySize: number;
|
|
4
|
-
ivSize: number;
|
|
5
|
-
mode: 'cbc' | 'ctr' | 'ecb' | 'cfb' | 'ofb' | 'gcm';
|
|
6
|
-
}
|
|
2
|
+
import { type AlgorithmInfo } from './cipher-utils.js';
|
|
7
3
|
declare class CipherBase {
|
|
8
4
|
protected _roundKeys: Uint8Array[];
|
|
9
5
|
protected _iv: Uint8Array;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gjsify/crypto",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.22",
|
|
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.
|
|
37
|
-
"@gjsify/unit": "^0.4.
|
|
36
|
+
"@gjsify/cli": "^0.4.22",
|
|
37
|
+
"@gjsify/unit": "^0.4.22",
|
|
38
38
|
"@types/diffie-hellman": "^5.0.3",
|
|
39
|
-
"@types/node": "^25.
|
|
39
|
+
"@types/node": "^25.9.1",
|
|
40
40
|
"typescript": "^6.0.3"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@girs/glib-2.0": "2.88.0-4.0.
|
|
44
|
-
"@gjsify/buffer": "^0.4.
|
|
45
|
-
"@gjsify/stream": "^0.4.
|
|
46
|
-
"@gjsify/utils": "^0.4.
|
|
43
|
+
"@girs/glib-2.0": "2.88.0-4.0.1",
|
|
44
|
+
"@gjsify/buffer": "^0.4.22",
|
|
45
|
+
"@gjsify/stream": "^0.4.22",
|
|
46
|
+
"@gjsify/utils": "^0.4.22"
|
|
47
47
|
}
|
|
48
48
|
}
|