1 | const { basename, dirname, join, format, extname } = require('path')
|
2 | const { createHash } = require('crypto')
|
3 |
|
4 | const SHA1 = 'sha1'
|
5 | const DEFAULT_ALGORITHM = SHA1
|
6 |
|
7 | const checksum = (s, algorithm = DEFAULT_ALGORITHM, format = 'hex') =>
|
8 | createHash(algorithm)
|
9 | .update(s)
|
10 | .digest(format)
|
11 |
|
12 | const hashFromFileContent = s => s.trim().split(/\s+/)[0]
|
13 |
|
14 | const checksumFilePath = path =>
|
15 | join(
|
16 | dirname(path),
|
17 | format({
|
18 | name: `.${basename(path)}`,
|
19 | ext: '.sha',
|
20 | }),
|
21 | )
|
22 |
|
23 | module.exports = {
|
24 | DEFAULT_ALGORITHM,
|
25 | SHA1,
|
26 | checksum,
|
27 | checksumFilePath,
|
28 | hashFromFileContent,
|
29 | }
|