图片上传hash 有bug,修复

This commit is contained in:
2025-12-03 18:05:40 +08:00
parent a97f015205
commit 87824c0021
6 changed files with 27 additions and 10 deletions

View File

@@ -1,16 +1,16 @@
import MD5 from 'crypto-js/md5.js';
import crypto from 'node:crypto';
import fs from 'node:fs';
export const getHash = (file: string) => {
if (!fs.existsSync(file)) return '';
const content = fs.readFileSync(file, 'utf-8');
return MD5(content).toString();
const buffer = fs.readFileSync(file); // 不指定编码,返回 Buffer
return crypto.createHash('md5').update(buffer).digest('hex');
};
export const getBufferHash = (buffer: Buffer) => {
return MD5(buffer.toString()).toString();
return crypto.createHash('md5').update(buffer).digest('hex');
};
export const getStringHash = (str: string) => {
return MD5(str).toString();
}
return crypto.createHash('md5').update(str).digest('hex');
}