mirror of
https://github.com/moparisthebest/mail
synced 2024-11-29 04:12:18 -05:00
Use native promises in crypto services
This commit is contained in:
parent
e32456c3db
commit
bc41486693
@ -13,9 +13,7 @@ var aes = require('crypto-lib').aes,
|
|||||||
* High level crypto api that invokes native crypto (if available) and
|
* High level crypto api that invokes native crypto (if available) and
|
||||||
* gracefully degrades to JS crypto (if unavailable)
|
* gracefully degrades to JS crypto (if unavailable)
|
||||||
*/
|
*/
|
||||||
function Crypto($q) {
|
function Crypto() {}
|
||||||
this._q = $q;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Encrypt plaintext using AES-GCM.
|
* Encrypt plaintext using AES-GCM.
|
||||||
@ -25,7 +23,7 @@ function Crypto($q) {
|
|||||||
* @return {String} The base64 encoded ciphertext
|
* @return {String} The base64 encoded ciphertext
|
||||||
*/
|
*/
|
||||||
Crypto.prototype.encrypt = function(plaintext, key, iv) {
|
Crypto.prototype.encrypt = function(plaintext, key, iv) {
|
||||||
return this._q(function(resolve) {
|
return new Promise(function(resolve) {
|
||||||
var ct = aes.encrypt(plaintext, key, iv);
|
var ct = aes.encrypt(plaintext, key, iv);
|
||||||
resolve(ct);
|
resolve(ct);
|
||||||
});
|
});
|
||||||
@ -39,7 +37,7 @@ Crypto.prototype.encrypt = function(plaintext, key, iv) {
|
|||||||
* @return {String} The decrypted plaintext in UTF-16
|
* @return {String} The decrypted plaintext in UTF-16
|
||||||
*/
|
*/
|
||||||
Crypto.prototype.decrypt = function(ciphertext, key, iv) {
|
Crypto.prototype.decrypt = function(ciphertext, key, iv) {
|
||||||
return this._q(function(resolve) {
|
return new Promise(function(resolve) {
|
||||||
var pt = aes.decrypt(ciphertext, key, iv);
|
var pt = aes.decrypt(ciphertext, key, iv);
|
||||||
resolve(pt);
|
resolve(pt);
|
||||||
});
|
});
|
||||||
@ -67,7 +65,7 @@ Crypto.prototype.deriveKey = function(password, salt, keySize) {
|
|||||||
//
|
//
|
||||||
|
|
||||||
Crypto.prototype.startWorker = function(options) {
|
Crypto.prototype.startWorker = function(options) {
|
||||||
return this._q(function(resolve, reject) {
|
return new Promise(function(resolve, reject) {
|
||||||
// check for WebWorker support
|
// check for WebWorker support
|
||||||
if (window.Worker) {
|
if (window.Worker) {
|
||||||
// init webworker thread
|
// init webworker thread
|
||||||
|
@ -10,9 +10,7 @@ var util = openpgp.util,
|
|||||||
/**
|
/**
|
||||||
* High level crypto api that handles all calls to OpenPGP.js
|
* High level crypto api that handles all calls to OpenPGP.js
|
||||||
*/
|
*/
|
||||||
function PGP($q) {
|
function PGP() {
|
||||||
this._q = $q;
|
|
||||||
|
|
||||||
openpgp.config.prefer_hash_algorithm = openpgp.enums.hash.sha256;
|
openpgp.config.prefer_hash_algorithm = openpgp.enums.hash.sha256;
|
||||||
openpgp.initWorker(config.workerPath + '/openpgp.worker.min.js');
|
openpgp.initWorker(config.workerPath + '/openpgp.worker.min.js');
|
||||||
}
|
}
|
||||||
@ -22,7 +20,7 @@ function PGP($q) {
|
|||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
*/
|
*/
|
||||||
PGP.prototype.generateKeys = function(options) {
|
PGP.prototype.generateKeys = function(options) {
|
||||||
return this._q(function(resolve) {
|
return new Promise(function(resolve) {
|
||||||
var userId, passphrase;
|
var userId, passphrase;
|
||||||
|
|
||||||
if (!util.emailRegEx.test(options.emailAddress) || !options.keySize) {
|
if (!util.emailRegEx.test(options.emailAddress) || !options.keySize) {
|
||||||
@ -156,7 +154,7 @@ PGP.prototype.extractPublicKey = function(privateKeyArmored) {
|
|||||||
*/
|
*/
|
||||||
PGP.prototype.importKeys = function(options) {
|
PGP.prototype.importKeys = function(options) {
|
||||||
var self = this;
|
var self = this;
|
||||||
return self._q(function(resolve) {
|
return new Promise(function(resolve) {
|
||||||
var pubKeyId, privKeyId;
|
var pubKeyId, privKeyId;
|
||||||
|
|
||||||
// check options
|
// check options
|
||||||
@ -202,7 +200,7 @@ PGP.prototype.importKeys = function(options) {
|
|||||||
*/
|
*/
|
||||||
PGP.prototype.exportKeys = function() {
|
PGP.prototype.exportKeys = function() {
|
||||||
var self = this;
|
var self = this;
|
||||||
return self._q(function(resolve) {
|
return new Promise(function(resolve) {
|
||||||
if (!self._publicKey || !self._privateKey) {
|
if (!self._publicKey || !self._privateKey) {
|
||||||
throw new Error('Could not export keys!');
|
throw new Error('Could not export keys!');
|
||||||
}
|
}
|
||||||
@ -220,7 +218,7 @@ PGP.prototype.exportKeys = function() {
|
|||||||
* @return {Promise}
|
* @return {Promise}
|
||||||
*/
|
*/
|
||||||
PGP.prototype.changePassphrase = function(options) {
|
PGP.prototype.changePassphrase = function(options) {
|
||||||
return this._q(function(resolve) {
|
return new Promise(function(resolve) {
|
||||||
var privKey, packets, newPassphrase, newKeyArmored;
|
var privKey, packets, newPassphrase, newKeyArmored;
|
||||||
|
|
||||||
// set undefined instead of empty string as passphrase
|
// set undefined instead of empty string as passphrase
|
||||||
@ -273,7 +271,7 @@ PGP.prototype.changePassphrase = function(options) {
|
|||||||
*/
|
*/
|
||||||
PGP.prototype.encrypt = function(plaintext, publicKeysArmored) {
|
PGP.prototype.encrypt = function(plaintext, publicKeysArmored) {
|
||||||
var self = this;
|
var self = this;
|
||||||
return self._q(function(resolve) {
|
return new Promise(function(resolve) {
|
||||||
var publicKeys;
|
var publicKeys;
|
||||||
|
|
||||||
// check keys
|
// check keys
|
||||||
@ -313,7 +311,7 @@ PGP.prototype.encrypt = function(plaintext, publicKeysArmored) {
|
|||||||
*/
|
*/
|
||||||
PGP.prototype.decrypt = function(ciphertext, publicKeyArmored) {
|
PGP.prototype.decrypt = function(ciphertext, publicKeyArmored) {
|
||||||
var self = this;
|
var self = this;
|
||||||
return self._q(function(resolve) {
|
return new Promise(function(resolve) {
|
||||||
var publicKeys, message;
|
var publicKeys, message;
|
||||||
|
|
||||||
// check keys
|
// check keys
|
||||||
@ -359,7 +357,7 @@ PGP.prototype.decrypt = function(ciphertext, publicKeyArmored) {
|
|||||||
*/
|
*/
|
||||||
PGP.prototype.verifyClearSignedMessage = function(clearSignedText, publicKeyArmored) {
|
PGP.prototype.verifyClearSignedMessage = function(clearSignedText, publicKeyArmored) {
|
||||||
var self = this;
|
var self = this;
|
||||||
return self._q(function(resolve) {
|
return new Promise(function(resolve) {
|
||||||
var publicKeys, message;
|
var publicKeys, message;
|
||||||
|
|
||||||
// check keys
|
// check keys
|
||||||
@ -401,7 +399,7 @@ PGP.prototype.verifyClearSignedMessage = function(clearSignedText, publicKeyArmo
|
|||||||
*/
|
*/
|
||||||
PGP.prototype.verifySignedMessage = function(message, pgpSignature, publicKeyArmored) {
|
PGP.prototype.verifySignedMessage = function(message, pgpSignature, publicKeyArmored) {
|
||||||
var self = this;
|
var self = this;
|
||||||
return self._q(function(resolve) {
|
return new Promise(function(resolve) {
|
||||||
var publicKeys, signatures;
|
var publicKeys, signatures;
|
||||||
|
|
||||||
// check keys
|
// check keys
|
||||||
|
@ -13,7 +13,7 @@ describe('Crypto unit tests', function() {
|
|||||||
ivSize = config.symIvSize;
|
ivSize = config.symIvSize;
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
crypto = new Crypto(qMock);
|
crypto = new Crypto();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {});
|
afterEach(function() {});
|
||||||
|
@ -39,7 +39,7 @@ describe('PGP Crypto Api unit tests', function() {
|
|||||||
'-----END PGP PRIVATE KEY BLOCK-----\r\n';
|
'-----END PGP PRIVATE KEY BLOCK-----\r\n';
|
||||||
|
|
||||||
beforeEach(function() {
|
beforeEach(function() {
|
||||||
pgp = new PGP(qMock);
|
pgp = new PGP();
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(function() {});
|
afterEach(function() {});
|
||||||
|
Loading…
Reference in New Issue
Block a user