3 var Transform = require('stream').Transform || require('readable-stream').Transform
4 var crypto = require('crypto')
5 var fs = require('graceful-fs')
8 exports.checkSync = checkSync
10 exports.getSync = getSync
11 exports.stream = stream
13 function check(file, expected, options, cb) {
14 if (typeof options === 'function') {
18 expected = expected.toLowerCase().trim()
19 get(file, options, function (er, actual) {
21 if (er.message) er.message += ' while getting shasum for ' + file
24 if (actual === expected) return cb(null)
26 'shasum check failed for ' + file + '\n'
27 + 'Expected: ' + expected + '\n'
28 + 'Actual: ' + actual))
31 function checkSync(file, expected, options) {
32 expected = expected.toLowerCase().trim()
35 actual = getSync(file, options)
37 if (er.message) er.message += ' while getting shasum for ' + file
40 if (actual !== expected) {
42 'shasum check failed for ' + file + '\n'
43 + 'Expected: ' + expected + '\n'
44 + 'Actual: ' + actual)
50 function get(file, options, cb) {
51 if (typeof options === 'function') {
55 options = options || {}
56 var algorithm = options.algorithm || 'sha1'
57 var hash = crypto.createHash(algorithm)
58 var source = fs.createReadStream(file)
61 .on('error', function (er) {
63 return cb(errState = er)
65 .on('data', function (chunk) {
69 .on('end', function () {
71 var actual = hash.digest("hex").toLowerCase().trim()
76 function getSync(file, options) {
77 options = options || {}
78 var algorithm = options.algorithm || 'sha1'
79 var hash = crypto.createHash(algorithm)
80 var source = fs.readFileSync(file)
82 return hash.digest("hex").toLowerCase().trim()
85 function stream(expected, options) {
86 expected = expected.toLowerCase().trim()
87 options = options || {}
88 var algorithm = options.algorithm || 'sha1'
89 var hash = crypto.createHash(algorithm)
91 var stream = new Transform()
92 stream._transform = function (chunk, encoding, callback) {
97 stream._flush = function (cb) {
98 var actual = hash.digest("hex").toLowerCase().trim()
99 if (actual === expected) return cb(null)
101 'shasum check failed for:\n'
102 + ' Expected: ' + expected + '\n'
103 + ' Actual: ' + actual))