import { createHash } from "crypto";
export function sha1(data: Buffer | string): string {
if (typeof data === "string") {
data = Buffer.from(data);
}
const sha1sum = createHash("sha1");
sha1sum.update(data);
return sha1sum.digest("hex");
}
export function uuidsha1(data: Buffer | string, knownSha1?: string): string {
const s = knownSha1 ?? sha1(data);
let i = -1;
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, (c) => {
i += 1;
switch (c) {
case "x":
return s[i] ?? "0";
case "y":
return ((parseInt("0x" + s[i], 16) & 0x3) | 0x8).toString(16);
}
return "0";
});
}