copay/js/plugins/EncryptedInsightStorage.js

50 lines
1.5 KiB
JavaScript
Raw Normal View History

var cryptoUtil = require('../util/crypto');
var InsightStorage = require('./InsightStorage');
var inherits = require('inherits');
function EncryptedInsightStorage(config) {
InsightStorage.apply(this, [config]);
}
inherits(EncryptedInsightStorage, InsightStorage);
2014-11-10 17:58:46 -08:00
EncryptedInsightStorage.prototype._brokenDecrypt = function(body) {
var key = cryptoUtil.kdf(this.password + this.email, 'mjuBtGybi/4=', 100);
log.debug('Trying legacy decrypt')
var decryptedJson = cryptoUtil.decrypt(key, body);
return decryptedJson;
};
EncryptedInsightStorage.prototype.getItem = function(name, callback) {
2014-10-27 06:49:25 -07:00
InsightStorage.prototype.getItem.apply(this, [name,
function(err, body) {
2014-10-28 11:20:43 -07:00
if (err) {
return callback(err);
}
2014-11-10 17:58:46 -08:00
var decryptedJson = cryptoUtil.decrypt(this.password, body);
log.debug('Could not decrypt value using current decryption schema');
if (!decryptedJson) {
decryptedJson = this._brokenDecrypt(body);
}
2014-10-27 06:49:25 -07:00
if (!decryptedJson) {
2014-11-10 17:58:46 -08:00
log.debug('Could not decrypt value.');
return callback('PNOTFOUND');
2014-10-27 06:49:25 -07:00
}
return callback(null, decryptedJson);
}
2014-10-27 06:49:25 -07:00
]);
};
EncryptedInsightStorage.prototype.setItem = function(name, value, callback) {
2014-11-10 17:58:46 -08:00
var record = cryptoUtil.encrypt(this.password, value);
InsightStorage.prototype.setItem.apply(this, [name, record, callback]);
};
2014-10-27 06:49:25 -07:00
EncryptedInsightStorage.prototype.removeItem = function(name, callback) {
InsightStorage.prototype.removeItem.apply(this, [name, callback]);
};
module.exports = EncryptedInsightStorage;