1
0
mirror of https://github.com/moparisthebest/mail synced 2024-12-22 15:28:49 -05:00

moved crypto batch code from util to own js file

This commit is contained in:
Tankred Hase 2013-05-15 13:36:59 +02:00
parent 98a4b8cfec
commit 6f2be99672
6 changed files with 135 additions and 103 deletions

View File

@ -4,8 +4,8 @@
// import web worker dependencies
importScripts('../../lib/forge/forge.rsa.bundle.js');
importScripts('../app-config.js');
importScripts('./crypto-batch.js');
importScripts('./aes-cbc.js');
importScripts('./util.js');
/**
* In the web worker thread context, 'this' and 'self' can be used as a global
@ -16,15 +16,15 @@
var i = e.data,
output = null,
aes = new app.crypto.AesCBC(forge),
util = new app.crypto.Util(null, null);
batch = new app.crypto.CryptoBatch(aes);
if (i.type === 'encrypt' && i.list) {
// start encryption
output = util.encryptList(aes, i.list);
output = batch.encryptList(i.list);
} else if (i.type === 'decrypt' && i.list) {
// start decryption
output = util.decryptList(aes, i.list);
output = batch.decryptList(i.list);
} else {
throw 'Not all arguments for web worker crypto are defined!';

View File

@ -4,9 +4,9 @@
// import web worker dependencies
importScripts('../../lib/forge/forge.rsa.bundle.js');
importScripts('../app-config.js');
importScripts('./crypto-batch.js');
importScripts('./aes-cbc.js');
importScripts('./rsa.js');
importScripts('./util.js');
/**
* In the web worker thread context, 'this' and 'self' can be used as a global
@ -16,19 +16,20 @@
var i = e.data,
output = null,
util = new app.crypto.Util(null, null),
aes = new app.crypto.AesCBC(forge),
rsa = new app.crypto.RSA(forge, util);
rsa = new app.crypto.RSA(forge),
batch = new app.crypto.CryptoBatch(aes, rsa);
// pass RSA keys to module
rsa.init(i.pubkeyPem, i.privkeyPem);
if (i.type === 'encrypt' && i.list) {
// start encryption
output = util.encryptListForUser(aes, rsa, i.list);
output = batch.encryptListForUser(i.list);
} else if (i.type === 'decrypt' && i.list) {
// start decryption
output = util.decryptListForUser(aes, rsa, i.list);
output = batch.decryptListForUser(i.list);
} else {
throw 'Not all arguments for web worker crypto are defined!';

View File

@ -0,0 +1,116 @@
/**
* Crypto batch library for processing large sets of data
*/
var CryptoBatch = function(aes, rsa) {
'use strict';
//
// Encryption
//
/**
* Encrypt a list of items using AES
* @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
});
});
return outList;
};
/**
* Encrypt a list of items using AES and RSA
* @list list [Array] The list of items to encrypt
*/
this.encryptListForUser = function(list) {
// encrypt list
var encryptedList = this.encryptList(list);
// encrypt keys for user
encryptedList.forEach(function(i) {
// process new values
i.itemIV = i.iv;
i.encryptedKey = rsa.encrypt(i.key);
i.keyIV = rsa.sign([i.itemIV, i.encryptedKey, i.ciphertext]);
// delete old ones
delete i.iv;
delete i.key;
});
return encryptedList;
};
//
// Decryption
//
/**
* Decrypt a list of items using AES
* @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
});
});
return outList;
};
/**
* Decrypt a list of items using AES and RSA
* @list list [Array] The list of items to decrypt
*/
this.decryptListForUser = function(encryptedList) {
var list = [],
self = this;
// decrypt keys for user
encryptedList.forEach(function(i) {
// verify signature
if (!rsa.verify([i.itemIV, i.encryptedKey, i.ciphertext], i.keyIV)) {
throw new Error('Verifying RSA signature failed!');
}
// precoess new values
i.iv = i.itemIV;
i.key = rsa.decrypt(i.encryptedKey);
// delete old values
delete i.keyIV;
delete i.itemIV;
delete i.encryptedKey;
});
// decrypt list
var decryptedList = this.decryptList(encryptedList);
// add plaintext to list
decryptedList.forEach(function(i) {
list.push(i.plaintext);
});
return list;
};
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = CryptoBatch;
} else {
app.crypto.CryptoBatch = CryptoBatch;
}

