File: dist/crypt.io.min.js

Recommend this page to a friend!
  Classes of Jason Gerfen   secStore.js   dist/crypt.io.min.js   Download  
File: dist/crypt.io.min.js
Role: Class source
Content type: text/plain
Description: Class source
Class: secStore.js
Store data securely using AES encryption
Author: By
Last change: Update of dist/crypt.io.min.js
Date: 2 years ago
Size: 2,633 bytes
 

Contents

Class file image Download
/* v1.0.7 https://github.com/jas-/crypt.io MIT */ (function(a,b){'use strict';var c=c||function(){var d={passphrase:'',storage:'local'},f=f||{init:function(k){k.passphrase=k.passphrase||h.key(k.passphrase)||h.key()}},g=g||{quota:function(k){var l=/local|session/.test(k)?5248000:4096,m=j.total(k);return 0<l-m},set:function(k,l,m,o){var p=!1;if(g.quota(k.storage)||o('Browser storage quota has been exceeded.'),k.passphrase)try{m=sjcl.encrypt(k.passphrase,g.fromJSON(m))}catch(q){return o(q)}p=this[k.storage]?this[k.storage].set(l,m):this.local.set(l,m),p?o(null,'Successfully set data'):o('Error occured saving data')},get:function(k,l,m){var o=!1;o=this[k.storage]?this[k.storage].get(l):this.local.get(l);try{o=sjcl.decrypt(k.passphrase,o)}catch(p){m(p)}o=g.toJSON(o),o?m(null,o):m('Error occured retrieving storage data')},fromJSON:function(k){var l;try{l=JSON.stringify(k)}catch(m){l=k}return l},toJSON:function(k){var l;try{l=JSON.parse(k)}catch(m){l=k}return l},cookie:{set:function(k,l){var m=new Date;return m.setTime(m.getTime()+2592000000),document.cookie=k+'='+l+';expires='+m.toGMTString()+';path=/;domain='+this.domain(),!0},get:function(k){var l,m,o,p=document.cookie.split(';');for(l=0;l<p.length;l++)if(m=p[l].substr(0,p[l].indexOf('=')),o=p[l].substr(p[l].indexOf('=')+1),m=m.replace(/^\s+|\s+$/g,''),m==k)return unescape(o);return!1},domain:function(){return location.hostname}},local:{set:function(k,l){try{return a.localStorage.setItem(k,l),!0}catch(m){return!1}},get:function(k){return a.localStorage.getItem(k)}},session:{set:function(k,l){try{return a.sessionStorage.setItem(k,l),!0}catch(m){return!1}},get:function(k){return a.sessionStorage.getItem(k)}}},h=h||{key:function(k){k=k||this.muid();var l=h.salt(k);return sjcl.codec.hex.fromBits(sjcl.misc.pbkdf2(k,l))},muid:function(){var k=a.navigator.appName+a.navigator.language+a.navigator.platform;return k},salt:function(){return sjcl.codec.base64.fromBits(sjcl.hash.sha256.hash(a.navigator.appName))}},j=j||{total:function(){var l='',m=a.storage+'Storage';for(var o in m)m.hasOwnProperty(o)&&(l+=m[o]);return l?3+16*l.length/8192:0},size:function(k){var l=0;if(/object/.test(typeof k))for(var m in k)k.hasOwnProperty(k[m])&&l++;else /array/.test(typeof k)&&(l=k.length);return l},merge:function(k,l){for(var m in k=k||{},k)k.hasOwnProperty(m)&&(l[m]=/object/.test(typeof k[m])?this.merge(l[m],k[m]):k[m]),l[m]=k[m];return l}};c.prototype.get=function(k,l,m){m==b&&(m=l,l=k,k={});var o=j.merge(k,d);f.init(o),g.get(o,l,m)},c.prototype.set=function(k,l,m,o){o==b&&(o=m,m=l,l=k,k={});var p=j.merge(k,d);f.init(p),g.set(p,l,m,o)}};a.cryptio=new c})(window);