-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
46 lines (34 loc) · 941 Bytes
/
index.js
File metadata and controls
46 lines (34 loc) · 941 Bytes
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
'use strict'
var crc
try {
crc = require('fast-crc32c')
} catch (e) {
crc = require('./crc32c.js')
}
var crypto = require('crypto')
var { PassThrough } = require('stream')
module.exports = function (cfg) {
cfg = cfg || {}
var crc32c = cfg.crc32c !== false
var md5 = cfg.md5 !== false
var hashes = {}
if (md5) hashes.md5 = crypto.createHash('md5')
var onData = function (chunk, enc, done) {
if (crc32c) hashes.crc32c = crc.calculate(chunk, hashes.crc32c || 0)
if (md5) hashes.md5.update(chunk)
done(null, chunk)
}
var onFlush = function (done) {
if (crc32c) hashes.crc32c = Buffer.from([hashes.crc32c]).toString('base64')
if (md5) hashes.md5 = hashes.md5.digest('base64')
done()
}
var validationStream = new PassThrough({
transform: onData,
flush: onFlush
})
validationStream.test = function (algo, sum) {
return hashes[algo] === sum
}
return validationStream
}