1
0
mirror of https://github.com/moparisthebest/mail synced 2024-11-15 21:55:04 -05:00
mail/src/js/crypto/nacl-crypto.js

128 lines
4.2 KiB
JavaScript
Raw Normal View History

2013-04-04 08:28:07 -04:00
/**
* A Wrapper for NaCl's asymmetric/symmetric crypto
*/
var NaclCrypto = function(nacl, util) {
2013-04-04 08:28:07 -04:00
'use strict';
/**
* Generates a baes64 encoded keypair for use with NaCl
2013-04-05 11:56:47 -04:00
* @param seed [String] A base64 encoded (pseudo) random seed e.g. PBKDF2
*/
this.generateKeypair = function(seed) {
2013-04-05 11:56:47 -04:00
var keys;
if (seed) {
2013-04-05 11:56:47 -04:00
var seedBuf = nacl.encode_latin1(util.base642Str(seed));
keys = nacl.crypto_box_keypair_from_seed(seedBuf);
} else {
2013-04-05 11:56:47 -04:00
keys = nacl.crypto_box_keypair();
}
2013-04-05 11:56:47 -04:00
return {
boxPk: util.str2Base64(nacl.decode_latin1(keys.boxPk)),
boxSk: util.str2Base64(nacl.decode_latin1(keys.boxSk))
};
};
2013-04-10 11:09:39 -04:00
/**
* Generates a random nonce and returns it base64 encoded
*/
this.generateNonce = function() {
// generate nonce
var nonce = nacl.crypto_secretbox_random_nonce();
var nonceBase64 = util.str2Base64(nacl.decode_latin1(nonce));
return nonceBase64;
};
/**
* Asymmetrically encrypt a String
2013-04-04 08:28:07 -04:00
* @param plaintext [String] The input string in UTF8
2013-04-10 11:09:39 -04:00
* @param nonce [String] The base64 encoded nonce
2013-04-11 03:46:22 -04:00
* @param recipientPk [String] The receiver's base64 encoded public key
* @param senderSk [String] The sender's base64 encoded private key
* @return [Object] The base64 encoded ciphertext and nonce
2013-04-04 08:28:07 -04:00
*/
2013-04-11 03:46:22 -04:00
this.asymEncrypt = function(plaintext, nonce, recipientPk, senderSk) {
// convert to Uint8Array
var ptBuf = nacl.encode_utf8(plaintext);
2013-04-11 03:46:22 -04:00
var recipientPkBuf = nacl.encode_latin1(util.base642Str(recipientPk));
var senderSkBuf = nacl.encode_latin1(util.base642Str(senderSk));
2013-04-10 11:09:39 -04:00
var nonceBuf = nacl.encode_latin1(util.base642Str(nonce));
// encrypt
2013-04-11 03:46:22 -04:00
var ct = nacl.crypto_box(ptBuf, nonceBuf, recipientPkBuf, senderSkBuf);
// encode to base64
2013-04-05 11:43:41 -04:00
var ctBase64 = util.str2Base64(nacl.decode_latin1(ct));
2013-04-04 08:28:07 -04:00
2013-04-10 11:09:39 -04:00
return ctBase64;
2013-04-04 08:28:07 -04:00
};
/**
* Asymmetrically decrypt a String
2013-04-04 08:28:07 -04:00
* @param ciphertext [String] The base64 encoded ciphertext
* @param nonce [String] The base64 encoded nonce
2013-04-11 03:46:22 -04:00
* @param senderPk [String] The sender's base64 encoded public key
* @param recipientSk [String] The receiver's base64 encoded private key
2013-04-04 08:28:07 -04:00
* @return [String] The decrypted plaintext in UTF8
*/
2013-04-11 03:46:22 -04:00
this.asymDecrypt = function(ciphertext, nonce, senderPk, recipientSk) {
// convert to Uint8Array
2013-04-05 11:43:41 -04:00
var ctBuf = nacl.encode_latin1(util.base642Str(ciphertext));
var nonceBuf = nacl.encode_latin1(util.base642Str(nonce));
2013-04-11 03:46:22 -04:00
var senderPkBuf = nacl.encode_latin1(util.base642Str(senderPk));
var recipientSkBuf = nacl.encode_latin1(util.base642Str(recipientSk));
// decrypt
2013-04-11 03:46:22 -04:00
var pt = nacl.crypto_box_open(ctBuf, nonceBuf, senderPkBuf, recipientSkBuf);
// decode to string
var ptStr = nacl.decode_utf8(pt);
return ptStr;
};
/**
* Symmetrically encrypt a String
* @param plaintext [String] The input string in UTF8
2013-04-10 11:09:39 -04:00
* @param nonce [String] The base64 encoded nonce
* @param secretKey [String] The receiver's base64 encoded public key
* @return [Object] The base64 encoded ciphertext and nonce
*/
2013-04-10 13:14:48 -04:00
this.symEncrypt = function(plaintext, nonce, secretKey) {
// convert to Uint8Array
var ptBuf = nacl.encode_utf8(plaintext);
var secretKeyBuf = nacl.encode_latin1(util.base642Str(secretKey));
2013-04-10 11:09:39 -04:00
var nonceBuf = nacl.encode_latin1(util.base642Str(nonce));
// encrypt
2013-04-10 11:09:39 -04:00
var ct = nacl.crypto_secretbox(ptBuf, nonceBuf, secretKeyBuf);
// encode to base64
var ctBase64 = util.str2Base64(nacl.decode_latin1(ct));
2013-04-10 11:09:39 -04:00
return ctBase64;
};
/**
* 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
*/
2013-04-10 13:14:48 -04:00
this.symDecrypt = 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);
2013-04-04 08:28:07 -04:00
return ptStr;
2013-04-04 08:28:07 -04:00
};
2013-04-05 11:43:41 -04:00
};
if (typeof module !== 'undefined' && module.exports) {
module.exports = NaclCrypto;
} else {
app.crypto.NaclCrypto = NaclCrypto;
}