0
0
mirror of https://github.com/renovatebot/renovate.git synced 2024-12-22 05:28:35 +00:00
renovatebot_renovate/lib/util/hash.ts
2023-11-07 15:50:29 +00:00

31 lines
756 B
TypeScript

import crypto from 'crypto';
import { pipeline } from 'stream/promises';
import type { LiteralUnion } from 'type-fest';
export type AlgorithmName = LiteralUnion<
'sha1' | 'sha224' | 'sha256' | 'sha384' | 'sha512',
string
>;
export function hash(
data: string | Buffer,
algorithm: AlgorithmName = 'sha512',
): string {
const hash = crypto.createHash(algorithm);
hash.update(data);
return hash.digest('hex');
}
export function toSha256(input: string): string {
return hash(input, 'sha256');
}
export async function hashStream(
inputStream: NodeJS.ReadableStream,
algorithm: AlgorithmName = 'sha512',
): Promise<string> {
const hash = crypto.createHash(algorithm);
await pipeline(inputStream, hash);
return hash.digest('hex');
}