mail/src/js/crypto/rsa.js

113 lines
2.8 KiB
JavaScript
Raw Normal View History

2013-05-14 10:05:31 -04:00
/**
* A Wrapper for Forge's RSA encryption
*/
app.crypto.RSA = function(forge) {
'use strict';
2013-05-14 12:48:13 -04:00
var utl = forge.util;
2013-05-14 10:05:31 -04:00
var publicKey = null,
privateKey = null;
/**
* Initializes the RSA module by passing the user's keypair
* The private key is option and required only for decryption
* and signing
*/
this.init = function(pubkeyPem, privkeyPem) {
publicKey = forge.pki.publicKeyFromPem(pubkeyPem);
if (privkeyPem) {
privateKey = forge.pki.privateKeyFromPem(privkeyPem);
}
};
/**
* Generate RSA keypair with the corresponding keysize
*/
this.generateKeypair = function(keySize, callback) {
forge.rsa.generateKeyPair({
bits: keySize,
workerScript: app.config.workerPath + '/../../lib/forge/prime.worker.js'
}, function(err, keypair) {
if (err || !keypair.publicKey || !keypair.privateKey) {
callback({
errMsg: 'RSA keygeneration failed!',
err: err
});
return;
}
publicKey = keypair.publicKey;
privateKey = keypair.privateKey;
callback();
});
};
/**
* Exports user's keypair as PEMs
*/
this.exportKeys = function() {
return {
pubkeyPem: forge.pki.publicKeyToPem(publicKey),
privkeyPem: forge.pki.privateKeyToPem(privateKey)
};
};
/**
* Encrypt a String using RSA with PKCS#1 v1.5 padding
2013-05-14 12:48:13 -04:00
* @param plaintext [String] The input string in UTF-16
2013-05-14 10:05:31 -04:00
* @return [String] The base64 encoded ciphertext
*/
this.encrypt = function(plaintext) {
2013-05-14 12:48:13 -04:00
// encode plaintext to utf8 and encrypt
var ct = publicKey.encrypt(utl.encodeUtf8(plaintext));
// encode ciphtext to base64
return utl.encode64(ct);
2013-05-14 10:05:31 -04:00
};
/**
* Decrypt a String using RSA with PKCS#1 v1.5 padding
* @param ciphertext [String] The base64 encoded ciphertext
2013-05-14 12:48:13 -04:00
* @return [String] The decrypted plaintext in UTF-16
2013-05-14 10:05:31 -04:00
*/
this.decrypt = function(ciphertext) {
2013-05-14 12:48:13 -04:00
// decode base64 ciphertext to utf8
var ctUtf8 = utl.decode64(ciphertext);
// decrypt and decode to utf16
return utl.decodeUtf8(privateKey.decrypt(ctUtf8));
2013-05-14 10:05:31 -04:00
};
/**
* Signs an Array of Base64 encoded parts with the private key
* @param parts [Array] Array of Base64 encoded parts
* @return [String] The Base64 encoded signature
*/
this.sign = function(parts) {
2013-05-14 10:05:31 -04:00
var sha = forge.md.sha256.create();
parts.forEach(function(i) {
2013-05-14 12:48:13 -04:00
sha.update(utl.decode64(i));
});
2013-05-14 10:05:31 -04:00
2013-05-14 12:48:13 -04:00
return utl.encode64(privateKey.sign(sha));
2013-05-14 10:05:31 -04:00
};
/**
* Verifies an Array of Base64 encoded parts with the public key
* @param parts [Array] Array of Base64 encoded parts
* @param sig [String] The Base64 encoded signatrure
* @return [bool] if the verification was successful
*/
this.verify = function(parts, sig) {
2013-05-14 10:05:31 -04:00
// parse base64 signature to utf8
2013-05-14 12:48:13 -04:00
var sigUtf8 = utl.decode64(sig);
2013-05-14 10:05:31 -04:00
var sha = forge.md.sha256.create();
parts.forEach(function(i) {
2013-05-14 12:48:13 -04:00
sha.update(utl.decode64(i));
});
2013-05-14 10:05:31 -04:00
return publicKey.verify(sha.digest().getBytes(), sigUtf8);
};
};