@syngrisi/syngrisi
Version:
Syngrisi - Visual Testing Tool
38 lines • 1.02 kB
JavaScript
// src/server/utils/hash.ts
import { createHash, randomUUID } from "crypto";
function hashSync(input) {
return createHash("sha512").update(input).digest("hex");
}
var CROCKFORD_ALPHABET = "0123456789ABCDEFGHJKMNPQRSTVWXYZ";
function toBase32(bytes) {
let result = "";
let bits = 0;
let value = 0;
for (const byte of bytes) {
value = value << 8 | byte;
bits += 8;
while (bits >= 5) {
bits -= 5;
result += CROCKFORD_ALPHABET[value >> bits & 31];
}
}
if (bits > 0) {
result += CROCKFORD_ALPHABET[value << 5 - bits & 31];
}
return result;
}
function generateApiKey() {
const uuid = randomUUID();
const uuidNoDashes = uuid.replace(/-/g, "");
const buf = Buffer.from(uuidNoDashes, "hex");
let base32 = toBase32(buf);
while (base32.length < 28) {
base32 += CROCKFORD_ALPHABET[0];
}
return `${base32.slice(0, 7)}-${base32.slice(7, 14)}-${base32.slice(14, 21)}-${base32.slice(21, 28)}`;
}
export {
generateApiKey,
hashSync
};
//# sourceMappingURL=hash.js.map