16 lines
513 B
TypeScript
16 lines
513 B
TypeScript
import crypto from 'node:crypto';
|
|
import fs from 'node:fs';
|
|
|
|
export const getHash = (file: string) => {
|
|
if (!fs.existsSync(file)) return '';
|
|
const buffer = fs.readFileSync(file); // 不指定编码,返回 Buffer
|
|
return crypto.createHash('md5').update(buffer).digest('hex');
|
|
};
|
|
|
|
export const getBufferHash = (buffer: Buffer) => {
|
|
return crypto.createHash('md5').update(buffer).digest('hex');
|
|
};
|
|
|
|
export const getStringHash = (str: string) => {
|
|
return crypto.createHash('md5').update(str).digest('hex');
|
|
} |