25 lines
612 B
TypeScript
25 lines
612 B
TypeScript
import crypto from 'crypto';
|
|
// 582af9ef5cdc53d6628f45cb842f874a
|
|
// const hashStr = '{"a":"a"}';
|
|
// const hash = crypto.createHash('md5').update(hashStr).digest('hex');
|
|
// console.log(hash);
|
|
|
|
/**
|
|
* 计算字符串的md5值
|
|
* @param str
|
|
* @returns
|
|
*/
|
|
export const hash = (str: string | Buffer | Object) => {
|
|
let hashStr: string | Buffer;
|
|
if (typeof str === 'object') {
|
|
hashStr = JSON.stringify(str, null, 2);
|
|
} else {
|
|
hashStr = str;
|
|
}
|
|
return crypto.createHash('md5').update(hashStr).digest('hex');
|
|
};
|
|
|
|
export const hashSringify = (str: Object) => {
|
|
return JSON.stringify(str, null, 2);
|
|
};
|