Skip to content

Instantly share code, notes, and snippets.

@scottgonzalez
Created July 21, 2010 23:21
Show Gist options
  • Select an option

  • Save scottgonzalez/485299 to your computer and use it in GitHub Desktop.

Select an option

Save scottgonzalez/485299 to your computer and use it in GitHub Desktop.
var crypto = require('crypto');
["sha1", "md5", "sha256", "sha512", "ripemd160"].forEach(function(algo) {
exports[algo] = function(data, salt) {
return (typeof salt != 'undefined' ?
crypto.createHmac(algo, salt) :
crypto.createHash(algo)
).update(data).digest("hex");
};
});
@Marak

Marak commented Jul 22, 2010

Copy link
Copy Markdown

ahahaha, isaacs did the exact same thing :p

@scottgonzalez

Copy link
Copy Markdown
Author

mine's totally smaller, just look at the awesome use of the grouping with the ternary :-)

@Marak

Marak commented Jul 22, 2010

Copy link
Copy Markdown

im totally gonna sue you now

@isaacs

isaacs commented Jul 22, 2010

Copy link
Copy Markdown

@Marak

Marak commented Jul 22, 2010

Copy link
Copy Markdown

^^^ aahahah isaacs

 var crypto = require('crypto');

 ["sha1", "md5", "sha256", "sha512", "ripemd160"].forEach(function(algo) {
   exports[algo] = function(data, salt) {
     return crypto["createH"+(typeof salt != 'undefined' ? "mac":"ash"](algo,salt)
            .update(data).digest("hex");
   };
 });

@scottgonzalez

Copy link
Copy Markdown
Author

I almost wrote that but opted for readability :-P Now let's all stop playing around and land this in node :-)

@Marak

Marak commented Jul 22, 2010

Copy link
Copy Markdown

so i think we are going to sit on this for a few days.

isaacs has some really good ideas for doing streaming file checksums. lets see if anyone else responds on the mailing list

@Marak

Marak commented Jul 22, 2010

Copy link
Copy Markdown

something like

 var h = hasher("md5"); sys.pump(myFileReadStream, h); h.on("data", function (hash) { assert(hash === expectedHash) }

@isaacs

isaacs commented Jul 22, 2010

Copy link
Copy Markdown

Oh my god, you totally took my non-open-source proprietary IM and open sourced it on github.

You are so super sued.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment