Files
certd/packages/core/basic/src/utils/util.hash.ts
T

23 lines
666 B
TypeScript
Raw Normal View History

import crypto, { BinaryToTextEncoding } from 'crypto';
2024-09-06 10:19:03 +08:00
function md5(data: string, digest: BinaryToTextEncoding = 'hex') {
return crypto.createHash('md5').update(data).digest(digest);
2024-09-06 10:19:03 +08:00
}
function sha256(data: string, digest: BinaryToTextEncoding = 'hex') {
return crypto.createHash('sha256').update(data).digest(digest);
2024-12-22 14:00:46 +08:00
}
2025-02-24 18:07:08 +08:00
2025-03-14 00:16:34 +08:00
function HmacSha256(data: string, key: string, digest: BinaryToTextEncoding = 'base64') {
return crypto.createHmac('sha256', Buffer.from(key, 'base64')).update(data).digest(digest);
}
2025-02-24 18:07:08 +08:00
function base64(data: string) {
return Buffer.from(data).toString('base64');
}
2024-09-06 10:19:03 +08:00
export const hashUtils = {
md5,
2024-12-22 14:00:46 +08:00
sha256,
2025-02-24 18:07:08 +08:00
base64,
2025-03-14 00:16:34 +08:00
HmacSha256,
2024-09-06 10:19:03 +08:00
};