UNPKG

@plamikcho/pbcrypto

Version:

Password based AES encrypt/decrypt using web crypto api

2 lines 7.45 kB
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e():"function"==typeof define&&define.amd?define([],e):"object"==typeof exports?exports.pbcrypto=e():t.pbcrypto=e()}(this,function(){return function(t){var e={};function r(n){if(e[n])return e[n].exports;var o=e[n]={i:n,l:!1,exports:{}};return t[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}return r.m=t,r.c=e,r.d=function(t,e,n){r.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},r.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},r.t=function(t,e){if(1&e&&(t=r(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var o in t)r.d(n,o,function(e){return t[e]}.bind(null,o));return n},r.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(e,"a",e),e},r.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},r.p="",r(r.s=50)}({14:function(t,e,r){"use strict";r.d(e,"a",function(){return c}),r.d(e,"b",function(){return a});var n=r(7);function o(t,e,r,n,o,u,c){try{var a=t[u](c),i=a.value}catch(t){return void r(t)}a.done?e(i):Promise.resolve(i).then(n,o)}function u(t){return function(){var e=this,r=arguments;return new Promise(function(n,u){var c=t.apply(e,r);function a(t){o(c,n,u,a,i,"next",t)}function i(t){o(c,n,u,a,i,"throw",t)}a(void 0)})}}var c=function(t,e){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:window.crypto,o=["encrypt","decrypt"],c="SHA-256",a=256,i=function(){var t=u(regeneratorRuntime.mark(function t(e,r){var u,i,f=arguments;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return u=f.length>2&&void 0!==f[2]?f[2]:window.crypto,t.next=3,u.subtle.importKey("raw",Object(n.c)(e),{name:"PBKDF2"},!1,["deriveBits","deriveKey"]);case 3:return i=t.sent,t.abrupt("return",u.subtle.deriveKey({name:"PBKDF2",salt:Object(n.c)(r),iterations:1e3,hash:c},i,{name:"AES-GCM",length:a},!0,o));case 5:case"end":return t.stop()}},t)}));return function(e,r){return t.apply(this,arguments)}}();return{encrypt:function(o,u){return i(t,e).then(function(t){return function(t,e,n){return r.subtle.encrypt({name:"AES-GCM",iv:e},t,n)}(t,u,Object(n.c)(o))}).then(function(t){return Object(n.a)(t)})},decrypt:function(o,u){return i(t,e).then(function(t){return function(t,e,n){return r.subtle.decrypt({name:"AES-GCM",iv:e},t,n)}(t,u,Object(n.c)(o))}).then(function(t){return Object(n.a)(t)})},getIv:function(){return r.getRandomValues(new Uint8Array(16))}}},a=function(){var t=u(regeneratorRuntime.mark(function t(){var e,r,n,o,u;return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:return e="w?",t.prev=1,r=c("a","b"),n=r.getIv(),t.next=6,r.encrypt(e,n);case 6:return o=t.sent,t.next=9,r.decrypt(o,n);case 9:return u=t.sent,t.abrupt("return",u===e);case 13:return t.prev=13,t.t0=t.catch(1),console.warn("Your browser does not support WebCrypto API",t.t0),t.abrupt("return",!1);case 17:case"end":return t.stop()}},t,null,[[1,13]])}));return function(){return t.apply(this,arguments)}}()},50:function(t,e,r){"use strict";r.r(e);var n=r(14),o=r(7);function u(t){return(u="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function c(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function a(t,e,r,n,o,u,c){try{var a=t[u](c),i=a.value}catch(t){return void r(t)}a.done?e(i):Promise.resolve(i).then(n,o)}function i(t){return function(){var e=this,r=arguments;return new Promise(function(n,o){var u=t.apply(e,r);function c(t){a(u,n,o,c,i,"next",t)}function i(t){a(u,n,o,c,i,"throw",t)}c(void 0)})}}var f=function(t,e){var r,u=function(t){return"".concat(t,"_iv")},a={clear:function(){t.clear()},get length(){return t.length},key:function(e){return t.key(e)}},f=function(){var t=i(regeneratorRuntime.mark(function t(){return regeneratorRuntime.wrap(function(t){for(;;)switch(t.prev=t.next){case 0:if(void 0!==r){t.next=4;break}return t.next=3,Object(n.b)();case 3:r=t.sent;case 4:case"end":return t.stop()}},t)}));return function(){return t.apply(this,arguments)}}();return function(t){for(var e=1;e<arguments.length;e++){var r=null!=arguments[e]?arguments[e]:{},n=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(r).filter(function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable}))),n.forEach(function(e){c(t,e,r[e])})}return t}({setItem:function(){var n=i(regeneratorRuntime.mark(function n(c,a){var i,p;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:return n.next=2,f();case 2:if(!r){n.next=18;break}return n.prev=3,i=e.getIv(),n.next=7,e.encrypt(a,i);case 7:p=n.sent,t.setItem(c,String(p)),t.setItem(u(c),Object(o.b)(i)),n.next=16;break;case 12:throw n.prev=12,n.t0=n.catch(3),console.error("Cannot set encrypted value for ".concat(c,". Error: ").concat(n.t0)),n.t0;case 16:n.next=19;break;case 18:t.setItem(c,a);case 19:case"end":return n.stop()}},n,null,[[3,12]])}));return function(t,e){return n.apply(this,arguments)}}(),getItem:function(){var n=i(regeneratorRuntime.mark(function n(c){var a,i,p;return regeneratorRuntime.wrap(function(n){for(;;)switch(n.prev=n.next){case 0:return n.next=2,f();case 2:if(!r){n.next=16;break}return n.prev=3,a=t.getItem(c),i=t.getItem(u(c)),n.next=8,e.decrypt(a,Object(o.d)(i));case 8:return p=n.sent,n.abrupt("return",p);case 12:return n.prev=12,n.t0=n.catch(3),console.error("Cannot get encrypted item for ".concat(c,". Error: ").concat(n.t0)),n.abrupt("return",null);case 16:return n.abrupt("return",t.getItem(c));case 17:case"end":return n.stop()}},n,null,[[3,12]])}));return function(t){return n.apply(this,arguments)}}(),removeItem:function(e){t.removeItem(e);var r=u(e);t.getItem(r)&&t.removeItem(r)}},a)};e.default={getPbCrypto:n.a,getEncryptedStorage:function(t){for(var e=arguments.length,r=new Array(e>1?e-1:0),o=1;o<e;o++)r[o-1]=arguments[o];var c=r[0],a=r[1];return"object"===u(c)?f(t,c):"string"==typeof c&&"string"==typeof a?function(t,e,r){return f(t,Object(n.a)(e,r))}(t,c,a):void 0}}},7:function(t,e,r){"use strict";function n(t){return function(t){if(Array.isArray(t)){for(var e=0,r=new Array(t.length);e<t.length;e++)r[e]=t[e];return r}}(t)||function(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance")}()}r.d(e,"a",function(){return o}),r.d(e,"c",function(){return u}),r.d(e,"b",function(){return c}),r.d(e,"d",function(){return a});var o=function(t){for(var e=new Uint16Array(t),r=e.length,n="",o=Math.pow(2,16)-1,u=0;u<r;u+=o)u+o>r&&(o=r-u),n+=String.fromCharCode.apply(null,e.subarray(u,u+o));return n},u=function(t){for(var e=new Uint16Array(t.length),r=0;r<t.length;r++)e[r]=t.charCodeAt(r);return e.buffer},c=function(t){var e;return(e=String.fromCharCode).call.apply(e,[null].concat(n(new Uint8Array(t))))},a=function(t){return Uint8Array.from(n(t).map(function(t){return t.charCodeAt()})).buffer}}})}); //# sourceMappingURL=index.js.map