0
Fork 0
mirror of https://github.com/verdaccio/verdaccio.git synced 2025-01-13 22:48:31 -05:00
verdaccio/packages/utils/src/crypto-utils.ts
Behrang Yarahmadi 13310814da
#2606 add prettier plugin sort imports (#2607)
* #2606 add prettier plugin sort imprts

* #2606 update pnpm-lock.yaml

* #2606 update eslint rules

* #2606 fixes website directory formatting

Co-authored-by: Ayush Sharma <ayush.sharma@trivago.com>
2021-10-29 17:33:05 +02:00

22 lines
683 B
TypeScript

import { Hash, createHash, pseudoRandomBytes } from 'crypto';
export const defaultTarballHashAlgorithm = 'sha1';
export function createTarballHash(): Hash {
return createHash(defaultTarballHashAlgorithm);
}
/**
* Express doesn't do ETAGS with requests <= 1024b
* we use md5 here, it works well on 1k+ bytes, but sucks with fewer data
* could improve performance using crc32 after benchmarks.
* @param {Object} data
* @return {String}
*/
export function stringToMD5(data: Buffer | string): string {
return createHash('md5').update(data).digest('hex');
}
export function generateRandomHexString(length = 8): string {
return pseudoRandomBytes(length).toString('hex');
}