UNPKG

js-crypto-aes

Version:

Universal Module for AES Encryption and Decryption in JavaScript

1 lines 11.7 kB
!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("crypto")):"function"==typeof define&&define.amd?define(["crypto"],t):"object"==typeof exports?exports.jscaes=t(require("crypto")):e.jscaes=t(e.crypto)}(this,(e=>(()=>{"use strict";var t={698:(e,t,r)=>{t.MX=void 0;t.MX=function(){var e="undefined"!=typeof window&&window.crypto?window.crypto.subtle:"undefined"==typeof window&&"undefined"!=typeof crypto?crypto.subtle:void 0,t="undefined"!=typeof window&&window.crypto||"undefined"==typeof window&&"undefined"!=typeof crypto?void 0:r(100);return void 0!==t?{name:"nodeCrypto",crypto:t}:void 0!==e?{name:"webCrypto",crypto:e}:{name:void 0}}},100:t=>{t.exports=e}},r={};function n(e){var o=r[e];if(void 0!==o)return o.exports;var a=r[e]={exports:{}};return t[e](a,a.exports,n),a.exports}n.d=(e,t)=>{for(var r in t)n.o(t,r)&&!n.o(e,r)&&Object.defineProperty(e,r,{enumerable:!0,get:t[r]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var o={};return(()=>{n.r(o),n.d(o,{decrypt:()=>b,default:()=>A,encrypt:()=>m,unwrapKey:()=>L,wrapKey:()=>E});var e=n(698);const t={ciphers:{"AES-GCM":{nodePrefix:"aes-",nodeSuffix:"-gcm",ivLength:12,tagLength:16,staticIvLength:!0},"AES-CBC":{nodePrefix:"aes-",nodeSuffix:"-cbc",ivLength:16,staticIvLength:!0},"AES-CTR":{nodePrefix:"aes-",nodeSuffix:"-ctr",ivLength:12,staticIvLength:!1}},wrapKeys:{"AES-KW":{nodePrefix:"id-aes",nodeSuffix:"-wrap",ivLength:8,staticIvLength:!0,defaultIv:new Uint8Array([166,166,166,166,166,166,166,166])}}};var r=function(e,t,r,n){var o=r.name,a=r.iv;return i(e,t,{name:o,iv:a},n,!0)},a=function(e,t,r,n){var o=r.name,a=r.iv;return c(e,t,{name:o,iv:a},n,!0)},i=function(e,r,n,o,a){var i=n.name,c=n.iv,p=n.additionalData,s=n.tagLength;void 0===a&&(a=!1);var y,f,h,d,l=u(i,r.byteLength,a?t.wrapKeys:t.ciphers);switch(i){case"AES-GCM":(y=o.createCipheriv(l,r,c,{authTagLength:s})).setAAD(p);break;case"AES-CTR":if(0===c.length||c.length>16)throw new Error("InvalidIVLength");var v=new Uint8Array(16);v.set(c),v[15]+=1,y=o.createCipheriv(l,r,v);break;default:y=o.createCipheriv(l,r,c)}try{f=new Uint8Array(y.update(e)),h=new Uint8Array(y.final()),d=new Uint8Array([]),"AES-GCM"===i&&(d=new Uint8Array(y.getAuthTag()))}catch(e){throw new Error("NodeCrypto_EncryptionFailure")}var w=new Uint8Array(f.length+h.length+d.length);return w.set(f),w.set(h,f.length),w.set(d,f.length+h.length),w},c=function(e,r,n,o,a){var i=n.name,c=n.iv,p=n.additionalData,s=n.tagLength;void 0===a&&(a=!1);var y,f,h,d,l=u(i,r.byteLength,a?t.wrapKeys:t.ciphers);switch(i){case"AES-GCM":(y=o.createDecipheriv(l,r,c,{authTagLength:s})).setAAD(p),f=e.slice(0,e.length-s);var v=e.slice(e.length-s);y.setAuthTag(v);break;case"AES-CTR":if(0===c.length||c.length>16)throw new Error("InvalidIVLength");var w=new Uint8Array(16);w.set(c),w[15]+=1,y=o.createDecipheriv(l,r,w),f=e;break;default:y=o.createDecipheriv(l,r,c),f=e}try{h=y.update(f),d=y.final()}catch(e){throw new Error("NodeCrypto_DecryptionFailure")}var g=new Uint8Array(d.length+h.length);return g.set(h),g.set(d,h.length),g},u=function(e,t,r){var n=r[e].nodePrefix;return(n="".concat(n).concat((8*t).toString()))+r[e].nodeSuffix},p=function(e,t,r,n){return new(r||(r=Promise))((function(o,a){function i(e){try{u(n.next(e))}catch(e){a(e)}}function c(e){try{u(n.throw(e))}catch(e){a(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(i,c)}u((n=n.apply(e,t||[])).next())}))},s=function(e,t){var r,n,o,a,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return a={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function c(c){return function(u){return function(c){if(r)throw new TypeError("Generator is already executing.");for(;a&&(a=0,c[0]&&(i=0)),i;)try{if(r=1,n&&(o=2&c[0]?n.return:c[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,c[1])).done)return o;switch(n=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return i.label++,{value:c[1],done:!1};case 5:i.label++,n=c[1],c=[0];continue;case 7:c=i.ops.pop(),i.trys.pop();continue;default:if(!((o=(o=i.trys).length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){i=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){i.label=c[1];break}if(6===c[0]&&i.label<o[1]){i.label=o[1],o=c;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(c);break}o[2]&&i.ops.pop(),i.trys.pop();continue}c=t.call(e,i)}catch(e){c=[6,e],n=0}finally{r=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,u])}}},y=function(e,t,r,n){var o=r.name,a=r.iv;return p(void 0,void 0,Promise,(function(){var r,i,c,u;return s(this,(function(p){switch(p.label){case 0:return p.trys.push([0,4,,5]),[4,n.importKey("raw",t,{name:o},!1,["wrapKey","unwrapKey"])];case 1:return r=p.sent(),[4,n.importKey("raw",e,{name:o},!0,["wrapKey","unwrapKey"])];case 2:return i=p.sent(),[4,n.wrapKey("raw",i,r,{name:o,iv:a})];case 3:return c=p.sent(),[2,new Uint8Array(c)];case 4:throw(u=p.sent())instanceof Error?new Error("WebCrypto_FailedToWrapKey - ".concat(u.message)):new Error("WebCrypto_FailedToWrapKey");case 5:return[2]}}))}))},f=function(e,t,r,n){var o=r.name,a=r.iv;return p(void 0,void 0,Promise,(function(){var r,i,c,u;return s(this,(function(p){switch(p.label){case 0:return p.trys.push([0,4,,5]),[4,n.importKey("raw",t,{name:o},!1,["wrapKey","unwrapKey"])];case 1:return r=p.sent(),[4,n.unwrapKey("raw",e,r,{name:o,iv:a},{name:"AES-GCM"},!0,["encrypt","decrypt"])];case 2:return i=p.sent(),c=Uint8Array.bind,[4,n.exportKey("raw",i)];case 3:return[2,new(c.apply(Uint8Array,[void 0,p.sent()]))];case 4:throw(u=p.sent())instanceof Error?new Error("WebCrypto_FailedToUnwrapKey - ".concat(u.message)):new Error("WebCrypto_FailedToUnwrapKey");case 5:return[2]}}))}))},h=function(e,t,r,n){var o=r.name,a=void 0===o?"AES-GCM":o,i=r.iv,c=r.additionalData,u=r.tagLength;return p(void 0,void 0,Promise,(function(){var r,o,p,y;return s(this,(function(s){switch(s.label){case 0:r=l({name:a,iv:i,additionalData:c,tagLength:u}),s.label=1;case 1:return s.trys.push([1,4,,5]),[4,n.importKey("raw",t,r,!1,["encrypt","decrypt"])];case 2:return o=s.sent(),[4,n.encrypt(r,o,e)];case 3:return p=s.sent(),[2,new Uint8Array(p)];case 4:throw(y=s.sent())instanceof Error?new Error("WebCrypto_EncryptionFailure: ".concat(y.message)):new Error("WebCrypto_EncryptionFailure");case 5:return[2]}}))}))},d=function(e,t,r,n){var o=r.name,a=r.iv,i=r.additionalData,c=r.tagLength;return p(void 0,void 0,Promise,(function(){var r,u,p,y;return s(this,(function(s){switch(s.label){case 0:r=l({name:o,iv:a,additionalData:i,tagLength:c}),s.label=1;case 1:return s.trys.push([1,4,,5]),[4,n.importKey("raw",t,r,!1,["encrypt","decrypt"])];case 2:return u=s.sent(),[4,n.decrypt(r,u,e)];case 3:return p=s.sent(),[2,new Uint8Array(p)];case 4:throw(y=s.sent())instanceof Error?new Error("WebCrypto_DecryptionFailure: ".concat(y.message)):new Error("WebCrypto_DecryptionFailure");case 5:return[2]}}))}))},l=function(e){var t=e.name,r=e.iv,n=e.additionalData,o=e.tagLength,a={name:t,iv:r,additionalData:n,tagLength:o};switch(t){case"AES-GCM":a.tagLength=8*o;break;case"AES-CBC":break;case"AES-CTR":if(0===r.length||r.length>16)throw new Error("InvalidIVLength");a.counter=new Uint8Array(16),a.counter.set(r),a.counter[15]+=1,a.length=128}return a},v=function(e,t,r,n){return new(r||(r=Promise))((function(o,a){function i(e){try{u(n.next(e))}catch(e){a(e)}}function c(e){try{u(n.throw(e))}catch(e){a(e)}}function u(e){var t;e.done?o(e.value):(t=e.value,t instanceof r?t:new r((function(e){e(t)}))).then(i,c)}u((n=n.apply(e,t||[])).next())}))},w=function(e,t){var r,n,o,a,i={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return a={next:c(0),throw:c(1),return:c(2)},"function"==typeof Symbol&&(a[Symbol.iterator]=function(){return this}),a;function c(c){return function(u){return function(c){if(r)throw new TypeError("Generator is already executing.");for(;a&&(a=0,c[0]&&(i=0)),i;)try{if(r=1,n&&(o=2&c[0]?n.return:c[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,c[1])).done)return o;switch(n=0,o&&(c=[2&c[0],o.value]),c[0]){case 0:case 1:o=c;break;case 4:return i.label++,{value:c[1],done:!1};case 5:i.label++,n=c[1],c=[0];continue;case 7:c=i.ops.pop(),i.trys.pop();continue;default:if(!((o=(o=i.trys).length>0&&o[o.length-1])||6!==c[0]&&2!==c[0])){i=0;continue}if(3===c[0]&&(!o||c[1]>o[0]&&c[1]<o[3])){i.label=c[1];break}if(6===c[0]&&i.label<o[1]){i.label=o[1],o=c;break}if(o&&i.label<o[2]){i.label=o[2],i.ops.push(c);break}o[2]&&i.ops.pop(),i.trys.pop();continue}c=t.call(e,i)}catch(e){c=[6,e],n=0}finally{r=o=0}if(5&c[0])throw c[1];return{value:c[0]?c[1]:void 0,done:!0}}([c,u])}}},g=function(e){var r=e.name,n=e.iv,o=e.tagLength;if(t.ciphers[r].ivLength){if(!(n instanceof Uint8Array))throw new Error("InvalidArguments");if(n.byteLength<2||n.byteLength>16)throw new Error("InvalidIVLength");if(t.ciphers[r].staticIvLength&&t.ciphers[r].ivLength!==n.byteLength)throw new Error("InvalidIVLength")}if(t.ciphers[r].tagLength&&o){if(!Number.isInteger(o))throw new Error("InvalidArguments");if(o<4||o>16)throw new Error("InvalidTagLength")}},m=function(r,n,o){var a=o.name,c=void 0===a?"AES-GCM":a,u=o.iv,p=o.additionalData,s=void 0===p?new Uint8Array([]):p,y=o.tagLength;return v(void 0,void 0,Promise,(function(){var o;return w(this,(function(a){if(g({name:c,iv:u,tagLength:y}),t.ciphers[c].tagLength&&!y&&(y=t.ciphers[c].tagLength),"webCrypto"===(o=e.MX()).name){if("function"!=typeof o.crypto.importKey||"function"!=typeof o.crypto.encrypt)throw new Error("UnsupportedWebCrypto");return[2,h(r,n,{name:c,iv:u,additionalData:s,tagLength:y},o.crypto)]}if("nodeCrypto"===o.name)return[2,i(r,n,{name:c,iv:u,additionalData:s,tagLength:y},o.crypto)];throw new Error("UnsupportedEnvironment")}))}))},b=function(r,n,o){var a=o.name,i=void 0===a?"AES-GCM":a,u=o.iv,p=o.additionalData,s=void 0===p?new Uint8Array([]):p,y=o.tagLength;return v(void 0,void 0,Promise,(function(){var o;return w(this,(function(a){if(g({name:i,iv:u,tagLength:y}),t.ciphers[i].tagLength&&!y&&(y=t.ciphers[i].tagLength),"webCrypto"===(o=e.MX()).name){if("function"!=typeof o.crypto.importKey||"function"!=typeof o.crypto.decrypt)throw new Error("UnsupportedWebCrypto");return[2,d(r,n,{name:i,iv:u,additionalData:s,tagLength:y},o.crypto)]}if("nodeCrypto"===o.name)return[2,c(r,n,{name:i,iv:u,additionalData:s,tagLength:y},o.crypto)];throw new Error("UnsupportedEnvironment")}))}))},E=function(n,o,a){var i=a.name;return v(void 0,void 0,Promise,(function(){var a,c;return w(this,(function(u){if(n.length%8>0)throw new Error("WrappedKeyMustBeMultipleOf8");if(a=e.MX(),c=t.wrapKeys["AES-KW"].defaultIv,"webCrypto"===a.name){if("function"!=typeof a.crypto.importKey||"function"!=typeof a.crypto.wrapKey)throw new Error("UnsupportedWebCrypto");return[2,y(n,o,{name:i,iv:c},a.crypto)]}if("nodeCrypto"===a.name)return[2,r(n,o,{name:i,iv:c},a.crypto)];throw new Error("UnsupportedEnvironment")}))}))},L=function(r,n,o){var i=o.name;return v(void 0,void 0,void 0,(function(){var o,c;return w(this,(function(u){if(o=e.MX(),c=t.wrapKeys["AES-KW"].defaultIv,"webCrypto"===o.name){if("function"!=typeof o.crypto.importKey||"function"!=typeof o.crypto.unwrapKey)throw new Error("UnsupportedWebCrypto");return[2,f(r,n,{name:i,iv:c},o.crypto)]}if("nodeCrypto"===o.name)return[2,a(r,n,{name:i,iv:c},o.crypto)];throw new Error("UnsupportedEnvironment")}))}))};const A={encrypt:m,decrypt:b,wrapKey:E,unwrapKey:L}})(),o})()));