-
Notifications
You must be signed in to change notification settings - Fork 0
/
stegger.js
226 lines (158 loc) · 7.04 KB
/
stegger.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
var http = require('http')
, fs = require('fs')
, crypto = require('crypto')
, moment = require('moment')
, shasum = crypto.createHash('sha1')
, Sequence = require('futures').sequence
, Promise = require('future')
, util = require("util")
, exec = require("child_process").exec;
var stegger = (function() {
return {
/* ----------------------------------------------------------------*/
/**
* @brief
*
* @param arg {
* inFile -- the file to read in
* msg -- a message to encode
* key -- a key to encode string
* method -- the encryption method used to encode
* passphrase -- a passphrase used by outguess
* }
*
* @return
*/
/* ------------------------------------------------------------------*/
encrypt : function(arg) {
var promise = Promise();
new Sequence()
.then(function(next) {
var cipher = crypto.createCipher(arg.method, arg.key);
var crypted = cipher.update(arg.msg,'utf8','hex') +
cipher.final('hex');
next(crypted);
})
.then(function(next, crypted) {
var filename = moment().valueOf().toString(36).toString('utf8') + '.txt';
fs.writeFile(filename, crypted, function(err, data) {
if (err) {
console.err('There was an error');
}
next(filename);
});
})
.then(function(next, filename) {
var outFilename = moment().valueOf().toString(36).toString('utf8') + '.jpg';
next(filename, outFilename);
})
.then(function(next, filename, outFilename) {
// ensure .jpg extension, else won't work
if (arg.inFile.slice(-3) !== 'jpg' &&
arg.inFile.slice(-4) !== 'jpeg') {
console.log(arg.inFile);
var newName = arg.inFile + '.jpg';
fs.rename(arg.inFile, newName, function(err) {
if (err) {
throw err;
}
arg.inFile = newName;
next(filename, outFilename);
});
} else {
next(filename, outFilename);
}
})
.then(function(next, filename, outFilename) {
var cmd = "outguess -k \'" + arg.passphrase + "\' -d " + filename + " " + arg.inFile + " " + outFilename;
exec(cmd , function(err, stdout, stderr) {
next(filename, outFilename);
});
})
.then(function(next, filename, outFilename) {
fs.unlink(filename, function() {
promise.fulfill(undefined, outFilename);
next();
});
});
return promise;
},
/* ----------------------------------------------------------------*/
/**
* @brief
*
* @param arg {
* inFile -- the file to read in (an image)
* outFile -- the file to write out the message
* key -- a key to encode string
* method -- the encryption method used to encode
* passphrase -- a passphrase used by outguess
* }
*
* @return a promise, with a callback signature (err, message)
*/
/* ------------------------------------------------------------------*/
decrypt : function(arg) {
var promise = Promise();
new Sequence()
.then(function(next) {
// ensure .jpg extension, else won't work
if (arg.inFile.slice(-3) !== 'jpg' &&
arg.inFile.slice(-4) !== 'jpeg') {
console.log(arg.inFile);
var newName = arg.inFile + '.jpg';
fs.rename(arg.inFile, newName, function(err) {
if (err) {
throw err;
}
arg.inFile = newName;
next();
});
} else {
next();
}
})
.then(function(next) {
var filename = moment().valueOf().toString(36).toString('utf8') + '.txt';
var cmd = "outguess -k \'" + arg.passphrase + "\' -r " + arg.inFile + " " + filename;
exec(cmd , function(err, stdout, stderr) {
next(filename);
});
})
.then(function(next, filename) {
fs.readFile(filename, function(err, data) {
next(data, filename);
});
})
.then(function(next, data, filename) {
try {
//if (data.isString('utf8')) {
//console.log('is string!');
//} else {
//console.log('not string!');
//}
var decipher = crypto.createDecipher(arg.method, arg.key);
var dec = decipher.update(data.toString('utf8'), 'hex', 'utf8');
dec += decipher.final('utf8');
if (dec === '') {
promise.fulfill(true, dec);
} else {
promise.fulfill(undefined, dec);
}
next(filename);
/* Invalid cipher */
} catch (ex) {
promise.fulfill(true, dec);
next(filename);
}
})
.then(function(next, filename) {
fs.unlink(filename, function() {
next();
});
});
return promise;
}
}
})();
module.exports = stegger;