greasemonkey/greasemonkey — Issue #1473: GM_md5, GM_sha1
Author: benibela
Created: 2011-11-18 19:03:34+00:00
State: open

There should be sha1/md5 functions in GM, like you get them if you add this to greasemonkey.js

sandbox.GM_xmlhttpRequest = GM_util.hitch(
new GM_xmlhttpRequester(aContentWin, aChromeWin, aUrl),
'contentStartRequest');
+ GM_util.sha1(""); GM_util.md5(""); //to assure they are loaded, guess there is a better way to do it
+ sandbox.GM_sha1 = GM_util.hitch(GM_util, 'sha1');
+ sandbox.GM_md5 = GM_util.hitch(GM_util, 'md5');
return sandbox;
}

And this in a md5.js file in util:

Components.utils.import('resource://greasemonkey/util.js');

const EXPORTED_SYMBOLS = ['md5'];

function md5(unicode) {
var unicodeConverter = Components
.classes["@mozilla.org/intl/scriptableunicodeconverter"]
.createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
unicodeConverter.charset = "UTF-8";

var data = unicodeConverter.convertToByteArray(unicode, {});
var ch = Components.classes["@mozilla.org/security/hash;1"]
.createInstance(Components.interfaces.nsICryptoHash);
ch.init(ch.MD5);
ch.update(data, data.length);
var hash = ch.finish(false); // hash as raw octets

var hex = [];
for (var i = 0; i < hash.length; i++) {
hex.push( ("0" + hash.charCodeAt(i).toString(16)).slice(-2) );
}
return hex.join('');
}
md5 = GM_util.memoize(md5);