UNPKG

iocane

Version:
1 lines 13.7 kB
!function(r,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.iocane=t():r.iocane=t()}(self,(()=>(()=>{"use strict";var r={d:(t,n)=>{for(var e in n)r.o(n,e)&&!r.o(t,e)&&Object.defineProperty(t,e,{enumerable:!0,get:n[e]})},o:(r,t)=>Object.prototype.hasOwnProperty.call(r,t),r:r=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(r,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(r,"__esModule",{value:!0})}},t={};r.r(t),r.d(t,{ALGO_DEFAULT:()=>e,DERIVED_KEY_ALGORITHM:()=>o,DERIVED_KEY_ITERATIONS:()=>a,EncryptionAlgorithm:()=>n,HMAC_KEY_SIZE:()=>i,NODE_ENC_ALGORITHM_CBC:()=>u,NODE_ENC_ALGORITHM_GCM:()=>c,NODE_HMAC_ALGORITHM:()=>l,PASSWORD_KEY_SIZE:()=>f,SALT_LENGTH:()=>s,SIZE_ENCODING_BYTES:()=>y,createAdapter:()=>or});var n=function(r){return r.CBC="cbc",r.GCM="gcm",r}({}),e=n.CBC,o="sha256",a=25e4,i=32,u="aes-256-cbc",c="aes-256-gcm",l="sha256",f=32,s=12,y=4;function d(r,t){if(r.byteLength!==t.byteLength)return!1;for(var n=new Uint8Array(r),e=new Uint8Array(t),o=0;o<r.byteLength;o+=1)if(n[o]!==e[o])return!1;return!0}function p(r){var t=Array.from(new Uint8Array(r)).map((function(r){return String.fromCharCode(r)})).join("");return btoa(t)}function h(r){for(var t,n=new Uint8Array(r),e="",o=0;o<n.byteLength;o+=1)(t=n[o].toString(16)).length<2&&(t="0"+t),e+=t;return e}function v(r){return(new TextDecoder).decode(r)}function m(r){for(var t=atob(r),n=new Uint8Array(new ArrayBuffer(t.length)),e=0;e<t.length;e+=1)n[e]=t.charCodeAt(e);return n.buffer}function b(r){if(r.length<=0)throw new Error("Failed concatenating array buffers: At least one must be passed");var t=r.reduce((function(r,t){return r+t.byteLength}),0),n=new Uint8Array(t),e=0;return r.forEach((function(r){n.set(new Uint8Array(r),e),e+=r.byteLength})),n.buffer}function g(r){var t=r.match(/[\da-f]{2}/gi).map((function(r){return parseInt(r,16)}));return new Uint8Array(t).buffer}function w(r){return(new TextEncoder).encode(r)}function A(r,t,n){return n?t?t(r):r:(r&&r.then||(r=Promise.resolve(r)),t?r.then(t):r)}function C(r){return function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];try{return Promise.resolve(r.apply(this,t))}catch(r){return Promise.reject(r)}}}function S(r,t){(null==t||t>r.length)&&(t=r.length);for(var n=0,e=new Array(t);n<t;n++)e[n]=r[n];return e}var E=C((function(r){if(r<=0)throw new Error("Failed generating salt: Invalid length supplied: ".concat(r));for(var t=K(),n="";n.length<r;){var e=new Uint8Array(3);t.getRandomValues(e);var o=e.reduce((function(r,t){return r+String.fromCharCode(t)}),"");(n+=btoa(o)).length>r&&(n=n.substr(0,r))}return n})),O=C((function(){var r=new Uint8Array(16);return K().getRandomValues(r),r.buffer})),I=C((function(r,t,e){var o=K(),a=t.rounds,i=new Uint8Array(e),u=h(e),c="string"==typeof r,l=c?w(r):r;return A(o.subtle.importKey("raw",t.key,{name:M},!1,["encrypt"]),(function(r){return A(o.subtle.encrypt({name:M,iv:i,additionalData:w("".concat(u).concat(t.salt))},r,l),(function(r){var e,o,i,l,f,s,y=(o=B/8,i=(e=r).slice(0,e.byteLength-o),l=e.slice(e.byteLength-o),s=2,function(r){if(Array.isArray(r))return r}(f=[i,l])||function(r,t){var n=null==r?null:"undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(null!=n){var e,o,a,i,u=[],c=!0,l=!1;try{if(a=(n=n.call(r)).next,0===t){if(Object(n)!==n)return;c=!1}else for(;!(c=(e=a.call(n)).done)&&(u.push(e.value),u.length!==t);c=!0);}catch(r){l=!0,o=r}finally{try{if(!c&&null!=n.return&&(i=n.return(),Object(i)!==i))return}finally{if(l)throw o}}return u}}(f,s)||function(r,t){if(r){if("string"==typeof r)return S(r,t);var n=Object.prototype.toString.call(r).slice(8,-1);return"Object"===n&&r.constructor&&(n=r.constructor.name),"Map"===n||"Set"===n?Array.from(r):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?S(r,t):void 0}}(f,s)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()),d=y[0],v=h(y[1]),m=c?p(d):d;return{method:n.GCM,iv:u,salt:t.salt,rounds:a,content:m,auth:v}}))}))})),j=C((function(r,t,e){var o=K(),a=t.rounds,i=t.salt,u=new Uint16Array(e),c=h(e),l="string"==typeof r,f=l?w(r):r;return A(o.subtle.importKey("raw",t.key,{name:T},!1,["encrypt"]),(function(r){return A(o.subtle.importKey("raw",t.hmac,{name:x,hash:_},!1,["sign"]),(function(t){return A(o.subtle.encrypt({name:T,iv:u},r,f),(function(r){var e=l?p(r):r,o=l?w("".concat(e).concat(c).concat(i)):b([e,w(c),w(i)]);return A(window.crypto.subtle.sign(x,t,o),(function(r){var t=h(r);return{method:n.CBC,auth:t,iv:c,salt:i,rounds:a,content:e}}))}))}))}))})),U=C((function(r,t){var n=K(),e=r.auth,o=r.content,a=r.iv,i=r.salt,u=g(a),c=g(e),l="string"==typeof o,f=b(l?[m(o),c]:[o,c]);return A(n.subtle.importKey("raw",t.key,{name:M},!1,["decrypt"]),(function(r){return A(n.subtle.decrypt({name:M,iv:u,additionalData:w("".concat(a).concat(i))},r,f),(function(r){return l?v(r):r}))}))})),D=C((function(r,t){var n=K(),e=r.auth,o=r.content,a=r.iv,i=r.salt,u=g(a),c=w(a),l=w(i),f=g(e),s="string"==typeof o,y=s?m(o):o;return A(n.subtle.importKey("raw",t.key,{name:T},!1,["decrypt"]),(function(r){return A(n.subtle.importKey("raw",t.hmac,{name:x,hash:_},!1,["verify"]),(function(t){var e=s?w("".concat(o).concat(a).concat(i)):b([y,c,l]);return A(n.subtle.verify(x,t,f,e),(function(t){if(!t)throw new Error("Authentication failed while decrypting content");return A(n.subtle.decrypt({name:T,iv:u},r,y),(function(r){return s?v(r):r}))}))}))}))})),T="AES-CBC",M="AES-GCM",B=128,_="SHA-256",x="HMAC";function K(){return window.crypto||window.msCrypto}function P(r,t,n){return n?t?t(r):r:(r&&r.then||(r=Promise.resolve(r)),t?r.then(t):r)}function L(r){return function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];try{return Promise.resolve(r.apply(this,t))}catch(r){return Promise.reject(r)}}}function k(r,t){return function(r){if(Array.isArray(r))return r}(r)||function(r,t){var n=null==r?null:"undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(null!=n){var e,o,a,i,u=[],c=!0,l=!1;try{if(a=(n=n.call(r)).next,0===t){if(Object(n)!==n)return;c=!1}else for(;!(c=(e=a.call(n)).done)&&(u.push(e.value),u.length!==t);c=!0);}catch(r){l=!0,o=r}finally{try{if(!c&&null!=n.return&&(i=n.return(),Object(i)!==i))return}finally{if(l)throw o}}return u}}(r,t)||function(r,t){if(r){if("string"==typeof r)return G(r,t);var n=Object.prototype.toString.call(r).slice(8,-1);return"Object"===n&&r.constructor&&(n=r.constructor.name),"Map"===n||"Set"===n?Array.from(r):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?G(r,t):void 0}}(r,t)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function G(r,t){(null==t||t>r.length)&&(t=r.length);for(var n=0,e=new Array(t);n<t;n++)e[n]=r[n];return e}var R=L((function(r,t,n,e){!function(){if(!window.TextEncoder||!window.TextDecoder)throw new Error("TextEncoder is not available")}();var o=window.crypto.subtle,a={name:"PBKDF2",hash:"SHA-256",salt:w(t),iterations:n},i=e/8/2;return P(o.importKey("raw",w(r),{name:"PBKDF2"},!1,["deriveBits"]),(function(r){return P(o.deriveBits(a,r,e),(function(r){return P(Promise.all([o.importKey("raw",r.slice(0,i),"AES-CBC",!0,["encrypt","decrypt"]),o.importKey("raw",r.slice(i,2*i),"AES-CBC",!0,["encrypt","decrypt"])]),(function(r){var t=k(r,2),n=t[0],e=t[1];return P(Promise.all([o.exportKey("raw",n),o.exportKey("raw",e)]),(function(r){var t=k(r,2);return b([t[0],t[1]])}))}))}))}))})),N=L((function(r,t,n){var e=!(arguments.length>3&&void 0!==arguments[3])||arguments[3];if(!r)throw new Error("Failed deriving key: Password must be provided");if(!t)throw new Error("Failed deriving key: Salt must be provided");if(!n||n<=0)throw new Error("Failed deriving key: Rounds must be greater than 0");return P(R(r,t,n,e?8*(f+i):8*f),(function(r){var o=h(r),a=o.length,i=g(e?o.substr(0,a/2):o);return{salt:t,key:i,rounds:n,hmac:e?g(o.substr(a/2,a/2)):null}}))}));function F(r,t){(null==t||t>r.length)&&(t=r.length);for(var n=0,e=new Array(t);n<t;n++)e[n]=r[n];return e}var H=1e3;function V(r){return[r.content,r.iv,r.salt,r.auth,r.rounds,r.method].join("$")}function $(r){var t,n,o=(t=r.split("$"),n=6,function(r){if(Array.isArray(r))return r}(t)||function(r,t){var n=null==r?null:"undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(null!=n){var e,o,a,i,u=[],c=!0,l=!1;try{if(a=(n=n.call(r)).next,0===t){if(Object(n)!==n)return;c=!1}else for(;!(c=(e=a.call(n)).done)&&(u.push(e.value),u.length!==t);c=!0);}catch(r){l=!0,o=r}finally{try{if(!c&&null!=n.return&&(i=n.return(),Object(i)!==i))return}finally{if(l)throw o}}return u}}(t,n)||function(r,t){if(r){if("string"==typeof r)return F(r,t);var n=Object.prototype.toString.call(r).slice(8,-1);return"Object"===n&&r.constructor&&(n=r.constructor.name),"Map"===n||"Set"===n?Array.from(r):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?F(r,t):void 0}}(t,n)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()),a=o[0],i=o[1],u=o[2],c=o[3],l=o[4],f=o[5];return{content:a,iv:i,salt:u,auth:c,rounds:l?parseInt(l,10):H,method:f||e}}var Y="iocane/2";function J(){return Y.split("").map((function(r){return r.charCodeAt(0)}))}function Z(r){return b(r.reduce((function(r,t){var n,e,o;if("string"==typeof t||"number"==typeof t)n=w("".concat(t));else{if(!(t instanceof ArrayBuffer))throw new Error("Failed packing data: Invalid component type: ".concat(typeof t));n=t}return r.push((e=n.byteLength,o=new ArrayBuffer(4),new DataView(o).setUint32(0,e,!1),o)),r.push(n),r}),[]))}function W(r){var t=new Uint8Array([1,0,0,0,0,0,0,1]).buffer,n=[q(r)];return r.content&&n.push(Z([t]),r.content,t),n.push(function(r){var t=r.auth;return Z([JSON.stringify({auth:t})])}(r)),b(n)}function q(r){var t=new Uint8Array(J()).buffer,n=r.iv,e=r.salt,o=r.rounds,a=r.method;return b([t,Z([JSON.stringify({iv:n,salt:e,rounds:o,method:a})])])}function z(r){var t=0,n=new Uint8Array(J()).buffer,e=n.byteLength;if(!d(r.slice(0,e),n))throw new Error("Failed unpacking data: Signature mismatch");t=e;var o=new DataView(r),a=o.getUint32(t);t+=y;var i=r.slice(t,t+a);t+=a;var u=o.getUint32(t);t+=y;var c=r.slice(t,t+u);if(t+=u,!d(new Uint8Array([1,0,0,0,0,0,0,1]),c))throw new Error("Decoding error: Encrypted content length is corrupt");var l=function(r,t){var n=arguments.length>2&&void 0!==arguments[2]?arguments[2]:0;return new Uint8Array(r).findIndex((function(r,e,o){if(e<n)return!1;var a=o.slice(e,e+t.byteLength).buffer;return Boolean(d(t,a))}))}(r,c,t);if(-1===l)throw new Error("Decoding error: Encrypted content corrupt or incomplete");var f=r.slice(t,l);t=l+u;var s=o.getUint32(t);t+=y;var p=r.slice(t,t+s);t+=s;var h=JSON.parse(v(i)),m=h.iv,b=h.salt,g=h.rounds,w=h.method;return{content:f,iv:m,salt:b,auth:JSON.parse(v(p)).auth,rounds:g,method:w}}function Q(r,t,n){return n?t?t(r):r:(r&&r.then||(r=Promise.resolve(r)),t?r.then(t):r)}function X(r){return function(){for(var t=[],n=0;n<arguments.length;n++)t[n]=arguments[n];try{return Promise.resolve(r.apply(this,t))}catch(r){return Promise.reject(r)}}}function rr(r,t){(null==t||t>r.length)&&(t=r.length);for(var n=0,e=new Array(t);n<t;n++)e[n]=r[n];return e}var tr=X((function(r,t,e){var o=r.algorithm,a=function(r,t){if(t===n.CBC)return r.encryptCBC;if(t===n.GCM)return r.encryptGCM;throw new Error("Invalid algorithm: ".concat(t))}(r,o);return Q(r.generateSalt(s),(function(n){return Q(Promise.all([r.deriveKey(e,n),r.generateIV()]),(function(n){var e,o,i=(o=2,function(r){if(Array.isArray(r))return r}(e=n)||function(r,t){var n=null==r?null:"undefined"!=typeof Symbol&&r[Symbol.iterator]||r["@@iterator"];if(null!=n){var e,o,a,i,u=[],c=!0,l=!1;try{if(a=(n=n.call(r)).next,0===t){if(Object(n)!==n)return;c=!1}else for(;!(c=(e=a.call(n)).done)&&(u.push(e.value),u.length!==t);c=!0);}catch(r){l=!0,o=r}finally{try{if(!c&&null!=n.return&&(i=n.return(),Object(i)!==i))return}finally{if(l)throw o}}return u}}(e,o)||function(r,t){if(r){if("string"==typeof r)return rr(r,t);var n=Object.prototype.toString.call(r).slice(8,-1);return"Object"===n&&r.constructor&&(n=r.constructor.name),"Map"===n||"Set"===n?Array.from(r):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?rr(r,t):void 0}}(e,o)||function(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()),u=i[0],c=i[1];return Q(a(t,u,c),(function(n){return"string"==typeof t?r.packText(n):r.packData(n)}))}))}))})),nr=X((function(r,t,e){var o=r.algorithm,a=r.derivationRounds;return N(t,e,a,o!==n.GCM)})),er=X((function(r,t,e){var o="string"==typeof t?r.unpackText(t):r.unpackData(t),a=o.salt,i=o.rounds,u=o.method,c=function(r,t){if(t===n.CBC)return r.decryptCBC;if(t===n.GCM)return r.decryptGCM;throw new Error("Invalid algorithm: ".concat(t))}(r,u);return r.algorithm=u,r.derivationRounds=i,Q(r.deriveKey(e,a),(function(r){return c(o,r)}))}));function or(){var r={algorithm:e,decrypt:function(t,n){return er(r,t,n)},decryptCBC:D,decryptGCM:U,derivationRounds:a,deriveKey:function(t,n){return nr(r,t,n)},encrypt:function(t,n){return tr(r,t,n)},encryptCBC:j,encryptGCM:I,generateIV:O,generateSalt:E,packData:W,packText:V,setAlgorithm:function(t){return r.algorithm=t,r},setDerivationRounds:function(t){return r.derivationRounds=t,r},unpackData:z,unpackText:$};return r}return t})()));