-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhasht.js
57 lines (52 loc) · 1.87 KB
/
hasht.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
const crypto = require('crypto')
const fs = require('fs')
const progress = require('progress-stream')
const logUpdate = require('log-update')
const frames = ['-', '\\', '|', '/']
exports.hash = (fileName, progCallback, finishCallback) => {
const hashMd5 = crypto.createHash('md5')
const hashSha1 = crypto.createHash('sha1')
const hashSha256 = crypto.createHash('sha256')
const hashSha384 = crypto.createHash('sha384')
const hashSha512 = crypto.createHash('sha512')
const stat = fs.statSync(fileName)
const prog = progress({
length: stat.size,
time: 100
})
let i = 0
prog.on('progress', function(progress) {
const frame = frames[i = ++i % frames.length]
const percentage = Math.round(progress.percentage)
const transferred = progress.transferred
const size = progress.length
logUpdate(`${frame} %${percentage} calculated: ${transferred}, size: ${size}`)
if (isFunction(progCallback)) {
progCallback(percentage, transferred, size)
}
})
const input = fs.createReadStream(fileName).pipe(prog)
input.on('data', (chunk) => {
hashMd5.update(chunk)
hashSha1.update(chunk)
hashSha256.update(chunk)
hashSha384.update(chunk)
hashSha512.update(chunk)
})
input.on('end', () => {
logUpdate(`hash (file: ${fileName}, size: ${stat.size})`)
console.log('-----------------------------------------------------------')
let md5 = hashMd5.digest('hex')
let sha1 = hashSha1.digest('hex')
let sha256 = hashSha256.digest('hex')
let sha384 = hashSha384.digest('hex')
let sha512 = hashSha512.digest('hex')
console.log(`MD5: ${md5}\nSHA-1: ${sha1}\nSHA-256: ${sha256}\nSHA-384: ${sha384}\nSHA-512: ${sha512}\n`)
if (isFunction(finishCallback)) {
finishCallback([md5, sha1, sha256, sha384, sha512])
}
})
}
function isFunction(func) {
return func && typeof(func) == 'function'
}