-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcli.js
More file actions
executable file
·94 lines (74 loc) · 1.99 KB
/
cli.js
File metadata and controls
executable file
·94 lines (74 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env node
"use strict";
const yargs = require("yargs");
// TESTABILITY: makes yargs throws instead of exiting.
yargs.fail(function (msg) {
let help = yargs.help();
if (msg) {
help += "\n" + msg;
}
throw help;
});
// --------------------------------------------------------------------
const hashy = require("./");
// ====================================================================
function main(argv) {
const options = yargs
.usage("Usage: hashy [<option>...]")
.example("hashy [ -a <algorithm> ] <secret>", "hash the secret")
.example("hashy <secret> <hash>", "verify the secret using the hash")
.options({
a: {
default: hashy.DEFAULT_ALGO,
describe: "algorithm to use for hashing",
},
h: {
alias: "help",
boolean: true,
describe: "display this help message",
},
v: {
alias: "version",
boolean: true,
describe: "display the version number",
},
c: {
alias: "cost",
describe: "cost for Bcrypt",
},
})
.parse(argv);
if (options.help) {
return yargs.help();
}
if (options.version) {
const pkg = require("./package");
return "Hashy version " + pkg.version;
}
if (options.cost) {
hashy.options.bcrypt.cost = +options.cost;
}
const args = options._;
if (args.length === 1) {
return hashy.hash(args[0], options.a).then(console.log);
}
if (args.length === 2) {
const password = args[0];
const hash = args[1];
return hashy.verify(password, hash).then(function (success) {
if (success) {
if (hashy.needsRehash(hash, options.a)) {
return "ok but password should be rehashed";
}
return "ok";
}
throw new Error("not ok");
});
}
throw new Error("incorrect number of arguments");
}
exports = module.exports = main;
// ====================================================================
if (!module.parent) {
require("exec-promise")(main);
}