1

I need to generate a hash on each uploaded file. The hash must be identical to hash git generated for a given file (which is, in a layman term, a variant of sha1).

I looked into /nodejs-v0.10.22-src/core-modules-sources/lib/crypto.js. The library refers to native binding. For portability, I do not want to depends on native code.

Is there a way to add a custom crypto algorithm into nodejs crypto module, in JavaScript, such that I can do these:

var hash = crypto.createHash('githash');
hash.update('...');
3
  • What benefit are you trying to gain from using the native crypto module vs writing an independent module? Dec 10, 2013 at 7:20
  • I am trying to use connect.bodyParser, which accept an option for the hash algorithm. The hash specified must be a string, and is passed to the crypto library. Dec 10, 2013 at 7:57
  • I don't see any hash option on bodyParser, so it is hard to judge, but I've left an answer. Dec 10, 2013 at 8:55

2 Answers 2

1

The quick answer is no, you can't extend the hashes available in the crypto module. The given hash is checked at https://github.com/joyent/node/blob/v0.10.22/src/node_crypto.cc#L2856 and basically depends on the hashes supported by OpenSSL.

You may be able to potentially monkey-patch createHash to redirect to your own code, but wouldn't be advisable.

Instead, I'd recommend reworking your middleware usage to hash the data after the bodyParser is complete, or skip the standard bodyParser and implement your own for your specific use-case.

1

Some points to note here:

  1. git does not use a different hash. It is sha1. It just adds some extra chars to the data. See here. Adding another hash for this is not justified.

  2. You need it solely for connect's bodyParser, consider adding a patch there. But,

  3. bodyparser will be removed in connect 3.0. You will be expected to parse body/forms yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.