Created
October 23, 2013 18:38
-
-
Save nekman/7124141 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Example of encrypted localstorage with CryptoJS. | |
* http://codepen.io/anon/pen/aCDHo | |
* | |
*/ | |
var EncryptedLocalStorage = (function(global) { | |
'use strict'; | |
// Constructor | |
var EncryptedLocalStorage = function(secret) { | |
if (!secret) { | |
throw 'Missing secret!'; | |
} | |
this.secret = secret; | |
}, | |
// Private methods | |
encrypt = function(object, secret) { | |
var message = JSON.stringify(object); | |
return CryptoJS.TripleDES.encrypt(message, secret); | |
}, | |
decrypt = function(encrypted, secret) { | |
var decrypted = CryptoJS.TripleDES.decrypt(encrypted, secret); | |
return JSON.parse(decrypted.toString(CryptoJS.enc.Utf8)); | |
}; | |
// Public API | |
EncryptedLocalStorage.prototype = { | |
get: function(key) { | |
var encrypted = global.localStorage.getItem(key); | |
return encrypted && decrypt(encrypted, this.secret); | |
}, | |
set: function(key, object) { | |
if (!object) { | |
this.remove(key); | |
return; | |
} | |
var encrypted = encrypt(object, this.secret); | |
global.localStorage.setItem(key, encrypted); | |
}, | |
remove: function(key) { | |
global.localStorage.removeItem(key); | |
} | |
}; | |
return EncryptedLocalStorage; | |
}(window)); | |
// Usage | |
var els = new EncryptedLocalStorage('my secret'); | |
els.set('item', { number: 123 }); | |
console.log('encrypted', window.localStorage.getItem('item')); | |
console.log('decrypted', els.get('item')); | |
els.remove('key'); | |
console.log('Removed from localstorage'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment