Hash 加密是不可逆的(简单的密码有密码库 这种可逆)
密码也不要存原文,都是 hash 后的密文,然后对比结果一致不一致,否则出问题就是密码泄露,hash 反而不会 除非密码太简单,密码库有
import * as crypto from 'crypto';
import * as fs from 'fs';
/**
* 计算文件 hash
*/
export const calculateFileHash = (file: string | Buffer): string => {
const hash = crypto.createHash('md5');
if (typeof file === 'string') {
hash.update(fs.readFileSync(file));
} else {
hash.update(file);
}
return hash.digest('hex');
};
@Post('base64CalHash')
@ApiOperation({ summary: '计算base64文件的hash' })
async base64CalHash (@Body() body: any): Promise<any> {
const { base64File } = body;
const buffer = Buffer.from(base64File, 'base64');
return calculateFileHash(buffer);
}