Skip to content
Snippets Groups Projects
Unverified Commit 1b932631 authored by Adam Setch's avatar Adam Setch Committed by GitHub
Browse files

feat: add hash util (#23524)

parent 68e871e8
No related branches found
Tags 38.2.1
No related merge requests found
import { hash, toSha256 } from './hash';
describe('util/hash', () => {
test('should hash data with sha256', () => {
expect(hash('https://example.com/test.txt', 'sha256')).toBe(
'd1dc63218c42abba594fff6450457dc8c4bfdd7c22acf835a50ca0e5d2693020'
);
expect(toSha256('https://example.com/test.txt')).toBe(
'd1dc63218c42abba594fff6450457dc8c4bfdd7c22acf835a50ca0e5d2693020'
);
});
});
import crypto from 'node:crypto';
import type { LiteralUnion } from 'type-fest';
export type AlgorithmName = LiteralUnion<
'sha1' | 'sha224' | 'sha256' | 'sha384' | 'sha512',
string
>;
export function hash(data: string | Buffer, algorithm: AlgorithmName): string {
const hash = crypto.createHash(algorithm);
hash.update(data);
return hash.digest('hex');
}
export function toSha256(input: string): string {
return hash(input, 'sha256');
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment