1
0
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:
Tankred Hase 2014-12-12 18:07:30 +01:00
parent e32456c3db
commit bc41486693
4 changed files with 15 additions and 19 deletions

View File

@ -13,9 +13,7 @@ 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($q) {
this._q = $q;
}
function Crypto() {}
/**
* Encrypt plaintext using AES-GCM.
@ -25,7 +23,7 @@ function Crypto($q) {
* @return {String} The base64 encoded ciphertext
*/
Crypto.prototype.encrypt = function(plaintext, key, iv) {
return this._q(function(resolve) {
return new Promise(function(resolve) {
var ct = aes.encrypt(plaintext, key, iv);
resolve(ct);
});
@ -39,7 +37,7 @@ Crypto.prototype.encrypt = function(plaintext, key, iv) {
* @return {String} The decrypted plaintext in UTF-16
*/
Crypto.prototype.decrypt = function(ciphertext, key, iv) {
return this._q(function(resolve) {
return new Promise(function(resolve) {
var pt = aes.decrypt(ciphertext, key, iv);
resolve(pt);
});
@ -67,7 +65,7 @@ Crypto.prototype.deriveKey = function(password, salt, keySize) {
//
Crypto.prototype.startWorker = function(options) {
return this._q(function(resolve, reject) {
return new Promise(function(resolve, reject) {
// check for WebWorker support
if (window.Worker) {
// init webworker thread

View File

@ -10,9 +10,7 @@ var util = openpgp.util,
/**
* High level crypto api that handles all calls to OpenPGP.js
*/
function PGP($q) {
this._q = $q;
function PGP() {
openpgp.config.prefer_hash_algorithm = openpgp.enums.hash.sha256;
openpgp.initWorker(config.workerPath + '/openpgp.worker.min.js');
}
@ -22,7 +20,7 @@ function PGP($q) {
* @return {Promise}
*/
PGP.prototype.generateKeys = function(options) {
return this._q(function(resolve) {
return new Promise(function(resolve) {
var userId, passphrase;
if (!util.emailRegEx.test(options.emailAddress) || !options.keySize) {
@ -156,7 +154,7 @@ PGP.prototype.extractPublicKey = function(privateKeyArmored) {
*/
PGP.prototype.importKeys = function(options) {
var self = this;
return self._q(function(resolve) {
return new Promise(function(resolve) {
var pubKeyId, privKeyId;
// check options
@ -202,7 +200,7 @@ PGP.prototype.importKeys = function(options) {
*/
PGP.prototype.exportKeys = function() {
var self = this;
return self._q(function(resolve) {
return new Promise(function(resolve) {
if (!self._publicKey || !self._privateKey) {
throw new Error('Could not export keys!');
}
@ -220,7 +218,7 @@ PGP.prototype.exportKeys = function() {
* @return {Promise}
*/
PGP.prototype.changePassphrase = function(options) {
return this._q(function(resolve) {
return new Promise(function(resolve) {
var privKey, packets, newPassphrase, newKeyArmored;
// set undefined instead of empty string as passphrase
@ -273,7 +271,7 @@ PGP.prototype.changePassphrase = function(options) {
*/
PGP.prototype.encrypt = function(plaintext, publicKeysArmored) {
var self = this;
return self._q(function(resolve) {
return new Promise(function(resolve) {
var publicKeys;
// check keys
@ -313,7 +311,7 @@ PGP.prototype.encrypt = function(plaintext, publicKeysArmored) {
*/
PGP.prototype.decrypt = function(ciphertext, publicKeyArmored) {
var self = this;
return self._q(function(resolve) {
return new Promise(function(resolve) {
var publicKeys, message;
// check keys
@ -359,7 +357,7 @@ PGP.prototype.decrypt = function(ciphertext, publicKeyArmored) {
*/
PGP.prototype.verifyClearSignedMessage = function(clearSignedText, publicKeyArmored) {
var self = this;
return self._q(function(resolve) {
return new Promise(function(resolve) {
var publicKeys, message;
// check keys
@ -401,7 +399,7 @@ PGP.prototype.verifyClearSignedMessage = function(clearSignedText, publicKeyArmo
*/
PGP.prototype.verifySignedMessage = function(message, pgpSignature, publicKeyArmored) {
var self = this;
return self._q(function(resolve) {
return new Promise(function(resolve) {
var publicKeys, signatures;
// check keys

View File

@ -13,7 +13,7 @@ describe('Crypto unit tests', function() {
ivSize = config.symIvSize;
beforeEach(function() {
crypto = new Crypto(qMock);
crypto = new Crypto();
});
afterEach(function() {});

View File

@ -39,7 +39,7 @@ describe('PGP Crypto Api unit tests', function() {
'-----END PGP PRIVATE KEY BLOCK-----\r\n';
beforeEach(function() {
pgp = new PGP(qMock);
pgp = new PGP();
});
afterEach(function() {});