mirror of
https://github.com/moparisthebest/mail
synced 2024-11-26 10:52:17 -05:00
implemented wrapper for symmetric nacl encryption
This commit is contained in:
parent
a2d147a564
commit
6841bd538c
@ -38,8 +38,9 @@ var NaclCrypto = function(nacl, util) {
|
|||||||
var senderBoxSkBuf = nacl.encode_latin1(util.base642Str(senderBoxSk));
|
var senderBoxSkBuf = nacl.encode_latin1(util.base642Str(senderBoxSk));
|
||||||
// generate nonce
|
// generate nonce
|
||||||
var nonce = nacl.crypto_secretbox_random_nonce();
|
var nonce = nacl.crypto_secretbox_random_nonce();
|
||||||
|
// encrypt
|
||||||
var ct = nacl.crypto_box(ptBuf, nonce, recipientBoxPkBuf, senderBoxSkBuf);
|
var ct = nacl.crypto_box(ptBuf, nonce, recipientBoxPkBuf, senderBoxSkBuf);
|
||||||
|
// encode to base64
|
||||||
var ctBase64 = util.str2Base64(nacl.decode_latin1(ct));
|
var ctBase64 = util.str2Base64(nacl.decode_latin1(ct));
|
||||||
var nonceBase64 = util.str2Base64(nacl.decode_latin1(nonce));
|
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 nonceBuf = nacl.encode_latin1(util.base642Str(nonce));
|
||||||
var senderBoxPkBuf = nacl.encode_latin1(util.base642Str(senderBoxPk));
|
var senderBoxPkBuf = nacl.encode_latin1(util.base642Str(senderBoxPk));
|
||||||
var recipientBoxSkBuf = nacl.encode_latin1(util.base642Str(recipientBoxSk));
|
var recipientBoxSkBuf = nacl.encode_latin1(util.base642Str(recipientBoxSk));
|
||||||
|
// decrypt
|
||||||
var pt = nacl.crypto_box_open(ctBuf, nonceBuf, senderBoxPkBuf, recipientBoxSkBuf);
|
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);
|
var ptStr = nacl.decode_utf8(pt);
|
||||||
|
|
||||||
return ptStr;
|
return ptStr;
|
||||||
|
@ -31,7 +31,7 @@ test("Generate Keypair", 2, function() {
|
|||||||
nacl_test.recipientKeypair = recipientKeypair;
|
nacl_test.recipientKeypair = recipientKeypair;
|
||||||
});
|
});
|
||||||
|
|
||||||
test("En/Decrypt", 2, function() {
|
test("Asymmetric En/Decrypt", 2, function() {
|
||||||
var plaintext = nacl_test.test_message;
|
var plaintext = nacl_test.test_message;
|
||||||
|
|
||||||
// encrypt
|
// encrypt
|
||||||
@ -42,3 +42,16 @@ test("En/Decrypt", 2, function() {
|
|||||||
var decrypted = nacl_test.crypto.asymmetricDecrypt(ct.ct, ct.nonce, nacl_test.senderKeypair.boxPk, nacl_test.recipientKeypair.boxSk);
|
var decrypted = nacl_test.crypto.asymmetricDecrypt(ct.ct, ct.nonce, nacl_test.senderKeypair.boxPk, nacl_test.recipientKeypair.boxSk);
|
||||||
equal(decrypted, plaintext, 'Decryption correct: ' + decrypted);
|
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);
|
||||||
|
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user