updated devicestorage

This commit is contained in:
Tankred Hase 2013-05-17 17:44:34 +02:00
parent 4a080eed26
commit 2c1458b663
5 changed files with 39 additions and 46 deletions

View File

@ -9,6 +9,6 @@
}, },
"dependencies": { "dependencies": {
"express": "latest", "express": "latest",
"crypto-lib": "git://github.com/whiteout-io/crypto-lib.git#master" "crypto-lib": "https://github.com/whiteout-io/crypto-lib/tarball/master"
} }
} }

View File

@ -9,19 +9,13 @@ var CryptoBatch = function(aes, rsa, util) {
* @list list [Array] The list of items to encrypt * @list list [Array] The list of items to encrypt
*/ */
this.encryptList = function(list) { this.encryptList = function(list) {
var outList = [];
list.forEach(function(i) { list.forEach(function(i) {
// stringify to JSON before encryption // stringify to JSON before encryption
outList.push({ i.ciphertext = aes.encrypt(JSON.stringify(i.plaintext), i.key, i.iv);
id: i.id, delete i.plaintext;
ciphertext: aes.encrypt(JSON.stringify(i.plaintext), i.key, i.iv),
key: i.key,
iv: i.iv
});
}); });
return outList; return list;
}; };
/** /**
@ -29,19 +23,13 @@ var CryptoBatch = function(aes, rsa, util) {
* @list list [Array] The list of items to decrypt * @list list [Array] The list of items to decrypt
*/ */
this.decryptList = function(list) { this.decryptList = function(list) {
var outList = [];
list.forEach(function(i) { list.forEach(function(i) {
// decrypt JSON and parse to object literal // decrypt JSON and parse to object literal
outList.push({ i.plaintext = JSON.parse(aes.decrypt(i.ciphertext, i.key, i.iv));
id: i.id, delete i.ciphertext;
plaintext: JSON.parse(aes.decrypt(i.ciphertext, i.key, i.iv)),
key: i.key,
iv: i.iv
});
}); });
return outList; return list;
}; };
/** /**
@ -69,8 +57,7 @@ var CryptoBatch = function(aes, rsa, util) {
* @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) {
var list = [], var j, self = this;
self = this;
// decrypt keys for user // decrypt keys for user
encryptedList.forEach(function(i) { encryptedList.forEach(function(i) {
@ -89,11 +76,11 @@ var CryptoBatch = function(aes, rsa, util) {
var decryptedList = this.decryptList(encryptedList); var decryptedList = this.decryptList(encryptedList);
// add plaintext to list // add plaintext to list
decryptedList.forEach(function(i) { for (j = 0; j < decryptedList.length; j++) {
list.push(i.plaintext); decryptedList[j] = decryptedList[j].plaintext;
}); }
return list; return decryptedList;
}; };
}; };

View File

@ -49,8 +49,8 @@ app.dao.DeviceStorage = function(util, crypto, jsonDao, sqlcipherDao) {
jsonDao.list(crypto.emailAddress + '_' + type, offset, num, function(encryptedList) { jsonDao.list(crypto.emailAddress + '_' + type, offset, num, function(encryptedList) {
// decrypt list // decrypt list
crypto.aesDecryptListForUser(encryptedList, function(decryptedList) { crypto.decryptListForUser(encryptedList, null, function(err, decryptedList) {
callback(decryptedList); callback(err, decryptedList);
}); });
}); });
}; };

View File

@ -17,8 +17,8 @@ asyncTest("Init", 2, function() {
emailAddress: crypto_test.user, emailAddress: crypto_test.user,
password: crypto_test.password, password: crypto_test.password,
keySize: crypto_test.keySize keySize: crypto_test.keySize
}, function() { }, function(err) {
ok(true, 'Init crypto'); ok(!err, 'Init crypto');
start(); start();
}); });

View File

@ -14,31 +14,36 @@ asyncTest("Init", 3, function() {
devicestorage_test.crypto = new app.crypto.Crypto(window, devicestorage_test.util); 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); devicestorage_test.storage = new app.dao.DeviceStorage(devicestorage_test.util, devicestorage_test.crypto, devicestorage_test.jsonDao, null);
ok(devicestorage_test.storage, 'DeviceStorageDAO'); ok(devicestorage_test.storage, 'DeviceStorageDAO');
// generate test data // generate test data
devicestorage_test.list = new TestData().getEmailCollection(100).toJSON(); devicestorage_test.list = new TestData().getEmailCollection(100).toJSON();
// init crypto // init crypto
devicestorage_test.crypto.init(devicestorage_test.user, devicestorage_test.password, devicestorage_test.keySize, devicestorage_test.ivSize, function() { devicestorage_test.crypto.init({
ok(true, 'Crypto initialized'); emailAddress: devicestorage_test.user,
password: devicestorage_test.password,
keySize: devicestorage_test.keySize
}, function(err) {
ok(!err, 'Init crypto');
// clear db before tests // clear db before tests
devicestorage_test.jsonDao.clear(function(err) { devicestorage_test.jsonDao.clear(function(err) {
ok(!err, 'DB cleared. Error status: ' + err); ok(!err, 'DB cleared. Error status: ' + err);
start(); start();
}); });
}); });
}); });
asyncTest("Encrypt list for user", 1, function() { asyncTest("Encrypt list for user", 2, function() {
devicestorage_test.crypto.aesEncryptListForUser(devicestorage_test.list, function(encryptedList) { devicestorage_test.crypto.encryptListForUser(devicestorage_test.list, null, function(err, encryptedList) {
ok(!err);
equal(encryptedList.length, devicestorage_test.list.length, 'Encrypt list'); equal(encryptedList.length, devicestorage_test.list.length, 'Encrypt list');
devicestorage_test.encryptedList = encryptedList; devicestorage_test.encryptedList = encryptedList;
start(); start();
}); });
}); });
asyncTest("Store encrypted list", 1, function() { 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, var offset = 2,
num = 6; num = 6;
// list items from storage (decrypted) // 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)'); equal(decryptedList.length, num, 'Found ' + decryptedList.length + ' items in store (and decrypted)');
var decrypted, orig = devicestorage_test.list[54]; 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) { if (decryptedList[i].id === orig.id && decryptedList[i].from === orig.from) {
deepEqual(decryptedList[i], orig, 'Messages decrypted correctly'); deepEqual(decryptedList[i], orig, 'Messages decrypted correctly');
break; break;
} }
} }
start(); start();
}); });
}); });