Skip to content

Commit 99a0fa9

Browse files
committed
Add CLI script
1 parent 82413e6 commit 99a0fa9

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

README.md

+42
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,48 @@ Warning: Decreasing `keyLength` or `work` can make your password database less s
102102
* @return {Object} credential object
103103

104104

105+
## CLI
106+
107+
```shell
108+
$ credential --help
109+
110+
Usage: cmd [options] [command]
111+
112+
113+
Commands:
114+
115+
hash [options] [password] Hash password
116+
verify [hash] <password> Verify password
117+
118+
Options:
119+
120+
-h, --help output usage information
121+
```
122+
123+
```shell
124+
$ credential hash --help
125+
126+
Usage: hash [options] [password]
127+
128+
Hash password
129+
130+
Options:
131+
132+
-h, --help output usage information
133+
-w --work <work> relative work load (0.5 for half the work)
134+
-k --key-length <key-length> length of salt
135+
```
136+
137+
The `password` argument for `hash` and the `hash` argument for `verify` both support piping by replacing with a dash (`-`):
138+
139+
```shell
140+
$ echo -n "my password" | credential hash - | credential verify - "my password"
141+
Verified
142+
```
143+
144+
Exit codes `0` and `1` is used to communicate verified or invalid as well.
145+
146+
105147
## Motivation
106148

107149
Several other libraries claim to do the same thing, but fall short. Several fail to use cryptographically secure salts, which make salt guessing possible. Others fail to use either a long enough salt, or a long enough hash. The salt should be the same size as the hash. No shorter, and no longer.

bin/cmd.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#!/usr/bin/env node
2+
'use strict';
3+
4+
var program = require('commander');
5+
var pluck = require('pluck-keys');
6+
var credential = require('../credential');
7+
8+
var stdin = '';
9+
10+
program
11+
.command('hash [password]')
12+
.description('Hash password')
13+
.option('-w --work <work>', 'relative work load (0.5 for half the work)', Number)
14+
.option('-k --key-length <key-length>', 'length of salt', Number)
15+
.action(function( password, options ){
16+
credential.configure(pluck([
17+
'keyLength',
18+
'hashMethod',
19+
'work'
20+
], options));
21+
22+
credential.hash(stdin || password, function( err, result ){
23+
if (err)
24+
return console.error(err);
25+
26+
console.log(result);
27+
});
28+
});
29+
30+
program
31+
.command('verify [hash] <password>')
32+
.description('Verify password')
33+
.action(function( hash, password ){
34+
credential.verify(stdin || hash, password, function( err, result ){
35+
if (err)
36+
return console.error(err);
37+
38+
console.log(result ? 'Verified' : 'Invalid');
39+
process.exit(result ? 0 : 1);
40+
});
41+
});
42+
43+
if (process.stdin.isTTY) {
44+
program.parse(process.argv);
45+
} else {
46+
process.stdin.on('readable', function(){
47+
stdin += this.read() || '';
48+
});
49+
50+
process.stdin.on('end', function(){
51+
program.parse(process.argv);
52+
});
53+
}

package.json

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,16 @@
44
"description": "Easy password hashing and verification in Node. Protects against brute force, rainbow tables, and timing attacks.",
55
"main": "credential.js",
66
"directories": {
7-
"test": "test"
7+
"test": "test",
8+
"bin": "bin"
9+
},
10+
"bin": {
11+
"credential": "bin/cmd.js"
812
},
913
"dependencies": {
10-
"mout": "~0.6.0"
14+
"mout": "~0.6.0",
15+
"commander": "^2.8.1",
16+
"pluck-keys": "^0.0.4"
1117
},
1218
"devDependencies": {
1319
"babel": "^5.6.14",

0 commit comments

Comments
 (0)