Question

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('...');
Was it helpful?

Solution

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.

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top