NSS already has all the machinery you want (and much more); I'm not
sure you can access it directly via JS though...
It should be easy to write an OO interface (as in "IDL") for the most
common tasks and write a (portable!) implementation in native code.
Luca
If commands are going to be needing to use hashing functions, it may be
an idea for Ubiquity to provide an easier way to do so.
- Blair
Ah, I wasn't aware of this interface :)
> If commands are going to be needing to use hashing functions, it may be
> an idea for Ubiquity to provide an easier way to do so.
Something like this?
const Cc = Components.classes;
const Ci = Components.interfaces;
function Hash(algo) {
if (algo != 'md5' && algo != 'sha1' && algo != 'sha256' &&
algo != 'sha384' && algo != 'sha512')
throw new Error('Invalid hash algorithm: ' + algo);
this._ch = Cc["@mozilla.org/security/hash;1"].createInstance(Ci.nsICryptoHash);
this._algo = algo;
}
Hash.prototype = {
get _unicodeConverter() {
/* Singleton */
var converter = Cc['@mozilla.org/intl/scriptableunicodeconverter'].
createInstance(Ci.nsIScriptableUnicodeConverter);
converter.charset = 'UTF-8';
this.__defineGetter__('_unicodeConverter', function() converter);
return converter;
},
_toHexString: function(c) ('0' + c.toString(16)).slice(-2),
compute: function(str) {
// result is an out parameter,
// result.value will contain the array length
var result = {};
// data is an array of bytes
var data = this._unicodeConverter.convertToByteArray(str, result);
this._ch.initWithString(this._algo);
this._ch.update(data, data.length);
var hash = this._ch.finish(false);
// convert the binary hash data to a hex string.
var hexHash = [this._toHexString(hash.charCodeAt(i)) for (i in
hash)].join("");
return hexHash;
}
}
MD5 = function() {}
MD5.prototype = new Hash('md5');
SHA1 = function() {}
SHA1.prototype = new Hash('sha1');
SHA256 = function() {}
SHA256.prototype = new Hash('sha256');
SHA384 = function() {}
SHA384.prototype = new Hash('sha384');
SHA512 = function() {}
SHA512.prototype = new Hash('sha512');
L