1 | #!/usr/bin/env node
|
2 |
|
3 | 'use strict'
|
4 |
|
5 | var yargs = require('yargs')
|
6 |
|
7 | yargs.fail(function (msg) {
|
8 | var help = yargs.help()
|
9 |
|
10 | if (msg) {
|
11 | help += '\n' + msg
|
12 | }
|
13 |
|
14 | throw help
|
15 | })
|
16 |
|
17 |
|
18 |
|
19 | var hashy = require('./')
|
20 |
|
21 |
|
22 |
|
23 | function main (argv) {
|
24 | var options = yargs
|
25 | .usage('Usage: hashy [<option>...]')
|
26 | .example('hashy <secret>', 'hash the secret')
|
27 | .example('hashy <secret> <hash>', 'verify the secret using the hash')
|
28 | .options({
|
29 | h: {
|
30 | alias: 'help',
|
31 | boolean: true,
|
32 | describe: 'display this help message'
|
33 | },
|
34 | v: {
|
35 | alias: 'version',
|
36 | boolean: true,
|
37 | describe: 'display the version number'
|
38 | },
|
39 | c: {
|
40 | alias: 'cost',
|
41 | describe: 'cost for Bcrypt'
|
42 | }
|
43 | })
|
44 | .parse(argv)
|
45 |
|
46 | if (options.help) {
|
47 | return yargs.help()
|
48 | }
|
49 |
|
50 | if (options.version) {
|
51 | var pkg = require('./package')
|
52 | return 'Hashy version ' + pkg.version
|
53 | }
|
54 |
|
55 | if (options.cost) {
|
56 | hashy.options.bcrypt.cost = +options.cost
|
57 | }
|
58 |
|
59 | var args = options._
|
60 |
|
61 | if (args.length === 1) {
|
62 | return hashy.hash(args[0]).then(console.log)
|
63 | }
|
64 |
|
65 | if (args.length === 2) {
|
66 | var password = args[0]
|
67 | var hash = args[1]
|
68 |
|
69 | return hashy.verify(password, hash).then(function (success) {
|
70 | if (success) {
|
71 | if (hashy.needsRehash(hash)) {
|
72 | return 'ok but password should be rehashed'
|
73 | }
|
74 |
|
75 | return 'ok'
|
76 | }
|
77 |
|
78 | throw new Error('not ok')
|
79 | })
|
80 | }
|
81 |
|
82 | throw new Error('incorrect number of arguments')
|
83 | }
|
84 | exports = module.exports = main
|
85 |
|
86 |
|
87 |
|
88 | if (!module.parent) {
|
89 | require('exec-promise')(main)
|
90 | }
|