mirror of
https://github.com/moparisthebest/mail
synced 2024-11-25 18:32:20 -05:00
Refactor crypto module
This commit is contained in:
parent
f4fe1a36a6
commit
88a48ec540
@ -13,27 +13,22 @@ var aes = require('crypto-lib').aes,
|
||||
* High level crypto api that invokes native crypto (if available) and
|
||||
* gracefully degrades to JS crypto (if unavailable)
|
||||
*/
|
||||
function Crypto() {}
|
||||
function Crypto($q) {
|
||||
this._q = $q;
|
||||
}
|
||||
|
||||
/**
|
||||
* Encrypt plaintext using AES-GCM.
|
||||
* @param {String} plaintext The input string in UTF-16
|
||||
* @param {String} key The base64 encoded key
|
||||
* @param {String} iv The base64 encoded IV
|
||||
* @param {Function} callback(error, ciphertext)
|
||||
* @return {String} The base64 encoded ciphertext
|
||||
*/
|
||||
Crypto.prototype.encrypt = function(plaintext, key, iv, callback) {
|
||||
var ct;
|
||||
|
||||
try {
|
||||
ct = aes.encrypt(plaintext, key, iv);
|
||||
} catch (err) {
|
||||
callback(err);
|
||||
return;
|
||||
}
|
||||
|
||||
callback(null, ct);
|
||||
Crypto.prototype.encrypt = function(plaintext, key, iv) {
|
||||
return this._q(function(resolve) {
|
||||
var ct = aes.encrypt(plaintext, key, iv);
|
||||
resolve(ct);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
@ -41,34 +36,26 @@ Crypto.prototype.encrypt = function(plaintext, key, iv, callback) {
|
||||
* @param {String} ciphertext The base64 encoded ciphertext
|
||||
* @param {String} key The base64 encoded key
|
||||
* @param {String} iv The base64 encoded IV
|
||||
* @param {Function} callback(error, plaintext)
|
||||
* @return {String} The decrypted plaintext in UTF-16
|
||||
*/
|
||||
Crypto.prototype.decrypt = function(ciphertext, key, iv, callback) {
|
||||
var pt;
|
||||
|
||||
try {
|
||||
pt = aes.decrypt(ciphertext, key, iv);
|
||||
} catch (err) {
|
||||
callback(err);
|
||||
return;
|
||||
}
|
||||
|
||||
callback(null, pt);
|
||||
Crypto.prototype.decrypt = function(ciphertext, key, iv) {
|
||||
return this._q(function(resolve) {
|
||||
var pt = aes.decrypt(ciphertext, key, iv);
|
||||
resolve(pt);
|
||||
});
|
||||
};
|
||||
|
||||
/**
|
||||
* Do PBKDF2 key derivation in a WebWorker thread
|
||||
*/
|
||||
Crypto.prototype.deriveKey = function(password, salt, keySize, callback) {
|
||||
startWorker({
|
||||
Crypto.prototype.deriveKey = function(password, salt, keySize) {
|
||||
return this.startWorker({
|
||||
script: config.workerPath + '/pbkdf2-worker.min.js',
|
||||
args: {
|
||||
password: password,
|
||||
salt: salt,
|
||||
keySize: keySize
|
||||
},
|
||||
callback: callback,
|
||||
noWorker: function() {
|
||||
return pbkdf2.getKey(password, salt, keySize);
|
||||
}
|
||||
@ -79,27 +66,25 @@ Crypto.prototype.deriveKey = function(password, salt, keySize, callback) {
|
||||
// helper functions
|
||||
//
|
||||
|
||||
function startWorker(options) {
|
||||
Crypto.prototype.startWorker = function(options) {
|
||||
return this._q(function(resolve, reject) {
|
||||
// check for WebWorker support
|
||||
if (window.Worker) {
|
||||
// init webworker thread
|
||||
var worker = new Worker(options.script);
|
||||
worker.onmessage = function(e) {
|
||||
if (e.data.err) {
|
||||
options.callback(e.data.err);
|
||||
return;
|
||||
}
|
||||
// return result from the worker
|
||||
options.callback(null, e.data);
|
||||
if (e.data.err) {
|
||||
reject(e.data.err);
|
||||
} else {
|
||||
resolve(e.data);
|
||||
}
|
||||
};
|
||||
worker.onerror = function(e) {
|
||||
// show error message in logger
|
||||
axe.error('Error handling web worker: Line ' + e.lineno + ' in ' + e.filename + ': ' + e.message);
|
||||
// return error
|
||||
options.callback({
|
||||
errMsg: (e.message) ? e.message : e
|
||||
});
|
||||
return;
|
||||
reject(e);
|
||||
};
|
||||
// send data to the worker
|
||||
worker.postMessage(options.args);
|
||||
@ -107,15 +92,7 @@ function startWorker(options) {
|
||||
}
|
||||
|
||||
// no WebWorker support... do synchronous call
|
||||
var result;
|
||||
try {
|
||||
result = options.noWorker();
|
||||
} catch (e) {
|
||||
// return error
|
||||
options.callback({
|
||||
errMsg: (e.message) ? e.message : e
|
||||
var result = options.noWorker();
|
||||
resolve(result);
|
||||
});
|
||||
return;
|
||||
}
|
||||
options.callback(null, result);
|
||||
}
|
||||
};
|
@ -13,7 +13,7 @@ describe('Crypto unit tests', function() {
|
||||
ivSize = config.symIvSize;
|
||||
|
||||
beforeEach(function() {
|
||||
crypto = new Crypto();
|
||||
crypto = new Crypto(qMock);
|
||||
});
|
||||
|
||||
afterEach(function() {});
|
||||
@ -24,32 +24,27 @@ describe('Crypto unit tests', function() {
|
||||
var key = util.random(keySize);
|
||||
var iv = util.random(ivSize);
|
||||
|
||||
crypto.encrypt(plaintext, key, iv, function(err, ciphertext) {
|
||||
expect(err).to.not.exist;
|
||||
crypto.encrypt(plaintext, key, iv).then(function(ciphertext) {
|
||||
expect(ciphertext).to.exist;
|
||||
|
||||
crypto.decrypt(ciphertext, key, iv, function(err, decrypted) {
|
||||
expect(err).to.not.exist;
|
||||
return crypto.decrypt(ciphertext, key, iv);
|
||||
}).then(function(decrypted) {
|
||||
expect(decrypted).to.equal(plaintext);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("PBKDF2 (Async/Worker)", function() {
|
||||
it('should work', function(done) {
|
||||
var salt = util.random(keySize);
|
||||
|
||||
crypto.deriveKey(password, salt, keySize, function(err, key) {
|
||||
expect(err).to.not.exist;
|
||||
crypto.deriveKey(password, salt, keySize).then(function(key) {
|
||||
expect(util.base642Str(key).length * 8).to.equal(keySize);
|
||||
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
Loading…
Reference in New Issue
Block a user