View File

@ -182,7 +182,8 @@ app.crypto.Crypto = function(window, util) {
});
} else {
var encryptedList = util.encryptList(aes, list);
var batch = new app.crypto.CryptoBatch(aes);
var encryptedList = batch.encryptList(list);
callback(encryptedList);
}
};
@ -200,7 +201,8 @@ app.crypto.Crypto = function(window, util) {
});
} else {
var decryptedList = util.decryptList(aes, list);
var batch = new app.crypto.CryptoBatch(aes);
var decryptedList = batch.decryptList(list);
callback(decryptedList);
}
};
@ -240,7 +242,8 @@ app.crypto.Crypto = function(window, util) {
});
} else {
var encryptedList = util.encryptListForUser(aes, rsa, envelopes);
var batch = new app.crypto.CryptoBatch(aes, rsa);
var encryptedList = batch.encryptListForUser(envelopes);
callback(null, encryptedList);
}
};
@ -262,7 +265,8 @@ app.crypto.Crypto = function(window, util) {
});
} else {
var decryptedList = util.decryptListForUser(aes, rsa, list);
var batch = new app.crypto.CryptoBatch(aes, rsa);
var decryptedList = batch.decryptListForUser(list);
callback(null, decryptedList);
}
};

View File

@ -38,96 +38,6 @@ var Util = function(window, uuid, crypt) {
return keyBase64;
};
/**
* Encrypt a list of items
* @param aes [Object] The object implementing the aes mode
* @list list [Array] The list of items to encrypt
*/
this.encryptList = function(aes, 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
});
});
return outList;
};
this.encryptListForUser = function(aes, rsa, list) {
// encrypt list
var encryptedList = this.encryptList(aes, list);
// encrypt keys for user
encryptedList.forEach(function(i) {
// process new values
i.itemIV = i.iv;
i.encryptedKey = rsa.encrypt(i.key);
i.keyIV = rsa.sign([i.itemIV, i.encryptedKey, i.ciphertext]);
// delete old ones
delete i.iv;
delete i.key;
});
return encryptedList;
};
/**
* Decrypt a list of items
* @param aes [Object] The object implementing the aes mode
* @list list [Array] The list of items to decrypt
*/
this.decryptList = function(aes, 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
});
});
return outList;
};
this.decryptListForUser = function(aes, rsa, encryptedList) {
var list = [],
self = this;
// decrypt keys for user
encryptedList.forEach(function(i) {
// verify signature
if (!rsa.verify([i.itemIV, i.encryptedKey, i.ciphertext], i.keyIV)) {
throw new Error('Verifying RSA signature failed!');
}
// precoess new values
i.iv = i.itemIV;
i.key = rsa.decrypt(i.encryptedKey);
// delete old values
delete i.keyIV;
delete i.itemIV;
delete i.encryptedKey;
});
// decrypt list
var decryptedList = this.decryptList(aes, encryptedList);
// add plaintext to list
decryptedList.forEach(function(i) {
list.push(i.plaintext);
});
return list;
};
/**
* Parse a date string with the following format "1900-01-31 18:17:53"
*/

View File

@ -44,6 +44,7 @@
<script src="../js/crypto/aes-cbc.js"></script>
<script src="../js/crypto/rsa.js"></script>
<script src="../js/crypto/nacl-crypto.js"></script>
<script src="../js/crypto/crypto-batch.js"></script>
<script src="../js/crypto/crypto.js"></script>
<script src="../js/dao/localstorage-dao.js"></script>