UNPKG

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