mirror of
https://github.com/certd/certd.git
synced 2026-04-16 23:00:55 +08:00
27 lines
753 B
TypeScript
27 lines
753 B
TypeScript
import crypto, { BinaryToTextEncoding } from "crypto";
|
|
|
|
function md5(data: string, digest: BinaryToTextEncoding = "hex") {
|
|
return crypto.createHash("md5").update(data).digest(digest);
|
|
}
|
|
function sha256(data: string, digest: BinaryToTextEncoding = "hex") {
|
|
return crypto.createHash("sha256").update(data).digest(digest);
|
|
}
|
|
|
|
function hmacSha256(data: string, digest: BinaryToTextEncoding = "base64") {
|
|
return crypto.createHmac("sha256", data).update(Buffer.alloc(0)).digest(digest);
|
|
}
|
|
|
|
function base64(data: string) {
|
|
return Buffer.from(data).toString("base64");
|
|
}
|
|
function base64Decode(data: string) {
|
|
return Buffer.from(data, "base64").toString("utf8");
|
|
}
|
|
export const hashUtils = {
|
|
md5,
|
|
sha256,
|
|
base64,
|
|
base64Decode,
|
|
hmacSha256,
|
|
};
|