1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-22 08:52:15 -05:00

Refactor crypto module

This commit is contained in:
Tankred Hase 2014-12-11 14:16:07 +01:00
parent f4fe1a36a6
commit 88a48ec540
2 changed files with 50 additions and 78 deletions

View File

@ -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,43 +66,33 @@ Crypto.prototype.deriveKey = function(password, salt, keySize, callback) {
// helper functions
//
function startWorker(options) {
// 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);
};
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
});
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) {
// return result from the worker
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
reject(e);
};
// send data to the worker
worker.postMessage(options.args);
return;
};
// send data to the worker
worker.postMessage(options.args);
return;
}
}
// no WebWorker support... do synchronous call
var result;
try {
result = options.noWorker();
} catch (e) {
// return error
options.callback({
errMsg: (e.message) ? e.message : e
});
return;
}
options.callback(null, result);
}
// no WebWorker support... do synchronous call
var result = options.noWorker();
resolve(result);
});
};

View File

@ -13,7 +13,7 @@ describe('Crypto unit tests', function() {
ivSize = config.symIvSize;
beforeEach(function() {
crypto = new Crypto();
crypto = new Crypto(qMock);
});
afterEach(function() {});
@ -24,16 +24,14 @@ 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;
expect(decrypted).to.equal(plaintext);
return crypto.decrypt(ciphertext, key, iv);
}).then(function(decrypted) {
expect(decrypted).to.equal(plaintext);
done();
});
done();
});
});
});
@ -42,14 +40,11 @@ describe('Crypto unit tests', 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();
});
});
});
});