-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsecretz.js
60 lines (42 loc) · 1.72 KB
/
secretz.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
/**
An encrypted, gzipped tar file will be piped in on process.stdin. To beat this
challenge, for each file in the tar input, print a hex-encoded md5 hash of the
file contents followed by a single space followed by the filename, then a
newline.
You will receive the cipher name as process.argv[2] and the cipher passphrase as
process.argv[3]. You can pass these arguments directly through to
`crypto.createDecipher()`.
The built-in zlib library you get when you `require('zlib')` has a
`zlib.createGunzip()` that returns a stream for gunzipping.
The `tar` module from npm has a `tar.Parse()` function that emits `'entry'`
events for each file in the tar input. Each `entry` object is a readable stream
of the file contents from the archive and:
`entry.type` is the kind of file ('File', 'Directory', etc)
`entry.path` is the file path
Using the tar module looks like:
var tar = require('tar');
var parser = tar.Parse();
parser.on('entry', function (e) {
console.dir(e);
});
var fs = require('fs');
fs.createReadStream('file.tar').pipe(parser);
Use `crypto.createHash('md5', { encoding: 'hex' })` to generate a stream that
outputs a hex md5 hash for the content written to it.
*/
var zlib = require('zlib');
var tar = require('tar');
var crypto = require('crypto');
var through = require('through');
var decrypt = crypto.createDecipher(process.argv[2], process.argv[3]);
var parser = tar.Parse();
parser.on('entry', function(e) {
if (e.type == 'File') {
var hash = crypto.createHash('md5', {encoding: 'hex'});
e.pipe(hash).pipe(through(null, end)).pipe(process.stdout);
function end() {
this.queue(" " + e.path + "\n");
}
}
});
process.stdin.pipe(decrypt).pipe(zlib.createGunzip()).pipe(parser);