From 1b95066e124730a095011193b5e9f53382fcc62c Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Tue, 14 May 2013 19:11:16 +0200 Subject: [PATCH] crypto cleanup of for loops using forEach --- src/js/crypto/crypto.js | 47 +++++++++++++++++++++-------------------- src/js/crypto/util.js | 30 +++++++++++++------------- 2 files changed, 39 insertions(+), 38 deletions(-) diff --git a/src/js/crypto/crypto.js b/src/js/crypto/crypto.js index 1fd2111..0eb77f0 100644 --- a/src/js/crypto/crypto.js +++ b/src/js/crypto/crypto.js @@ -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); }); diff --git a/src/js/crypto/util.js b/src/js/crypto/util.js index 01be62f..6cffdc5 100644 --- a/src/js/crypto/util.js +++ b/src/js/crypto/util.js @@ -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; };