1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-29 12:22:22 -05:00

refactoring and cleanup of crypto batch lib

This commit is contained in:
Tankred Hase 2013-05-15 13:47:49 +02:00
parent 6f2be99672
commit 838fc0396f

View File

@ -4,10 +4,6 @@
var CryptoBatch = function(aes, rsa) { var CryptoBatch = function(aes, rsa) {
'use strict'; 'use strict';
//
// Encryption
//
/** /**
* Encrypt a list of items using AES * Encrypt a list of items using AES
* @list list [Array] The list of items to encrypt * @list list [Array] The list of items to encrypt
@ -28,32 +24,6 @@ var CryptoBatch = function(aes, rsa) {
return outList; return outList;
}; };
/**
* Encrypt a list of items using AES and RSA
* @list list [Array] The list of items to encrypt
*/
this.encryptListForUser = function(list) {
// encrypt list
var encryptedList = this.encryptList(list);
// encrypt keys for user
encryptedList.forEach(function(i) {
// process new values
i.itemIV = i.iv;
i.encryptedKey = rsa.encrypt(i.key);
i.keyIV = rsa.sign([i.itemIV, i.encryptedKey, i.ciphertext]);
// delete old ones
delete i.iv;
delete i.key;
});
return encryptedList;
};
//
// Decryption
//
/** /**
* Decrypt a list of items using AES * Decrypt a list of items using AES
* @list list [Array] The list of items to decrypt * @list list [Array] The list of items to decrypt
@ -75,7 +45,27 @@ var CryptoBatch = function(aes, rsa) {
}; };
/** /**
* Decrypt a list of items using AES and RSA * Encrypt and sign a list of items using AES and RSA
* @list list [Array] The list of items to encrypt
*/
this.encryptListForUser = function(list) {
// encrypt list
var encryptedList = this.encryptList(list);
// encrypt keys for user
encryptedList.forEach(function(i) {
// process new values
i.encryptedKey = rsa.encrypt(i.key);
i.signature = rsa.sign([i.iv, i.encryptedKey, i.ciphertext]);
// delete old ones
delete i.key;
});
return encryptedList;
};
/**
* Decrypt and verify a list of items using AES and RSA
* @list list [Array] The list of items to decrypt * @list list [Array] The list of items to decrypt
*/ */
this.decryptListForUser = function(encryptedList) { this.decryptListForUser = function(encryptedList) {
@ -85,15 +75,13 @@ var CryptoBatch = function(aes, rsa) {
// decrypt keys for user // decrypt keys for user
encryptedList.forEach(function(i) { encryptedList.forEach(function(i) {
// verify signature // verify signature
if (!rsa.verify([i.itemIV, i.encryptedKey, i.ciphertext], i.keyIV)) { if (!rsa.verify([i.iv, i.encryptedKey, i.ciphertext], i.signature)) {
throw new Error('Verifying RSA signature failed!'); throw new Error('Verifying RSA signature failed!');
} }
// precoess new values // precoess new values
i.iv = i.itemIV;
i.key = rsa.decrypt(i.encryptedKey); i.key = rsa.decrypt(i.encryptedKey);
// delete old values // delete old values
delete i.keyIV; delete i.signature;
delete i.itemIV;
delete i.encryptedKey; delete i.encryptedKey;
}); });