-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
44 lines (39 loc) · 1.05 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
var crypto = require('crypto');
const StreamZip = require('node-stream-zip');
async function zipDigest(filePath) {
var files = await zipManifest(filePath)
var hash = crypto.createHash('sha256');
hash.update(files);
return hash.digest('base64');
}
async function zipManifest(filePath){
let zip;
return new Promise((resolve, reject) => {
zip = new StreamZip({storeEntries: true, file: filePath});
zip.on('ready', () => {
var files = ""
for (const entry of Object.values(zip.entries()).sort()) {
if(!entry.isDirectory){
const data = zip.entryDataSync(entry.name);
var hash = crypto.createHash('sha256');
hash.update(data);
var digest = hash.digest('hex');
files += `${digest} ${entry.name}\n`
}
}
zip.close()
resolve(files)
})
zip.on('error', e => {
try {
if (zip) {
zip.close()
}
} catch (e) {
// ignore
}
reject(e)
})
});
}
module.exports = {zipDigest, zipManifest}