HazAT/jCryption — Issue #11: Keys provider
Author: MartyIX
Created: 2012-03-07 14:19:55+00:00
State: open

Hi,

first of all, I would like to thank you for your plugin! I would like to propose a modification of function getKeys in file: https://github.com/HazAT/jCryption/blob/master/jquery.jcryption.js#L130 -- the keys are downloaded from the url but sometimes it is not necessary as I can send the key from PHP to Javascript.

The below method accepts an url OR the public key itself. As an advantage I see it is possible to save one (or more) connection to the server.

/**
* Gets the RSA keys from the specified url, and saves it into a RSA keypair
* @param {string|array} keysProvider The url to contact OR the public key itself
* @param {function} callback The function to call when the operation has finshed
*/
$.jCryption.getKeys = function(keysProvider, callback) {
var jCryptionKeyPair = function(encryptionExponent, modulus, maxdigits) {
setMaxDigits(parseInt(maxdigits,10));
this.e = biFromHex(encryptionExponent);
this.m = biFromHex(modulus);
this.chunkSize = 2 * biHighIndex(this.m);
this.radix = 16;
this.barrett = new BarrettMu(this.m);
};

var keyReceivedCallback = function(data) {
var keys = new jCryptionKeyPair(data.e, data.n, data.maxdigits);
if($.isFunction(callback)) {
callback.call(this, keys);
}
}

if (typeof(keysProvider) == 'string') {
/**
* Gets the data from the specified url, and converts it into a RSA keypair
* @param {string} url The URL to contact
* @param {string} data The JSON data
*/
$.getJSON(keysProvider, keyReceivedCallback);
} else {
keyReceivedCallback(keysProvider);
}
};

Regards,
Martin