-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
66 lines (52 loc) · 1.41 KB
/
index.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
var fs = require('fs')
var PNG = require('png-coder').PNG
var lsb = require('lsb')
var tempfile = require('tempfile')
var compression = require('compressjs').Bzip2
// Encode image data - ignoring the alpha channel
// as it would interfere with the RGB channels
function rgb (n) {
return n + (n / 3) | 0
}
function pdf_to_base64 (pdf_file) {
var bits = compression.compressFile(fs.readFileSync(pdf_file))
return new Buffer(bits).toString('base64')
}
function base64_to_pdf (string, pdf_out) {
var buf = new Buffer(string, 'base64')
fs.writeFileSync(pdf_out, new Buffer(compression.decompressFile(buf)))
}
function encode (pdf_in, png_out, cb) {
var temp_png = tempfile('.png')
require('pdf-to-png')({
input: pdf_in,
output: temp_png,
scale: 4.5
}, stash)
function stash () {
var stegotext = pdf_to_base64(pdf_in)
var png = new PNG()
var src = fs.createReadStream(temp_png)
var dst = fs.createWriteStream(png_out)
dst.on('finish', cb)
png.on('parsed', function (data) {
lsb.encode(data, stegotext, rgb)
png.pack().pipe(dst)
})
src.pipe(png)
}
}
function decode (png_in, pdf_out, cb) {
var png = new PNG()
var src = fs.createReadStream(png_in)
png.on('parsed', function (data) {
var stegotext = lsb.decode(data, rgb)
base64_to_pdf(stegotext, pdf_out)
cb()
})
src.pipe(png)
}
module.exports = {
encode: encode,
decode: decode
}