Wallet/js/models/storage/LocalEncrypted.js

51 lines
1.1 KiB
JavaScript
Raw Normal View History

2014-04-07 14:36:57 -03:00
'use strict';
var imports = require('soop').imports();
2014-04-15 18:13:25 -03:00
//var buffertools = imports.buffertools || require('buffertools');
2014-04-16 11:55:18 -03:00
var parent = imports.parent || require('./LocalPlain');
2014-04-07 14:36:57 -03:00
2014-04-15 19:14:42 -03:00
var id = 0;
2014-04-07 14:36:57 -03:00
function Storage() {
2014-04-15 19:14:42 -03:00
this.__uniqueid = ++id;
2014-04-07 14:36:57 -03:00
}
2014-04-15 18:13:25 -03:00
Storage.parent = parent;
2014-04-07 14:36:57 -03:00
2014-04-15 18:13:25 -03:00
2014-04-15 19:14:42 -03:00
var pps = {};
2014-04-15 18:13:25 -03:00
Storage.prototype._getPassphrase = function() {
2014-04-15 19:14:42 -03:00
return pps[this.__uniqueid];
2014-04-15 18:13:25 -03:00
}
Storage.prototype._setPassphrase = function(password) {
2014-04-15 19:14:42 -03:00
pps[this.__uniqueid] = password;
2014-04-15 16:31:00 -03:00
}
Storage.prototype._encrypt = function(data) {
2014-04-15 18:13:25 -03:00
return CryptoJS.AES.encrypt(data, this._getPassphrase());
2014-04-15 16:31:00 -03:00
};
Storage.prototype._decrypt = function(encrypted) {
2014-04-15 18:13:25 -03:00
return CryptoJS.AES.decrypt(encrypted, this._getPassphrase());
2014-04-15 16:31:00 -03:00
};
Storage.prototype._read = function(k) {
var ret;
try {
2014-04-15 18:13:25 -03:00
ret = localStorage.getItem(k);
ret = this._decrypt(ret);
ret = ret.toString(CryptoJS.enc.Utf8);
ret = JSON.parse(ret);
} catch (e) {
console.log('Error while decrypting: '+e);
throw e;
};
2014-04-15 16:31:00 -03:00
return ret;
};
2014-04-15 18:13:25 -03:00
Storage.prototype._write = function(k,v) {
v = JSON.stringify(v);
v = this._encrypt(v);
localStorage.setItem(k, v);
2014-04-15 16:31:00 -03:00
};
2014-04-07 14:36:57 -03:00
module.exports = require('soop')(Storage);