diff options
author | ForbesLindesay <forbes@lindesay.co.uk> | 2013-04-28 05:04:33 +0400 |
---|---|---|
committer | isaacs <i@izs.me> | 2013-04-29 08:20:09 +0400 |
commit | f44b1fd0935a94e253ed650504d5720fefb72a9b (patch) | |
tree | feb43aaa0c0383efd7d00e6790f77b3c45ef8a70 /node_modules/sha/index.js | |
parent | 1ef9748c81882a90e61bbff6770c4ade23df7f37 (diff) |
replace `./lib/utils/sha.js` with `sha`
closes #3381
Diffstat (limited to 'node_modules/sha/index.js')
-rw-r--r-- | node_modules/sha/index.js | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/node_modules/sha/index.js b/node_modules/sha/index.js new file mode 100644 index 000000000..8e4e13683 --- /dev/null +++ b/node_modules/sha/index.js @@ -0,0 +1,63 @@ +var crypto = require('crypto') +var fs +try { + fs = require('graceful-fs') +} catch (ex) { + fs = require('fs') +} +try { + process.binding('crypto') +} catch (e) { + var er = new Error( 'crypto binding not found.\n' + + 'Please build node with openssl.\n' + + e.message ) + throw er +} + +exports.check = check +exports.get = get + +function check(file, expected, options, cb) { + if (typeof options === 'function') { + cb = options; + options = undefined; + } + expected = expected.toLowerCase().trim(); + get(file, options, function (er, actual) { + if (er) { + if (er.message) er.message += ' while getting shasum for ' + file; + return cb(er) + } + if (actual === expected) return cb(null); + cb(new Error( + 'shasum check failed for ' + file + '\n' + + 'Expected: ' + expected + '\n' + + 'Actual: ' + actual)) + }) +} + +function get(file, options, cb) { + if (typeof options === 'function') { + cb = options; + options = undefined; + } + options = options || {}; + var algorithm = options.algorithm || 'sha1'; + var hash = crypto.createHash(algorithm) + var source = fs.createReadStream(file) + var errState = null + source + .on('error', function (er) { + if (errState) return + return cb(errState = er) + }) + .on('data', function (chunk) { + if (errState) return + hash.update(chunk) + }) + .on('end', function () { + if (errState) return + var actual = hash.digest("hex").toLowerCase().trim() + cb(null, actual) + }) +} |