Created
June 17, 2013 19:06
-
-
Save zandroid/5799407 to your computer and use it in GitHub Desktop.
hashfile node utility for shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
/* | |
Usage in shell: | |
hashfile [--sha1|--md5] <file> [--eq <hash2>|--eqf <file2>] | |
Examples: | |
> hashfile package.json => print sha1 hash of package.json | |
> hashfile --md5 package.json => print md5 hash of package.json | |
> hashfile package.json --eq lkm43lkm4lr35n4jn3n34jn5k => true|false | |
> hashfile package.json --eqf ../../package.json => true|false | |
*/ | |
'use strict'; | |
var path = require('path'), | |
fs = require('fs'), | |
hash_file = require('hash_file'); | |
var basedir = process.cwd(); | |
var alg = process.argv[2], | |
hash, file, i = 3, hash2, file2; | |
function check() { | |
if (hash && hash2) { | |
console.log(hash === hash2); | |
} | |
} | |
switch (alg) { | |
case '--sha1': | |
case '--md5': | |
alg = alg.substring(2); | |
file = process.argv[3]; | |
i = 4; | |
break; | |
default: | |
file = alg; | |
alg = 'sha1'; | |
break; | |
} | |
switch (process.argv[i]) { | |
case '--eq': | |
hash2 = process.argv[i + 1]; | |
break; | |
case '--eqf': | |
file2 = process.argv[i + 1]; | |
break; | |
} | |
file = path.resolve(basedir, file); | |
if (!fs.existsSync(file)) { | |
console.error('File "' + file + '" does not exist.'); | |
return; | |
} | |
if (file2) { | |
file2 = path.resolve(basedir, file2); | |
if (!fs.existsSync(file2)) { | |
console.error('File "' + file2 + '" does not exist.'); | |
return; | |
} | |
hash_file(file, alg, function(error, res) { | |
if (error) { | |
console.error(error.message || error); | |
return; | |
} | |
hash = res; | |
check(); | |
}); | |
hash_file(file2, alg, function(error, res) { | |
if (error) { | |
console.error(error.message || error); | |
return; | |
} | |
hash2 = res; | |
check(); | |
}); | |
} | |
else { | |
hash_file(file, alg, function(error, res) { | |
if (error) { | |
console.error(error.message || error); | |
return; | |
} | |
console.log(hash2 ? hash2.toLowerCase() === res : res); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment