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

27 lines
753 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
function hmacSha256(data: string, digest: BinaryToTextEncoding = "base64") {
return crypto.createHmac("sha256", data).update(Buffer.alloc(0)).digest(digest);
2025-03-14 00:16:34 +08:00
}
2025-02-24 18:07:08 +08:00
function base64(data: string) {
return Buffer.from(data).toString("base64");
2025-02-24 18:07:08 +08:00
}
function base64Decode(data: string) {
return Buffer.from(data, "base64").toString("utf8");
}
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,
base64Decode,
2025-03-18 00:52:50 +08:00
hmacSha256,
2024-09-06 10:19:03 +08:00
};