-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathindex.js
43 lines (33 loc) · 883 Bytes
/
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
const crypto = require('crypto')
const fs = require('fs')
const BUFFER_SIZE = 8192
function md5FileSync (path) {
const fd = fs.openSync(path, 'r')
const hash = crypto.createHash('md5')
const buffer = Buffer.alloc(BUFFER_SIZE)
try {
let bytesRead
do {
bytesRead = fs.readSync(fd, buffer, 0, BUFFER_SIZE)
hash.update(buffer.slice(0, bytesRead))
} while (bytesRead === BUFFER_SIZE)
} finally {
fs.closeSync(fd)
}
return hash.digest('hex')
}
function md5File (path) {
return new Promise((resolve, reject) => {
const output = crypto.createHash('md5')
const input = fs.createReadStream(path)
input.on('error', (err) => {
reject(err)
})
output.once('readable', () => {
resolve(output.read().toString('hex'))
})
input.pipe(output)
})
}
module.exports = md5File
module.exports.sync = md5FileSync