UNPKG

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