forked from jbenet/js-senc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·76 lines (65 loc) · 1.43 KB
/
cli.js
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
#!/usr/bin/env node
var senc = require('./index')
var mb = require('multibase')
var P = require('minimist')(process.argv.slice(2))
var usage = `usage: senc -e -k <key> - encrypt stdin with aes
senc -d -k <key> - decrypt stdin with aes
OPTIONS
-h, --help this help text
-d set decrypt mode
-e set encrypt mode
-k <string> key to use (in multibase)
-b <base> the multibase encoding to write key to
--key-gen generate a key
`
function die(err) {
console.error('error: ' + err)
process.exit(-1)
}
if (process.argv.slice(2).length == 0 || P.h || P.help) {
console.log(usage)
process.exit(0)
}
if (P.e && P.d) {
die('use either -e or -d but not both')
}
// select base
if (P.b) {
try {
mb.Encode(P.b, new Buffer())
} catch (e) {
die('unrecognized multibase: ' + P.b)
}
} else {
P.b = 'base58btc'
}
// run key-gen
if (P['key-gen']) {
var o = mb.encode(P.b, senc.RandomKey()).toString()
process.stdout.write(o)
process.stdout.write("\n")
} else {
// select Key
if (!P.k) {
if (P.key) {
P.k = P.key
} else {
die('key required')
}
}
if (!mb.isEncoded(P.k)) {
die('key must be in multibase format')
}
var key = mb.decode(P.k)
var S
if (P.e) {
S = senc.EncryptStream(key)
} else if (P.d) {
S = senc.DecryptStream(key)
} else {
die('must use either -e or -d')
}
process.stdin
.pipe(S)
.pipe(process.stdout)
}