2025-04-14 17:40:23 +08:00
|
|
|
import crypto, { BinaryToTextEncoding } from "crypto";
|
2024-09-06 10:19:03 +08:00
|
|
|
|
2025-04-14 17:40:23 +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
|
|
|
}
|
2025-04-14 17:40:23 +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-04-14 17:40:23 +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) {
|
2025-04-14 17:40:23 +08:00
|
|
|
return Buffer.from(data).toString("base64");
|
2025-02-24 18:07:08 +08:00
|
|
|
}
|
2025-05-16 08:38:38 +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,
|
2025-05-16 08:38:38 +08:00
|
|
|
base64Decode,
|
2025-03-18 00:52:50 +08:00
|
|
|
hmacSha256,
|
2024-09-06 10:19:03 +08:00
|
|
|
};
|