1 |
3a515b92
|
cagy
|
#! /usr/bin/env node
|
2 |
|
|
|
3 |
|
|
var createHash = require('./browserify')
|
4 |
|
|
var argv = process.argv.slice(2)
|
5 |
|
|
|
6 |
|
|
function pipe (algorithm, s) {
|
7 |
|
|
var start = Date.now()
|
8 |
|
|
var hash = createHash(algorithm || 'sha1')
|
9 |
|
|
|
10 |
|
|
s.on('data', function (data) {
|
11 |
|
|
hash.update(data)
|
12 |
|
|
})
|
13 |
|
|
|
14 |
|
|
s.on('end', function () {
|
15 |
|
|
if (process.env.DEBUG) {
|
16 |
|
|
return console.log(hash.digest('hex'), Date.now() - start)
|
17 |
|
|
}
|
18 |
|
|
|
19 |
|
|
console.log(hash.digest('hex'))
|
20 |
|
|
})
|
21 |
|
|
}
|
22 |
|
|
|
23 |
|
|
function usage () {
|
24 |
|
|
console.error('sha.js [algorithm=sha1] [filename] # hash filename with algorithm')
|
25 |
|
|
console.error('input | sha.js [algorithm=sha1] # hash stdin with algorithm')
|
26 |
|
|
console.error('sha.js --help # display this message')
|
27 |
|
|
}
|
28 |
|
|
|
29 |
|
|
if (!process.stdin.isTTY) {
|
30 |
|
|
pipe(argv[0], process.stdin)
|
31 |
|
|
} else if (argv.length) {
|
32 |
|
|
if (/--help|-h/.test(argv[0])) {
|
33 |
|
|
usage()
|
34 |
|
|
} else {
|
35 |
|
|
var filename = argv.pop()
|
36 |
|
|
var algorithm = argv.pop()
|
37 |
|
|
pipe(algorithm, require('fs').createReadStream(filename))
|
38 |
|
|
}
|
39 |
|
|
} else {
|
40 |
|
|
usage()
|
41 |
|
|
}
|