(function(f,l){typeof exports=="object"&&typeof module!="undefined"?l(exports):typeof define=="function"&&define.amd?define(["exports"],l):(f=typeof globalThis!="undefined"?globalThis:f||self,l(f.networkCrypt={}))})(this,function(f){"use strict";var P=Object.defineProperty;var _=(f,l,y)=>l in f?P(f,l,{enumerable:!0,configurable:!0,writable:!0,value:y}):f[l]=y;var T=(f,l,y)=>(_(f,typeof l!="symbol"?l+"":l,y),y);var l={};l.byteLength=x,l.toByteArray=R,l.fromByteArray=j;for(var y=[],d=[],C=typeof Uint8Array!="undefined"?Uint8Array:Array,m="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",B=0,L=m.length;B0)throw new Error("Invalid string. Length must be a multiple of 4");var e=o.indexOf("=");e===-1&&(e=t);var r=e===t?0:4-e%4;return[e,r]}function x(o){var t=U(o),e=t[0],r=t[1];return(e+r)*3/4-r}function K(o,t,e){return(t+e)*3/4-e}function R(o){var t,e=U(o),r=e[0],i=e[1],s=new C(K(o,r,i)),n=0,h=i>0?r-4:r,c;for(c=0;c>16&255,s[n++]=t>>8&255,s[n++]=t&255;return i===2&&(t=d[o.charCodeAt(c)]<<2|d[o.charCodeAt(c+1)]>>4,s[n++]=t&255),i===1&&(t=d[o.charCodeAt(c)]<<10|d[o.charCodeAt(c+1)]<<4|d[o.charCodeAt(c+2)]>>2,s[n++]=t>>8&255,s[n++]=t&255),s}function S(o){return y[o>>18&63]+y[o>>12&63]+y[o>>6&63]+y[o&63]}function E(o,t,e){for(var r,i=[],s=t;sh?h:n+s));return r===1?(t=o[e-1],i.push(y[t>>2]+y[t<<4&63]+"==")):r===2&&(t=(o[e-2]<<8)+o[e-1],i.push(y[t>>10]+y[t>>4&63]+y[t<<2&63]+"=")),i.join("")}class v{static stringToArrayBufferInUtf8(t){const e=typeof window=="undefined"?require("util").TextEncoder:window.TextEncoder;return new e().encode(t)}static utf8ArrayBufferToString(t){const e=typeof window=="undefined"?require("util").TextDecoder:window.TextDecoder;return new e("utf-8").decode(t)}static arrayBufferToBase64(t){return l.fromByteArray(t)}static base64ToArrayBuffer(t){return l.toByteArray(t)}}const u=16,g=Uint8Array.from([214,144,233,254,204,225,61,183,22,182,20,194,40,251,44,5,43,103,154,118,42,190,4,195,170,68,19,38,73,134,6,153,156,66,80,244,145,239,152,122,51,84,11,67,237,207,172,98,228,179,28,169,201,8,232,149,128,223,148,250,117,143,63,166,71,7,167,252,243,115,23,186,131,89,60,25,230,133,79,168,104,107,129,178,113,100,218,139,248,235,15,75,112,86,157,53,30,36,14,94,99,88,209,162,37,34,124,59,1,33,120,135,212,0,70,87,159,211,39,82,76,54,2,231,160,196,200,158,234,191,138,210,64,199,56,181,163,247,242,206,249,97,21,161,224,174,93,164,155,52,26,85,173,147,50,48,245,140,177,227,29,246,226,46,130,102,202,96,192,41,35,171,13,83,78,111,213,219,55,69,222,253,142,47,3,255,106,114,109,108,91,81,141,27,175,146,187,221,188,127,17,217,92,65,31,16,90,216,10,193,49,136,165,205,123,189,45,116,208,18,184,229,180,176,137,105,151,74,12,150,119,126,101,185,241,9,197,110,198,132,24,240,125,236,58,220,77,32,121,238,95,62,215,203,57,72]),M=Uint32Array.from([462357,472066609,943670861,1415275113,1886879365,2358483617,2830087869,3301692121,3773296373,4228057617,404694573,876298825,1347903077,1819507329,2291111581,2762715833,3234320085,3705924337,4177462797,337322537,808926789,1280531041,1752135293,2223739545,2695343797,3166948049,3638552301,4110090761,269950501,741554753,1213159005,1684763257]),w=Uint32Array.from([2746333894,1453994832,1736282519,2993693404]);class F{constructor(t){T(this,"key");T(this,"iv",null);T(this,"mode","cbc");T(this,"cipherType","text");T(this,"encryptRoundKeys");T(this,"decryptRoundKeys");const e=v.stringToArrayBufferInUtf8(t.key);if(e.length!==16)throw new Error("key should be a 16 bytes string");this.key=e;let r=new Uint8Array(0);if(t.iv!==void 0&&t.iv!==null&&(r=v.stringToArrayBufferInUtf8(t.iv),r.length!==16))throw new Error("iv should be a 16 bytes string");this.iv=r,this.mode="cbc",t.mode&&["cbc","ecb"].includes(t.mode)&&(this.mode=t.mode),this.cipherType="base64",t.outType&&["base64","text"].includes(t.outType)&&(this.cipherType=t.outType),this.encryptRoundKeys=new Uint32Array(32),this.spawnEncryptRoundKeys(),this.decryptRoundKeys=Uint32Array.from(this.encryptRoundKeys),this.decryptRoundKeys.reverse()}doBlockCrypt(t,e){const r=new Uint32Array(36);r.set(t,0);for(let s=0;s<32;s++)r[s+4]=r[s]^this.tTransform1(r[s+1]^r[s+2]^r[s+3]^e[s]);const i=new Uint32Array(4);return i[0]=r[35],i[1]=r[34],i[2]=r[33],i[3]=r[32],i}spawnEncryptRoundKeys(){const t=new Uint32Array(4);t[0]=this.key[0]<<24|this.key[1]<<16|this.key[2]<<8|this.key[3],t[1]=this.key[4]<<24|this.key[5]<<16|this.key[6]<<8|this.key[7],t[2]=this.key[8]<<24|this.key[9]<<16|this.key[10]<<8|this.key[11],t[3]=this.key[12]<<24|this.key[13]<<16|this.key[14]<<8|this.key[15];const e=new Uint32Array(36);e[0]=t[0]^w[0],e[1]=t[1]^w[1],e[2]=t[2]^w[2],e[3]=t[3]^w[3];for(let r=0;r<32;r++)e[r+4]=e[r]^this.tTransform2(e[r+1]^e[r+2]^e[r+3]^M[r]),this.encryptRoundKeys[r]=e[r+4]}rotateLeft(t,e){return t<>>32-e}linearTransform1(t){return t^this.rotateLeft(t,2)^this.rotateLeft(t,10)^this.rotateLeft(t,18)^this.rotateLeft(t,24)}linearTransform2(t){return t^this.rotateLeft(t,13)^this.rotateLeft(t,23)}tauTransform(t){return g[t>>>24&255]<<24|g[t>>>16&255]<<16|g[t>>>8&255]<<8|g[t&255]}tTransform1(t){const e=this.tauTransform(t);return this.linearTransform1(e)}tTransform2(t){const e=this.tauTransform(t);return this.linearTransform2(e)}padding(t){if(t===null)return null;const e=u-t.length%u,r=new Uint8Array(t.length+e);return r.set(t,0),r.fill(e,t.length),r}dePadding(t){if(t===null)return null;const e=t[t.length-1];return t.slice(0,t.length-e)}uint8ToUint32Block(t,e=0){const r=new Uint32Array(4);return r[0]=t[e]<<24|t[e+1]<<16|t[e+2]<<8|t[e+3],r[1]=t[e+4]<<24|t[e+5]<<16|t[e+6]<<8|t[e+7],r[2]=t[e+8]<<24|t[e+9]<<16|t[e+10]<<8|t[e+11],r[3]=t[e+12]<<24|t[e+13]<<16|t[e+14]<<8|t[e+15],r}encrypt(t){const e=v.stringToArrayBufferInUtf8(t),r=this.padding(e),i=r.length/u,s=new Uint8Array(r.length);if(this.mode==="cbc"){if(this.iv===null||this.iv.length!==16)throw new Error("iv error");let n=this.uint8ToUint32Block(this.iv);for(let h=0;h>(3-k)%4*8&255}}else for(let n=0;n>(3-a)%4*8&255}return this.cipherType==="base64"?v.arrayBufferToBase64(s):v.utf8ArrayBufferToString(s)}decrypt(t){let e=new Uint8Array;this.cipherType==="base64"?e=v.base64ToArrayBuffer(t):e=v.stringToArrayBufferInUtf8(t);const r=e.length/u,i=new Uint8Array(e.length);if(this.mode==="cbc"){if(this.iv===null||this.iv.length!==16)throw new Error("iv error");let n=this.uint8ToUint32Block(this.iv);for(let h=0;h>(3-A)%4*8&255}}else for(let n=0;n>(3-a)%4*8&255}const s=this.dePadding(i);return v.utf8ArrayBufferToString(s)}}f.SM4=F,f.default=F,Object.defineProperties(f,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});