bin.js 991 B

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