aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/utils/hash.mjs
blob: 0dc3114e8cb500c7aac15e76db5850079b03e797 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import crypto from 'node:crypto';
import fs from 'node:fs/promises';

/**
 * Generate hash from array of strings
 * @param {string[]} input
 * @param {string} algorithm
 * @returns {string}
 */
export function hashFromArray(input, algorithm = 'sha512') {
  const hash = crypto.createHash(algorithm);
  for (const str of input) {
    hash.update(str);
  }

  return hash.digest('hex');
}

/**
 * Generate hash from array of strings
 * @param {string} file
 * @param {string} algorithm
 * @returns {Promise<string>}
 */
export async function hashFile(file, algorithm = 'sha512') {
  const data = await fs.readFile(file);
  const hash = crypto.createHash(algorithm);
  hash.update(data);
  return hash.digest('hex');
}