1 | const {
|
2 | basename,
|
3 | dirname,
|
4 | format: formatPath,
|
5 | join: joinPath,
|
6 | } = require('path')
|
7 |
|
8 | const { createHash } = require('crypto')
|
9 | const { algorithm: default_algorithm, fileExtension } = require('./defaults')
|
10 |
|
11 | const getChecksum = (s, algorithm = default_algorithm, format = 'hex') =>
|
12 | createHash(algorithm)
|
13 | .update(s)
|
14 | .digest(format)
|
15 |
|
16 | const hashFromFileContent = s => s.trim().split(/\s+/)[0]
|
17 |
|
18 | const getChecksumFilePath = path =>
|
19 | joinPath(
|
20 | dirname(path),
|
21 | formatPath({ name: `.${basename(path)}`, ext: fileExtension }),
|
22 | )
|
23 |
|
24 | module.exports = { getChecksum, getChecksumFilePath, hashFromFileContent }
|