mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.3 KiB
51 lines
1.3 KiB
|
|
var fs = require("graceful-fs")
|
|
, crypto = require("crypto")
|
|
, log = require("npmlog")
|
|
, binding
|
|
|
|
try { binding = 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, sum, cb) {
|
|
get(file, function (er, actual) {
|
|
if (er) {
|
|
log.error("shasum", "error getting shasum")
|
|
return cb(er)
|
|
}
|
|
var expected = sum.toLowerCase().trim()
|
|
, ok = actual === expected
|
|
cb(ok ? null : new Error(
|
|
"shasum check failed for "+file+"\n"
|
|
+"Expected: "+expected+"\n"
|
|
+"Actual: "+actual))
|
|
})
|
|
}
|
|
|
|
function get (file, cb) {
|
|
var h = crypto.createHash("sha1")
|
|
, s = fs.createReadStream(file)
|
|
, errState = null
|
|
s.on("error", function (er) {
|
|
if (errState) return
|
|
log.silly("shasum", "error", er)
|
|
return cb(errState = er)
|
|
}).on("data", function (chunk) {
|
|
if (errState) return
|
|
log.silly("shasum", "updated bytes", chunk.length)
|
|
h.update(chunk)
|
|
}).on("end", function () {
|
|
if (errState) return
|
|
var actual = h.digest("hex").toLowerCase().trim()
|
|
log.info("shasum", actual+"\n"+file)
|
|
cb(null, actual)
|
|
})
|
|
}
|
|
|