Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Move SHA1 (in its own file) to GM_sha1 (in utils.js).
  • Loading branch information
arantius committed May 8, 2010
1 parent 1d60c19 commit 620edf1
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
1 change: 0 additions & 1 deletion components/greasemonkey.js
Expand Up @@ -145,7 +145,6 @@ var greasemonkeyService = {
loader.loadSubScript("chrome://greasemonkey/content/miscapis.js");
loader.loadSubScript("chrome://greasemonkey/content/xmlhttprequester.js");
loader.loadSubScript("chrome://greasemonkey/content/scriptdownloader.js");
loader.loadSubScript("chrome://greasemonkey/content/sha1.js");
},

shouldLoad: function(ct, cl, org, ctx, mt, ext) {
Expand Down
4 changes: 2 additions & 2 deletions content/config.js
Expand Up @@ -78,7 +78,7 @@ Config.prototype = {
script._modified = script._file.lastModifiedTime;
var rawMeta = this.parse(
getContents(script._file), script._downloadURL, true)._rawMeta;
script._dependhash = SHA1(rawMeta);
script._dependhash = GM_sha1(rawMeta);
fileModified = true;
} else {
script._modified = node.getAttribute("modified");
Expand Down Expand Up @@ -361,7 +361,7 @@ Config.prototype = {
}

script._modified = script._file.lastModifiedTime;
script._metahash = SHA1(script._rawMeta);
script._metahash = GM_sha1(script._rawMeta);

this._scripts.push(script);
this._changed(script, "install", null);
Expand Down
2 changes: 1 addition & 1 deletion content/script.js
Expand Up @@ -138,7 +138,7 @@ Script.prototype = {
this._description = newScript._description;
this._unwrap = newScript._unwrap;

var dependhash = SHA1(newScript._rawMeta);
var dependhash = GM_sha1(newScript._rawMeta);
if (dependhash != this._dependhash && !newScript._dependFail) {
this._dependhash = dependhash;
this._requires = newScript._requires;
Expand Down
19 changes: 0 additions & 19 deletions content/sha1.js

This file was deleted.

22 changes: 22 additions & 0 deletions content/utils.js
Expand Up @@ -334,6 +334,28 @@ function GM_uriFromUrl(url, baseUrl) {
}
GM_uriFromUrl = GM_memoize(GM_uriFromUrl);

// UTF-8 encodes input, SHA-1 hashes it and returns the 40-char hex version.
function GM_sha1(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.SHA1);
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('');
}
GM_sha1 = GM_memoize(GM_sha1);

// Decorate a function with a memoization wrapper, with a limited-size cache
// to reduce peak memory utilization. Simple usage:
//
Expand Down

0 comments on commit 620edf1

Please sign in to comment.