From 2c1458b663714e7a4b2d33cc7dc238c4537779be Mon Sep 17 00:00:00 2001 From: Tankred Hase Date: Fri, 17 May 2013 17:44:34 +0200 Subject: [PATCH] updated devicestorage --- package.json | 2 +- src/js/crypto/crypto-batch.js | 35 +++++++++-------------------- src/js/dao/devicestorage.js | 4 ++-- test/unit/crypto-test.js | 4 ++-- test/unit/devicestorage-test.js | 40 +++++++++++++++++++-------------- 5 files changed, 39 insertions(+), 46 deletions(-) diff --git a/package.json b/package.json index e283414..129eece 100644 --- a/package.json +++ b/package.json @@ -9,6 +9,6 @@ }, "dependencies": { "express": "latest", - "crypto-lib": "git://github.com/whiteout-io/crypto-lib.git#master" + "crypto-lib": "https://github.com/whiteout-io/crypto-lib/tarball/master" } } \ No newline at end of file diff --git a/src/js/crypto/crypto-batch.js b/src/js/crypto/crypto-batch.js index c5db6db..49a1411 100644 --- a/src/js/crypto/crypto-batch.js +++ b/src/js/crypto/crypto-batch.js @@ -9,19 +9,13 @@ var CryptoBatch = function(aes, rsa, util) { * @list list [Array] The list of items to encrypt */ this.encryptList = function(list) { - var outList = []; - list.forEach(function(i) { // stringify to JSON before encryption - outList.push({ - id: i.id, - ciphertext: aes.encrypt(JSON.stringify(i.plaintext), i.key, i.iv), - key: i.key, - iv: i.iv - }); + i.ciphertext = aes.encrypt(JSON.stringify(i.plaintext), i.key, i.iv); + delete i.plaintext; }); - return outList; + return list; }; /** @@ -29,19 +23,13 @@ var CryptoBatch = function(aes, rsa, util) { * @list list [Array] The list of items to decrypt */ this.decryptList = function(list) { - var outList = []; - list.forEach(function(i) { // decrypt JSON and parse to object literal - outList.push({ - id: i.id, - plaintext: JSON.parse(aes.decrypt(i.ciphertext, i.key, i.iv)), - key: i.key, - iv: i.iv - }); + i.plaintext = JSON.parse(aes.decrypt(i.ciphertext, i.key, i.iv)); + delete i.ciphertext; }); - return outList; + return list; }; /** @@ -69,8 +57,7 @@ var CryptoBatch = function(aes, rsa, util) { * @list list [Array] The list of items to decrypt */ this.decryptListForUser = function(encryptedList) { - var list = [], - self = this; + var j, self = this; // decrypt keys for user encryptedList.forEach(function(i) { @@ -89,11 +76,11 @@ var CryptoBatch = function(aes, rsa, util) { var decryptedList = this.decryptList(encryptedList); // add plaintext to list - decryptedList.forEach(function(i) { - list.push(i.plaintext); - }); + for (j = 0; j < decryptedList.length; j++) { + decryptedList[j] = decryptedList[j].plaintext; + } - return list; + return decryptedList; }; }; diff --git a/src/js/dao/devicestorage.js b/src/js/dao/devicestorage.js index 4be1ffa..3eab838 100644 --- a/src/js/dao/devicestorage.js +++ b/src/js/dao/devicestorage.js @@ -49,8 +49,8 @@ app.dao.DeviceStorage = function(util, crypto, jsonDao, sqlcipherDao) { jsonDao.list(crypto.emailAddress + '_' + type, offset, num, function(encryptedList) { // decrypt list - crypto.aesDecryptListForUser(encryptedList, function(decryptedList) { - callback(decryptedList); + crypto.decryptListForUser(encryptedList, null, function(err, decryptedList) { + callback(err, decryptedList); }); }); }; diff --git a/test/unit/crypto-test.js b/test/unit/crypto-test.js index 7df9fe3..dab0dc2 100644 --- a/test/unit/crypto-test.js +++ b/test/unit/crypto-test.js @@ -17,8 +17,8 @@ asyncTest("Init", 2, function() { emailAddress: crypto_test.user, password: crypto_test.password, keySize: crypto_test.keySize - }, function() { - ok(true, 'Init crypto'); + }, function(err) { + ok(!err, 'Init crypto'); start(); }); diff --git a/test/unit/devicestorage-test.js b/test/unit/devicestorage-test.js index 0feb0d3..46dd392 100644 --- a/test/unit/devicestorage-test.js +++ b/test/unit/devicestorage-test.js @@ -14,31 +14,36 @@ asyncTest("Init", 3, function() { devicestorage_test.crypto = new app.crypto.Crypto(window, devicestorage_test.util); devicestorage_test.storage = new app.dao.DeviceStorage(devicestorage_test.util, devicestorage_test.crypto, devicestorage_test.jsonDao, null); ok(devicestorage_test.storage, 'DeviceStorageDAO'); - + // generate test data devicestorage_test.list = new TestData().getEmailCollection(100).toJSON(); - + // init crypto - devicestorage_test.crypto.init(devicestorage_test.user, devicestorage_test.password, devicestorage_test.keySize, devicestorage_test.ivSize, function() { - ok(true, 'Crypto initialized'); - + devicestorage_test.crypto.init({ + emailAddress: devicestorage_test.user, + password: devicestorage_test.password, + keySize: devicestorage_test.keySize + }, function(err) { + ok(!err, 'Init crypto'); + // clear db before tests devicestorage_test.jsonDao.clear(function(err) { ok(!err, 'DB cleared. Error status: ' + err); - + start(); }); - + }); }); -asyncTest("Encrypt list for user", 1, function() { - devicestorage_test.crypto.aesEncryptListForUser(devicestorage_test.list, function(encryptedList) { +asyncTest("Encrypt list for user", 2, function() { + devicestorage_test.crypto.encryptListForUser(devicestorage_test.list, null, function(err, encryptedList) { + ok(!err); equal(encryptedList.length, devicestorage_test.list.length, 'Encrypt list'); - + devicestorage_test.encryptedList = encryptedList; start(); - }); + }); }); asyncTest("Store encrypted list", 1, function() { @@ -49,13 +54,14 @@ asyncTest("Store encrypted list", 1, function() { }); }); -asyncTest("List items", 2, function() { - +asyncTest("List items", 3, function() { + var offset = 2, num = 6; - + // list items from storage (decrypted) - devicestorage_test.storage.listItems('email_inbox_5',offset ,num, function(decryptedList) { + devicestorage_test.storage.listItems('email_inbox_5', offset, num, function(err, decryptedList) { + ok(!err); equal(decryptedList.length, num, 'Found ' + decryptedList.length + ' items in store (and decrypted)'); var decrypted, orig = devicestorage_test.list[54]; @@ -65,9 +71,9 @@ asyncTest("List items", 2, function() { if (decryptedList[i].id === orig.id && decryptedList[i].from === orig.from) { deepEqual(decryptedList[i], orig, 'Messages decrypted correctly'); break; - } + } } - + start(); }); }); \ No newline at end of file