rwadurian/tools/mnemonic-test/node_modules/hash-wasm/dist/pbkdf2.umd.min.js

8 lines
3.8 KiB
JavaScript

/*!
* hash-wasm (https://www.npmjs.com/package/hash-wasm)
* (c) Dani Biro
* @license MIT
*/
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).hashwasm=e.hashwasm||{})}(this,(function(e){"use strict";function t(e,t,n,r){return new(n||(n=Promise))((function(i,o){function u(e){try{f(r.next(e))}catch(e){o(e)}}function a(e){try{f(r.throw(e))}catch(e){o(e)}}function f(e){var t;e.done?i(e.value):(t=e.value,t instanceof n?t:new n((function(e){e(t)}))).then(u,a)}f((r=r.apply(e,t||[])).next())}))}var n;"function"==typeof SuppressedError&&SuppressedError;const r="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:"undefined"!=typeof window?window:global,i=null!==(n=r.Buffer)&&void 0!==n?n:null,o=r.TextEncoder?new r.TextEncoder:null,u="a".charCodeAt(0)-10,a="0".charCodeAt(0);const f=null!==i?e=>{if("string"==typeof e){const t=i.from(e,"utf8");return new Uint8Array(t.buffer,t.byteOffset,t.length)}if(i.isBuffer(e))return new Uint8Array(e.buffer,e.byteOffset,e.length);if(ArrayBuffer.isView(e))return new Uint8Array(e.buffer,e.byteOffset,e.byteLength);throw new Error("Invalid data type!")}:e=>{if("string"==typeof e)return o.encode(e);if(ArrayBuffer.isView(e))return new Uint8Array(e.buffer,e.byteOffset,e.byteLength);throw new Error("Invalid data type!")},s="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",d=new Uint8Array(256);for(let e=0;e<s.length;e++)d[s.charCodeAt(e)]=e;function l(e,t){e.init();const{blockSize:n}=e,r=function(e,t){const{blockSize:n}=e,r=f(t);if(r.length>n){e.update(r);const t=e.digest("binary");return e.init(),t}return new Uint8Array(r.buffer,r.byteOffset,r.length)}(e,t),i=new Uint8Array(n);i.set(r);const o=new Uint8Array(n);for(let e=0;e<n;e++){const t=i[e];o[e]=92^t,i[e]=54^t}e.update(i);const u={init:()=>(e.init(),e.update(i),u),update:t=>(e.update(t),u),digest:t=>{const n=e.digest("binary");return e.init(),e.update(o),e.update(n),e.digest(t)},save:()=>{throw new Error("save() not supported")},load:()=>{throw new Error("load() not supported")},blockSize:e.blockSize,digestSize:e.digestSize};return u}function h(e,n,r,i,o){return t(this,void 0,void 0,(function*(){const t=new Uint8Array(i),s=new Uint8Array(n.length+4),d=new DataView(s.buffer),l=f(n),h=new Uint8Array(l.buffer,l.byteOffset,l.length);s.set(h);let p=0;const c=e.digestSize,y=Math.ceil(i/c);let w=null,b=null;for(let o=1;o<=y;o++){d.setUint32(n.length,o),e.init(),e.update(s),w=e.digest("binary"),b=w.slice();for(let t=1;t<r;t++){e.init(),e.update(b),b=e.digest("binary");for(let e=0;e<c;e++)w[e]^=b[e]}t.set(w.subarray(0,i-p),p),p+=c}if("binary"===o)return t;return function(e,t,n){let r=0;for(let i=0;i<n;i++){let n=t[i]>>>4;e[r++]=n>9?n+u:n+a,n=15&t[i],e[r++]=n>9?n+u:n+a}return String.fromCharCode.apply(null,e)}(new Uint8Array(2*i),t,i)}))}e.pbkdf2=function(e){return t(this,void 0,void 0,(function*(){(e=>{if(!e||"object"!=typeof e)throw new Error("Invalid options parameter. It requires an object.");if(!e.hashFunction||!e.hashFunction.then)throw new Error('Invalid hash function is provided! Usage: pbkdf2("password", "salt", 1000, 32, createSHA1()).');if(!Number.isInteger(e.iterations)||e.iterations<1)throw new Error("Iterations should be a positive number");if(!Number.isInteger(e.hashLength)||e.hashLength<1)throw new Error("Hash length should be a positive number");if(void 0===e.outputType&&(e.outputType="hex"),!["hex","binary"].includes(e.outputType))throw new Error(`Insupported output type ${e.outputType}. Valid values: ['hex', 'binary']`)})(e);return h(yield function(e,t){if(!e||!e.then)throw new Error('Invalid hash function is provided! Usage: createHMAC(createMD5(), "key").');return e.then((e=>l(e,t)))}(e.hashFunction,e.password),e.salt,e.iterations,e.hashLength,e.outputType)}))}}));