-
Notifications
You must be signed in to change notification settings - Fork 205
/
cli.js
229 lines (200 loc) · 7.17 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#!/usr/bin/env node
'use strict'
// CLI TOOL Full of sideffects !
const {
program
} = require('commander')
const StegCloak = require('./stegcloak')
const R = require('ramda')
const chalk = require('chalk')
const clipboardy = require('clipboardy')
var inquirer = require('inquirer')
const ora = require('ora')
const fs = require('fs')
const jsonfile = require('jsonfile');
const { zwcHuffMan } = require('./components/compact')
const { zwcOperations } = require("./components/message");
const { expand } = zwcHuffMan(StegCloak.zwc)
const { detach } = zwcOperations(StegCloak.zwc);
function cliHide(secret, password, cover, crypt, integrity, op) {
const stegcloak = new StegCloak(crypt, integrity)
const spinner = ora(chalk.cyan.bold('Hiding your text'))
spinner.start()
let payload
try {
payload = stegcloak.hide(secret, password, cover, op)
} catch (e) {
console.log('\n')
console.log(chalk.red(e))
process.exit(0)
}
clipboardy.writeSync(payload)
setTimeout(() => {
spinner.stop()
if (op) {
fs.writeFileSync(op, payload)
console.log(chalk.grey(`\n Written to ${op} \n`))
process.exit(0)
}
console.log(chalk.grey('\nCopied to clipboard\n'))
process.exit(0)
}, 300)
};
function createStringQuestion(str, nameIt) {
return { type: 'string', message: str, name: nameIt }
}
function cliReveal(payload, password, op) {
const stegcloak = new StegCloak()
var spinner = ora(chalk.cyan.bold('Decrypting'))
spinner.start()
let secret
try {
secret = stegcloak.reveal(payload, password)
} catch (e) {
console.log('\n')
console.log(chalk.red(e))
process.exit(0)
}
setTimeout(() => {
spinner.stop()
if (op) {
fs.writeFileSync(op, secret)
console.log(chalk.grey(`\n Written to ${op} \n`))
}
console.log('\n')
console.log(chalk.cyan.bold(' Secret: ') + chalk.green.bold(secret))
console.log('\n')
process.exit(0)
}, 300)
};
program
.command('hide [secret] [cover]')
.option('-fc, --fcover <fcover> ', 'Extract cover text from file')
.option('-fs, --fsecret <fsecret> ', 'Extract secret text from file')
.option('-n, --nocrypt', "If you don't need encryption", false)
.option('-i, --integrity', 'If additional security of preventing tampering is needed', false)
.option('-o, --output <output> ', 'Stream the results to an output file')
.option('-c, --config <config>', 'Config file')
.action(async (secret, cover, args) => {
if (args.config) {
jsonfile.readFile(args.config)
.then(obj => {
if (!("secret" in obj && "cover" in obj)) {
console.error(chalk.red("Config Parse error") + " : Missing inputs");
process.exit(0);
}
secret = obj.secret;
cover = obj.cover;
let password = obj.password || process.env["STEGCLOAK_PASSWORD"];
if (!obj.password && process.env["STEGCLOAK_PASSWORD"]) {
console.warn(chalk.yellow("Warning:") + " using password from environment variable");
}
let integrity = obj.integrity || false;
let nocrypt = obj.nocrypt || false;
let output = obj.output || false;
cliHide(secret, password, cover, !nocrypt, integrity, output);
})
.catch(error => console.error(error))
return;
}
const questions = process.env["STEGCLOAK_PASSWORD"] ? (console.warn(chalk.yellow("Warning:") + " using password from environment variable\n"), []) : [{
type: 'password',
message: 'Enter password :',
name: 'password',
mask: true
}];
const qsecret = "What's your secret? :"
const qcover = 'Enter the text you want to hide your secret within? (Minimum 2 words):'
if (args.nocrypt) questions.pop()
if (args.fcover) {
cover = fs.readFileSync(args.fcover, 'utf-8');
}
if (args.fsecret) {
secret = fs.readFileSync(args.fsecret, 'utf-8')
}
if (!secret && !cover) {
questions.push(createStringQuestion(qsecret, 'secret'), createStringQuestion(qcover, 'cover'))
} else if (!secret) {
questions.push(createStringQuestion(qsecret, 'secret'))
} else if (!cover) {
questions.push(createStringQuestion(qcover, 'cover'))
}
let answers = {};
if (questions.length) {
answers = await inquirer.prompt(questions);
}
cliHide(answers.secret || secret, answers.password || process.env["STEGCLOAK_PASSWORD"], cover || answers.cover, !args.nocrypt, args.integrity, args.output)
})
// CLI
program
.command('reveal [message]')
.option('-f, --file <file> ', 'Extract message to be revealed from file')
.option('-cp, --clip', 'Copy message directly from clipboard')
.option('-o, --output <output> ', 'Stream the secret to an output file')
.option('-c, --config <config>', 'Config file')
.action((data, args) => {
if (args.config) {
jsonfile.readFile(args.config)
.then(obj => {
if (!("message" in obj)) {
console.error(chalk.red("Config Parse error") + " : Missing inputs");
process.exit(0);
}
data = obj.message;
if (!obj.password && process.env["STEGCLOAK_PASSWORD"]) {
console.warn(chalk.yellow("Warning:") + " using password from environment variable");
}
let password = obj.password || process.env["STEGCLOAK_PASSWORD"];
let output = obj.output || false;
cliReveal(data, password, output)
})
.catch(error => console.error(error));
return;
}
const questions = [{ type: 'string', message: 'Enter message to decrypt:', name: 'payload' }, {
type: 'password',
message: 'Enter password :',
name: 'password',
mask: true
}];
if (args.file) {
data = fs.readFileSync(args.file, 'utf-8')
console.log(chalk.cyan(`Extracted text from ${args.file} to be decrypted !`))
console.log()
}
if (args.clip || data) {
const mutatedQuestions = questions.slice(1)
data = data || clipboardy.readSync()
const stream = expand(detach(data))
if (stream[0] === StegCloak.zwc[2] || process.env["STEGCLOAK_PASSWORD"]) {
if (process.env["STEGCLOAK_PASSWORD"]) {
console.warn(chalk.yellow("Warning:") + " using password from environment variable");
}
mutatedQuestions.pop()
}
if (mutatedQuestions.length) {
inquirer.prompt(mutatedQuestions).then(answers => {
cliReveal(data, answers.password || process.env["STEGCLOAK_PASSWORD"], args.output)
})
} else {
cliReveal(data, process.env["STEGCLOAK_PASSWORD"] || null, args.output)
}
}
else {
inquirer.prompt([questions[0]]).then(answers => {
const stream = expand(detach(answers.payload))
if (stream[0] === StegCloak.zwc[2]) {
cliReveal(answers.payload, null, args.output)
} else {
if (!process.env["STEGCLOAK_PASSWORD"]) {
inquirer.prompt([questions[1]]).then(ans => {
cliReveal(answers.payload, ans.password, args.output)
})
} else {
cliReveal(answers.payload, process.env["STEGCLOAK_PASSWORD"], args.output)
}
}
})
}
})
program.parse(process.argv)