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": {
"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
*/
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;
};
};

View File

@ -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);
});
});
};

View File

@ -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();
});

View File

@ -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();
});
});