1 | const { createHash } = require('crypto')
|
2 | const { createReadStream } = require('fs')
|
3 |
|
4 | const getStream = require('get-stream')
|
5 | const locatePath = require('locate-path')
|
6 |
|
7 |
|
8 |
|
9 |
|
10 | const getHash = async function(digests, move) {
|
11 |
|
12 | if (move || digests.length === 0) {
|
13 | return
|
14 | }
|
15 |
|
16 | const digestPath = await locatePath(digests)
|
17 | if (digestPath === undefined) {
|
18 | return
|
19 | }
|
20 |
|
21 | const hash = await hashFile(digestPath)
|
22 | return hash
|
23 | }
|
24 |
|
25 |
|
26 | const hashFile = async function(path) {
|
27 | const contentStream = createReadStream(path, 'utf8')
|
28 | const hashStream = createHash(HASH_ALGO, { encoding: 'hex' })
|
29 | contentStream.pipe(hashStream)
|
30 | const hash = await getStream(hashStream)
|
31 | return hash
|
32 | }
|
33 |
|
34 |
|
35 |
|
36 | const HASH_ALGO = 'sha1'
|
37 |
|
38 | module.exports = { getHash }
|