pax_global_header00006660000000000000000000000064122051316310014504gustar00rootroot0000000000000052 comment=73c9457821ae6181acf7e2cd2a4e47600e8d6d53 sha-1.2.3/000077500000000000000000000000001220513163100122625ustar00rootroot00000000000000sha-1.2.3/.gitignore000066400000000000000000000001421220513163100142470ustar00rootroot00000000000000lib-cov *.seed *.log *.csv *.dat *.out *.pid *.gz pids logs results npm-debug.log node_modules sha-1.2.3/.npmignore000066400000000000000000000000501220513163100142540ustar00rootroot00000000000000node_modules test .gitignore .travis.ymlsha-1.2.3/.travis.yml000066400000000000000000000000571220513163100143750ustar00rootroot00000000000000language: node_js node_js: - "0.8" - "0.10"sha-1.2.3/LICENSE000066400000000000000000000044261220513163100132750ustar00rootroot00000000000000Copyright (c) 2013 Forbes Lindesay The BSD License Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. The MIT License (MIT) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.sha-1.2.3/README.md000066400000000000000000000033001220513163100135350ustar00rootroot00000000000000# sha Check and get file hashes (using any algorithm) [![Build Status](https://travis-ci.org/ForbesLindesay/sha.png?branch=master)](https://travis-ci.org/ForbesLindesay/sha) [![Dependency Status](https://gemnasium.com/ForbesLindesay/sha.png)](https://gemnasium.com/ForbesLindesay/sha) [![NPM version](https://badge.fury.io/js/sha.png)](http://badge.fury.io/js/sha) ## Installation $ npm install sha ## API ### check(fileName, expected, [options,] cb) / checkSync(filename, expected, [options]) Asynchronously check that `fileName` has a "hash" of `expected`. The callback will be called with either `null` or an error (indicating that they did not match). Options: - algorithm: defaults to `sha1` and can be any of the algorithms supported by `crypto.createHash` ### get(fileName, [options,] cb) / getSync(filename, [options]) Asynchronously get the "hash" of `fileName`. The callback will be called with an optional `error` object and the (lower cased) hex digest of the hash. Options: - algorithm: defaults to `sha1` and can be any of the algorithms supported by `crypto.createHash` ### stream(expected, [options]) Check the hash of a stream without ever buffering it. This is a pass through stream so you can do things like: ```js fs.createReadStream('src') .pipe(sha.stream('expected')) .pipe(fs.createWriteStream('dest')) ``` `dest` will be a complete copy of `src` and an error will be emitted if the hash did not match `'expected'`. Options: - algorithm: defaults to `sha1` and can be any of the algorithms supported by `crypto.createHash` ## License You may use this software under the BSD or MIT. Take your pick. If you want me to release it under another license, open a pull request.sha-1.2.3/index.js000066400000000000000000000057661220513163100137450ustar00rootroot00000000000000'use strict' var Transform = require('stream').Transform || require('readable-stream').Transform 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.checkSync = checkSync exports.get = get exports.getSync = getSync exports.stream = stream 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 checkSync(file, expected, options) { expected = expected.toLowerCase().trim() var actual try { actual = getSync(file, options) } catch (er) { if (er.message) er.message += ' while getting shasum for ' + file throw er } if (actual !== expected) { var ex = new Error( 'shasum check failed for ' + file + '\n' + 'Expected: ' + expected + '\n' + 'Actual: ' + actual) throw ex } } 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) }) } function getSync(file, options) { options = options || {} var algorithm = options.algorithm || 'sha1' var hash = crypto.createHash(algorithm) var source = fs.readFileSync(file) hash.update(source) return hash.digest("hex").toLowerCase().trim() } function stream(expected, options) { expected = expected.toLowerCase().trim() options = options || {} var algorithm = options.algorithm || 'sha1' var hash = crypto.createHash(algorithm) var stream = new Transform() stream._transform = function (chunk, encoding, callback) { hash.update(chunk) stream.push(chunk) callback() } stream._flush = function (cb) { var actual = hash.digest("hex").toLowerCase().trim() if (actual === expected) return cb(null) cb(new Error( 'shasum check failed for:\n' + ' Expected: ' + expected + '\n' + ' Actual: ' + actual)) this.push(null) } return stream }sha-1.2.3/package.json000066400000000000000000000006041220513163100145500ustar00rootroot00000000000000{ "name": "sha", "version": "1.2.3", "description": "Check and get file hashes", "scripts": { "test": "mocha -R spec" }, "repository": { "type": "git", "url": "https://github.com/ForbesLindesay/sha.git" }, "license": "BSD", "optionalDependencies": { "graceful-fs": "2", "readable-stream": "1.0" }, "devDependencies": { "mocha": "~1.9.0" } }sha-1.2.3/test/000077500000000000000000000000001220513163100132415ustar00rootroot00000000000000sha-1.2.3/test/data000066400000000000000000000057661220513163100141130ustar00rootroot00000000000000'use strict' var Transform = require('stream').Transform || require('readable-stream').Transform 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.checkSync = checkSync exports.get = get exports.getSync = getSync exports.stream = stream 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 checkSync(file, expected, options) { expected = expected.toLowerCase().trim() var actual try { actual = getSync(file, options) } catch (er) { if (er.message) er.message += ' while getting shasum for ' + file throw er } if (actual !== expected) { var ex = new Error( 'shasum check failed for ' + file + '\n' + 'Expected: ' + expected + '\n' + 'Actual: ' + actual) throw ex } } 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) }) } function getSync(file, options) { options = options || {} var algorithm = options.algorithm || 'sha1' var hash = crypto.createHash(algorithm) var source = fs.readFileSync(file) hash.update(source) return hash.digest("hex").toLowerCase().trim() } function stream(expected, options) { expected = expected.toLowerCase().trim() options = options || {} var algorithm = options.algorithm || 'sha1' var hash = crypto.createHash(algorithm) var stream = new Transform() stream._transform = function (chunk, encoding, callback) { hash.update(chunk) stream.push(chunk) callback() } stream._flush = function (cb) { var actual = hash.digest("hex").toLowerCase().trim() if (actual === expected) return cb(null) cb(new Error( 'shasum check failed for:\n' + ' Expected: ' + expected + '\n' + ' Actual: ' + actual)) this.push(null) } return stream }sha-1.2.3/test/index.js000066400000000000000000000227451220513163100147200ustar00rootroot00000000000000'use strict' var sha = require('../') var assert = require('assert') var read = require('fs').createReadStream var write = require('fs').createWriteStream var del = require('fs').unlinkSync afterEach(function () { try { del(__dirname + '/output') } catch (ex) { if (ex.code !== 'ENOENT') throw ex } }) describe('get', function () { describe('(filename, callback(err, hash))', function () { describe('with a non-existant file', function () { it('results in an error', function (done) { sha.get(__dirname + '/non-existant', function (err, hash) { assert.equal(hash, undefined) assert.equal(err.code, 'ENOENT') return done() }) }) }) describe('with a file', function () { it('results in the `sha1` hash of the file', function (done) { sha.get(__dirname + '/data', function (err, hash) { assert.equal(hash, '068e929d0e5eb008bf404b15b10282f498a31f8b') return done() }) }) }) }) describe('(filename, {algorithm: "md5"}, callback(err, hash))', function () { describe('with a non-existant file', function () { it('results in an error', function (done) { sha.get(__dirname + '/non-existant', {algorithm: "md5"}, function (err, hash) { assert.equal(hash, undefined) assert.equal(err.code, 'ENOENT') return done() }) }) }) describe('with a file', function () { it('results in the `md5` hash of the file', function (done) { sha.get(__dirname + '/data', {algorithm: "md5"}, function (err, hash) { assert.equal(hash, '19a2013a3e497eaa0da5e6fae9d613ef') return done() }) }) }) }) }) describe('getSync', function () { describe('(filename)', function () { describe('with a non-existant file', function () { it('results in an error', function () { try { sha.getSync(__dirname + '/non-existant') } catch (err) { assert.equal(err.code, 'ENOENT') return } assert(false, 'Should throw an error') }) }) describe('with a file', function () { it('results in the `sha1` hash of the file', function () { var hash = sha.getSync(__dirname + '/data') assert.equal(hash, '068e929d0e5eb008bf404b15b10282f498a31f8b') }) }) }) describe('(filename, {algorithm: "md5"})', function () { describe('with a non-existant file', function () { it('results in an error', function () { try { sha.getSync(__dirname + '/non-existant', {algorithm: "md5"}) } catch (err) { assert.equal(err.code, 'ENOENT') return } assert(false, 'Should throw an error') }) }) describe('with a file', function () { it('results in the `md5` hash of the file', function () { var hash = sha.getSync(__dirname + '/data', {algorithm: "md5"}) assert.equal(hash, '19a2013a3e497eaa0da5e6fae9d613ef') }) }) }) }) describe('check', function () { describe('(filename, expected, callback(err, hash))', function () { describe('with a non-existant file', function () { it('results in an error', function (done) { sha.check(__dirname + '/non-existant', '068e929d0e5eb008bf404b15b10282f498a31f8b', function (err) { assert.equal(err.code, 'ENOENT') return done() }) }) }) describe('with the correct hash', function () { it('results in a `null` error', function (done) { sha.check(__dirname + '/data', '068e929d0e5eb008bf404b15b10282f498a31f8b', function (err) { assert.equal(err, null) return done() }) }) }) describe('with the wrong hash', function () { it('results in an error', function (done) { sha.check(__dirname + '/data', '19a2013a3e497eaa0da5e6fae9d613ef', function (err) { assert(err) assert(err instanceof Error) return done() }) }) }) }) describe('(filename, expected, {algorithm: "md5"}, callback(err, hash))', function () { describe('with a non-existant file', function () { it('results in an error', function (done) { sha.check(__dirname + '/non-existant', '19a2013a3e497eaa0da5e6fae9d613ef', {algorithm: "md5"}, function (err) { assert.equal(err.code, 'ENOENT') return done() }) }) }) describe('with the correct hash', function () { it('results in a `null` error', function (done) { sha.check(__dirname + '/data', '19a2013a3e497eaa0da5e6fae9d613ef', {algorithm: "md5"}, function (err) { assert.equal(err, null) return done() }) }) }) describe('with the wrong hash', function () { it('results in an error', function (done) { sha.check(__dirname + '/data', '068e929d0e5eb008bf404b15b10282f498a31f8b', {algorithm: "md5"}, function (err) { assert(err) assert(err instanceof Error) return done() }) }) }) }) }) describe('checkSync', function () { describe('(filename, expected)', function () { describe('with a non-existant file', function () { it('results in an error', function () { try { sha.checkSync(__dirname + '/non-existant', '068e929d0e5eb008bf404b15b10282f498a31f8b') } catch (err) { assert.equal(err.code, 'ENOENT') return } assert(false, 'Should throw an error') }) }) describe('with the correct hash', function () { it('does not result in an error', function () { sha.checkSync(__dirname + '/data', '068e929d0e5eb008bf404b15b10282f498a31f8b') }) }) describe('with the wrong hash', function () { it('results in an error', function () { try { sha.checkSync(__dirname + '/data', '19a2013a3e497eaa0da5e6fae9d613ef') } catch (err) { assert(err) assert(err instanceof Error) return } assert(false, 'Should throw an error') }) }) }) describe('(filename, expected, {algorithm: "md5"})', function () { describe('with a non-existant file', function () { it('results in an error', function () { try { sha.checkSync(__dirname + '/non-existant', '19a2013a3e497eaa0da5e6fae9d613ef', {algorithm: "md5"}) } catch (err) { assert.equal(err.code, 'ENOENT') return } assert(false, 'Should throw an error') }) }) describe('with the correct hash', function () { it('does not result in an error', function () { sha.checkSync(__dirname + '/data', '19a2013a3e497eaa0da5e6fae9d613ef', {algorithm: "md5"}) }) }) describe('with the wrong hash', function () { it('results in an error', function () { try { sha.checkSync(__dirname + '/data', '068e929d0e5eb008bf404b15b10282f498a31f8b', {algorithm: "md5"}) } catch (err) { assert(err) assert(err instanceof Error) return } assert(false, 'Should throw an error') }) }) }) }) describe('stream', function () { describe('.pipe(sha.stream(expected))', function () { describe('with the correct hash', function () { it('results in pass through', function (done) { var checkStream = read(__dirname + '/data') .pipe(sha.stream('068e929d0e5eb008bf404b15b10282f498a31f8b')) var writeStream = checkStream.pipe(write(__dirname + '/output')) checkStream.on('error', done) writeStream.on('close', function () { sha.check(__dirname + '/output', '068e929d0e5eb008bf404b15b10282f498a31f8b', done) }) }) }) describe('with the wrong hash', function () { it('results in an error', function (done) { var checkStream = read(__dirname + '/data') .pipe(sha.stream('19a2013a3e497eaa0da5e6fae9d613ef')) var writeStream = checkStream.pipe(write(__dirname + '/output')) var erred = false checkStream.on('error', function (err) { assert.ok(err) erred = true }) writeStream.on('close', function () { if (erred) return done() }) }) }) }) describe('.pipe(sha.stream(expected, {algorithm: "md5"}))', function () { describe('with the correct hash', function () { it('results in a `null` error', function (done) { var checkStream = read(__dirname + '/data') .pipe(sha.stream('19a2013a3e497eaa0da5e6fae9d613ef', {algorithm: "md5"})) var writeStream = checkStream.pipe(write(__dirname + '/output')) checkStream.on('error', done) writeStream.on('close', function () { sha.check(__dirname + '/output', '068e929d0e5eb008bf404b15b10282f498a31f8b', done) }) }) }) describe('with the wrong hash', function () { it('results in an error', function (done) { var checkStream = read(__dirname + '/data') .pipe(sha.stream('068e929d0e5eb008bf404b15b10282f498a31f8b', {algorithm: "md5"})) var writeStream = checkStream.pipe(write(__dirname + '/output')) var erred = false checkStream.on('error', function (err) { assert.ok(err) erred = true }) writeStream.on('close', function () { if (erred) return done() }) }) }) }) })