1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-22 08:52:15 -05:00

implemented wrapper for symmetric nacl encryption

This commit is contained in:
Tankred Hase 2013-04-10 10:51:03 +02:00
parent a2d147a564
commit 6841bd538c
2 changed files with 62 additions and 3 deletions

View File

@ -38,8 +38,9 @@ var NaclCrypto = function(nacl, util) {
var senderBoxSkBuf = nacl.encode_latin1(util.base642Str(senderBoxSk));
// generate nonce
var nonce = nacl.crypto_secretbox_random_nonce();
// encrypt
var ct = nacl.crypto_box(ptBuf, nonce, recipientBoxPkBuf, senderBoxSkBuf);
// encode to base64
var ctBase64 = util.str2Base64(nacl.decode_latin1(ct));
var nonceBase64 = util.str2Base64(nacl.decode_latin1(nonce));
@ -63,8 +64,53 @@ var NaclCrypto = function(nacl, util) {
var nonceBuf = nacl.encode_latin1(util.base642Str(nonce));
var senderBoxPkBuf = nacl.encode_latin1(util.base642Str(senderBoxPk));
var recipientBoxSkBuf = nacl.encode_latin1(util.base642Str(recipientBoxSk));
// decrypt
var pt = nacl.crypto_box_open(ctBuf, nonceBuf, senderBoxPkBuf, recipientBoxSkBuf);
// decode to string
var ptStr = nacl.decode_utf8(pt);
return ptStr;
};
/**
* Symmetrically encrypt a String
* @param plaintext [String] The input string in UTF8
* @param secretKey [String] The receiver's base64 encoded public key
* @return [Object] The base64 encoded ciphertext and nonce
*/
this.symmetricEncrypt = function(plaintext, secretKey) {
// convert to Uint8Array
var ptBuf = nacl.encode_utf8(plaintext);
var secretKeyBuf = nacl.encode_latin1(util.base642Str(secretKey));
// generate nonce
var nonce = nacl.crypto_secretbox_random_nonce();
// encrypt
var ct = nacl.crypto_secretbox(ptBuf, nonce, secretKeyBuf);
// encode to base64
var ctBase64 = util.str2Base64(nacl.decode_latin1(ct));
var nonceBase64 = util.str2Base64(nacl.decode_latin1(nonce));
return {
ct: ctBase64,
nonce: nonceBase64
};
};
/**
* Symmetrically decrypt a String
* @param ciphertext [String] The base64 encoded ciphertext
* @param nonce [String] The base64 encoded nonce
* @param secretKey [String] The sender's base64 encoded public key
* @return [String] The decrypted plaintext in UTF8
*/
this.symmetricDecrypt = function(ciphertext, nonce, secretKey) {
// convert to Uint8Array
var ctBuf = nacl.encode_latin1(util.base642Str(ciphertext));
var nonceBuf = nacl.encode_latin1(util.base642Str(nonce));
var secretKeyBuf = nacl.encode_latin1(util.base642Str(secretKey));
// decrypt
var pt = nacl.crypto_secretbox_open(ctBuf, nonceBuf, secretKeyBuf);
// decode to string
var ptStr = nacl.decode_utf8(pt);
return ptStr;

View File

@ -31,7 +31,7 @@ test("Generate Keypair", 2, function() {
nacl_test.recipientKeypair = recipientKeypair;
});
test("En/Decrypt", 2, function() {
test("Asymmetric En/Decrypt", 2, function() {
var plaintext = nacl_test.test_message;
// encrypt
@ -41,4 +41,17 @@ test("En/Decrypt", 2, function() {
// decrypt
var decrypted = nacl_test.crypto.asymmetricDecrypt(ct.ct, ct.nonce, nacl_test.senderKeypair.boxPk, nacl_test.recipientKeypair.boxSk);
equal(decrypted, plaintext, 'Decryption correct: ' + decrypted);
});
test("Symmetric En/Decrypt", 2, function() {
var plaintext = nacl_test.test_message;
// encrypt
var ct = nacl_test.crypto.symmetricEncrypt(plaintext, nacl_test.senderKeypair.boxSk);
ok(ct.ct && ct.nonce, 'Ciphertext length: ' + ct.ct.length);
// decrypt
var decrypted = nacl_test.crypto.symmetricDecrypt(ct.ct, ct.nonce, nacl_test.senderKeypair.boxSk);
equal(decrypted, plaintext, 'Decryption correct: ' + decrypted);
});