crypto cleanup of for loops using forEach

This commit is contained in:
Tankred Hase 2013-05-14 19:11:16 +02:00
parent b25b5256d6
commit 1b95066e12
2 changed files with 39 additions and 38 deletions

View File

@ -210,58 +210,59 @@ app.crypto.Crypto = function(window, util) {
};
this.aesEncryptListForUser = function(list, callback) {
var i, envelope, envelopes = [],
var envelope, envelopes = [],
self = this;
// package objects into batchable envelope format
for (i = 0; i < list.length; i++) {
list.forEach(function(i) {
envelope = {
id: list[i].id,
plaintext: list[i],
id: i.id,
plaintext: i,
key: util.random(self.keySize),
iv: util.random(self.ivSize)
};
envelopes.push(envelope);
}
});
// encrypt list
this.aesEncryptList(envelopes, function(encryptedList) {
// encrypt keys for user
for (i = 0; i < encryptedList.length; i++) {
encryptedList.forEach(function(i) {
// process new values
encryptedList[i].itemIV = encryptedList[i].iv;
encryptedList[i].keyIV = util.random(self.ivSize);
encryptedList[i].encryptedKey = self.aesEncryptForUserSync(encryptedList[i].key, encryptedList[i].keyIV);
i.itemIV = i.iv;
i.keyIV = util.random(self.ivSize);
i.encryptedKey = self.aesEncryptForUserSync(i.key, i.keyIV);
// delete old ones
delete encryptedList[i].iv;
delete encryptedList[i].key;
}
delete i.iv;
delete i.key;
});
callback(encryptedList);
});
};
this.aesDecryptListForUser = function(encryptedList, callback) {
var i, list = [];
var list = [],
self = this;
// decrypt keys for user
for (i = 0; i < encryptedList.length; i++) {
encryptedList.forEach(function(i) {
// decrypt item key
encryptedList[i].key = this.aesDecryptForUserSync(encryptedList[i].encryptedKey, encryptedList[i].keyIV);
encryptedList[i].iv = encryptedList[i].itemIV;
i.key = self.aesDecryptForUserSync(i.encryptedKey, i.keyIV);
i.iv = i.itemIV;
// delete old values
delete encryptedList[i].keyIV;
delete encryptedList[i].itemIV;
delete encryptedList[i].encryptedKey;
}
delete i.keyIV;
delete i.itemIV;
delete i.encryptedKey;
});
// decrypt list
this.aesDecryptList(encryptedList, function(decryptedList) {
// add plaintext to list
for (i = 0; i < decryptedList.length; i++) {
list.push(decryptedList[i].plaintext);
}
decryptedList.forEach(function(i) {
list.push(i.plaintext);
});
callback(list);
});

View File

@ -44,19 +44,19 @@ var Util = function(window, uuid, crypt) {
* @list list [Array] The list of items to encrypt
*/
this.encryptList = function(aes, list) {
var i, json, ct, outList = [];
var json, ct, outList = [];
for (i = 0; i < list.length; i++) {
list.forEach(function(i) {
// stringify to JSON before encryption
json = JSON.stringify(list[i].plaintext);
ct = aes.encrypt(json, list[i].key, list[i].iv);
json = JSON.stringify(i.plaintext);
ct = aes.encrypt(json, i.key, i.iv);
outList.push({
id: list[i].id,
id: i.id,
ciphertext: ct,
key: list[i].key,
iv: list[i].iv
key: i.key,
iv: i.iv
});
}
});
return outList;
};
@ -67,19 +67,19 @@ var Util = function(window, uuid, crypt) {
* @list list [Array] The list of items to decrypt
*/
this.decryptList = function(aes, list) {
var i, json, pt, outList = [];
var json, pt, outList = [];
for (i = 0; i < list.length; i++) {
list.forEach(function(i) {
// decrypt JSON and parse to object literal
json = aes.decrypt(list[i].ciphertext, list[i].key, list[i].iv);
json = aes.decrypt(i.ciphertext, i.key, i.iv);
pt = JSON.parse(json);
outList.push({
id: list[i].id,
id: i.id,
plaintext: pt,
key: list[i].key,
iv: list[i].iv
key: i.key,
iv: i.iv
});
}
});
return outList;
};