🌳
pt0/serverF/calcHashF.mts
1import { createHash } from 'crypto'
2import { compact, isArray } from 'lodash-es'
7type HashAlgorithm = 'sha1' | 'sha256'
8const hashAlgorithms = ['sha1', 'sha256'] as const
10export const calcReqHash = (str: string | string[], alg?: HashAlgorithm): string => {
11 if (isArray(str)) {
12 const arr = str
13 const arrLen = arr.length
14 const arrCompactLen = compact(arr).length
15 throwIf(() => arrLen != arrCompactLen, {arr, arrLen, arrCompactLen})
16 str = str.join('')
17 }
19 return calcHash(str, alg)
22export const calcHash = (str: string, alg: HashAlgorithm = 'sha1'): string => {
23 assertIncludes(hashAlgorithms, alg, {alg})
24 const shasum = createHash(alg)
25 shasum.update(str)
26 return shasum.digest('hex')