Kirk Lin
2023-10-08 a712a8e5a088da59b32343eb23819ea9040830ef
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import { createHash } from 'node:crypto';
 
function createContentHash(content: string, hashLSize = 12) {
  const hash = createHash('sha256').update(content);
  return hash.digest('hex').slice(0, hashLSize);
}
function strToHex(str: string) {
  const result: string[] = [];
  for (let i = 0; i < str.length; ++i) {
    const hex = str.charCodeAt(i).toString(16);
    result.push(('000' + hex).slice(-4));
  }
  return result.join('').toUpperCase();
}
 
export { createContentHash, strToHex };