2016-09-03 06:03:13 +02:00
|
|
|
|
function CryptoService() {
|
2016-09-02 06:50:57 +02:00
|
|
|
|
|
2016-09-03 06:03:13 +02:00
|
|
|
|
};
|
2016-09-02 06:50:57 +02:00
|
|
|
|
|
2016-09-03 06:03:13 +02:00
|
|
|
|
!function () {
|
|
|
|
|
var _key,
|
|
|
|
|
_b64Key,
|
|
|
|
|
_aes;
|
|
|
|
|
|
|
|
|
|
CryptoService.prototype.setKey = function (key, callback) {
|
2016-09-02 06:50:57 +02:00
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
|
throw 'callback function required';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_key = key;
|
|
|
|
|
chrome.storage.local.set({
|
|
|
|
|
'key': sjcl.codec.base64.fromBits(key)
|
|
|
|
|
}, function () {
|
|
|
|
|
callback();
|
|
|
|
|
});
|
2016-09-03 06:03:13 +02:00
|
|
|
|
}
|
2016-09-02 06:50:57 +02:00
|
|
|
|
|
2016-09-03 06:03:13 +02:00
|
|
|
|
CryptoService.prototype.getKey = function (b64, callback) {
|
2016-09-02 06:50:57 +02:00
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
|
throw 'callback function required';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (b64 && b64 === true && _b64Key) {
|
|
|
|
|
return callback(_b64Key);
|
|
|
|
|
}
|
|
|
|
|
else if (!b64 && _key) {
|
|
|
|
|
return callback(_key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
chrome.storage.local.get('key', function (key) {
|
|
|
|
|
if (key) {
|
|
|
|
|
_key = sjcl.codec.base64.toBits(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (b64 && b64 === true) {
|
|
|
|
|
_b64Key = sjcl.codec.base64.fromBits(_key);
|
|
|
|
|
return callback(_b64Key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return callback(_key);
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-03 06:03:13 +02:00
|
|
|
|
CryptoService.prototype.clearKey = function (callback) {
|
2016-09-02 06:50:57 +02:00
|
|
|
|
if (!callback || typeof callback !== 'function') {
|
|
|
|
|
throw 'callback function required';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_key = _b64Key = _aes = null;
|
|
|
|
|
chrome.storage.local.remove('key', function () {
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-03 06:03:13 +02:00
|
|
|
|
CryptoService.prototype.makeKey = function (password, salt, b64) {
|
2016-09-02 06:50:57 +02:00
|
|
|
|
var key = sjcl.misc.pbkdf2(password, salt, 5000, 256, null);
|
|
|
|
|
|
|
|
|
|
if (b64 && b64 === true) {
|
|
|
|
|
return sjcl.codec.base64.fromBits(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return key;
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-03 06:03:13 +02:00
|
|
|
|
CryptoService.prototype.hashPassword = function (password, key) {
|
2016-09-02 06:50:57 +02:00
|
|
|
|
if (!key) {
|
2016-09-03 06:03:13 +02:00
|
|
|
|
key = this.getKey();
|
2016-09-02 06:50:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!password || !key) {
|
|
|
|
|
throw 'Invalid parameters.';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var hashBits = sjcl.misc.pbkdf2(key, password, 1, 256, null);
|
|
|
|
|
return sjcl.codec.base64.fromBits(hashBits);
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-03 06:03:13 +02:00
|
|
|
|
CryptoService.prototype.getAes = function () {
|
|
|
|
|
if (!_aes && this.getKey()) {
|
|
|
|
|
_aes = new sjcl.cipher.aes(this.getKey());
|
2016-09-02 06:50:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return _aes;
|
|
|
|
|
};
|
|
|
|
|
|
2016-09-03 06:03:13 +02:00
|
|
|
|
CryptoService.prototype.encrypt = function (plaintextValue, key) {
|
|
|
|
|
if (!this.getKey() && !key) {
|
2016-09-02 06:50:57 +02:00
|
|
|
|
throw 'Encryption key unavailable.';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!key) {
|
2016-09-03 06:03:13 +02:00
|
|
|
|
key = this.getKey();
|
2016-09-02 06:50:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var response = {};
|
|
|
|
|
var params = {
|
|
|
|
|
mode: "cbc",
|
|
|
|
|
iv: sjcl.random.randomWords(4, 0)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
var ctJson = sjcl.encrypt(key, plaintextValue, params, response);
|
|
|
|
|
|
|
|
|
|
var ct = ctJson.match(/"ct":"([^"]*)"/)[1];
|
|
|
|
|
var iv = sjcl.codec.base64.fromBits(response.iv);
|
|
|
|
|
|
2016-09-03 18:07:30 +02:00
|
|
|
|
return new CipherString(iv + "|" + ct);
|
2016-09-02 06:50:57 +02:00
|
|
|
|
};
|
|
|
|
|
|
2016-09-03 18:07:30 +02:00
|
|
|
|
CryptoService.prototype.decrypt = function (cipherString) {
|
2016-09-03 06:03:13 +02:00
|
|
|
|
if (!this.getAes()) {
|
2016-09-02 06:50:57 +02:00
|
|
|
|
throw 'AES encryption unavailable.';
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-03 18:07:30 +02:00
|
|
|
|
var ivBits = sjcl.codec.base64.toBits(cipherString.initializationVector);
|
|
|
|
|
var ctBits = sjcl.codec.base64.toBits(cipherString.cipherText);
|
2016-09-02 06:50:57 +02:00
|
|
|
|
|
2016-09-03 06:03:13 +02:00
|
|
|
|
var decBits = sjcl.mode.cbc.decrypt(this.getAes(), ctBits, ivBits, null);
|
2016-09-02 06:50:57 +02:00
|
|
|
|
return sjcl.codec.utf8String.fromBits(decBits);
|
|
|
|
|
};
|
2016-09-02 07:20:41 +02:00
|
|
|
|
}();
|