1
0
mirror of https://github.com/moparisthebest/mail synced 2024-12-02 05:42:20 -05:00
mail/test/unit/nacl-crypto-test.js

65 lines
2.2 KiB
JavaScript
Raw Normal View History

2013-04-04 08:28:07 -04:00
module("NaCl Crypto");
var nacl_test = {
2013-04-11 05:52:48 -04:00
keySize: 128,
nonceSize: 192
2013-04-04 08:28:07 -04:00
};
test("Init", 1, function() {
// init dependencies
2013-04-05 11:56:47 -04:00
nacl_test.util = new app.crypto.Util(window, uuid);
ok(nacl_test.util, 'Util');
2013-04-04 08:28:07 -04:00
// generate test data
nacl_test.test_message = new TestData().generateBigString(1000);
nacl_test.crypto = new app.crypto.NaclCrypto(nacl, nacl_test.util);
2013-04-04 08:28:07 -04:00
});
2013-04-05 11:56:47 -04:00
test("Generate Keypair from seed", 1, function() {
// generate keypair from seed
var seed = nacl_test.util.random(128);
var keys = nacl_test.crypto.generateKeypair(seed);
2013-04-11 05:52:48 -04:00
ok(keys.boxSk && keys.boxPk && keys.id, "Keypair: " + JSON.stringify(keys));
2013-04-05 11:56:47 -04:00
});
2013-04-05 11:43:41 -04:00
test("Generate Keypair", 2, function() {
2013-04-05 11:56:47 -04:00
// generate keypair
var senderKeypair = nacl_test.crypto.generateKeypair();
ok(senderKeypair.boxSk && senderKeypair.boxPk, "Sender keypair: " + JSON.stringify(senderKeypair));
var recipientKeypair = nacl_test.crypto.generateKeypair();
ok(recipientKeypair.boxSk && recipientKeypair.boxPk, "Receiver keypair: " + JSON.stringify(recipientKeypair));
nacl_test.senderKeypair = senderKeypair;
nacl_test.recipientKeypair = recipientKeypair;
2013-04-04 08:28:07 -04:00
});
test("Asymmetric En/Decrypt (Synchronous)", 3, function() {
2013-04-04 08:28:07 -04:00
var plaintext = nacl_test.test_message;
2013-04-10 11:09:39 -04:00
var nonce = nacl_test.crypto.generateNonce();
ok(nonce, 'Nonce: ' + nonce);
2013-04-04 08:28:07 -04:00
// encrypt
var ct = nacl_test.crypto.asymEncryptSync(plaintext, nonce, nacl_test.recipientKeypair.boxPk, nacl_test.senderKeypair.boxSk);
2013-04-10 11:09:39 -04:00
ok(ct, 'Ciphertext length: ' + ct.length);
2013-04-04 08:28:07 -04:00
// decrypt
var decrypted = nacl_test.crypto.asymDecryptSync(ct, nonce, nacl_test.senderKeypair.boxPk, nacl_test.recipientKeypair.boxSk);
2013-04-04 08:28:07 -04:00
equal(decrypted, plaintext, 'Decryption correct: ' + decrypted);
});
asyncTest("Asymmetric En/Decrypt (Async/Worker)", 3, function() {
var plaintext = nacl_test.test_message;
2013-04-10 11:09:39 -04:00
var nonce = nacl_test.crypto.generateNonce();
ok(nonce, 'Nonce: ' + nonce);
// encrypt
nacl_test.crypto.asymEncrypt(plaintext, nonce, nacl_test.recipientKeypair.boxPk, nacl_test.senderKeypair.boxSk, function(ct) {
ok(ct, 'Ciphertext length: ' + ct.length);
2013-04-10 11:09:39 -04:00
// decrypt
nacl_test.crypto.asymDecrypt(ct, nonce, nacl_test.senderKeypair.boxPk, nacl_test.recipientKeypair.boxSk, function(decrypted) {
equal(decrypted, plaintext, 'Decryption correct: ' + decrypted);
2013-04-10 11:09:39 -04:00
start();
});
});
2013-04-04 08:28:07 -04:00
});