Initial commit of eigent-main

This commit is contained in:
puzhen 2025-08-12 01:16:39 +02:00
commit 723df5a03e
1144 changed files with 103478 additions and 0 deletions

View file

@ -0,0 +1,55 @@
// src/utils/hashes.tsx
import bcrypt from "bcryptjs";
import { StackAssertionError } from "./errors";
async function sha512(input) {
const bytes = typeof input === "string" ? new TextEncoder().encode(input) : input;
return new Uint8Array(await crypto.subtle.digest("SHA-512", bytes));
}
async function hashPassword(password) {
const passwordBytes = new TextEncoder().encode(password);
if (passwordBytes.length >= 72) {
throw new StackAssertionError(`Password is too long for bcrypt`, { len: passwordBytes.length });
}
const salt = await bcrypt.genSalt(10);
return await bcrypt.hash(password, salt);
}
async function comparePassword(password, hash) {
switch (await getPasswordHashAlgorithm(hash)) {
case "bcrypt": {
return await bcrypt.compare(password, hash);
}
default: {
return false;
}
}
}
async function isPasswordHashValid(hash) {
return !!await getPasswordHashAlgorithm(hash);
}
async function getPasswordHashAlgorithm(hash) {
if (typeof hash !== "string") {
throw new StackAssertionError(`Passed non-string value to getPasswordHashAlgorithm`, { hash });
}
if (hash.match(/^\$2[ayb]\$.{56}$/)) {
try {
if (bcrypt.getRounds(hash) > 16) {
return void 0;
}
await bcrypt.compare("any string", hash);
return "bcrypt";
} catch (e) {
console.warn(`Error while checking bcrypt password hash. Assuming the hash is invalid`, e);
return void 0;
}
} else {
return void 0;
}
}
export {
comparePassword,
getPasswordHashAlgorithm,
hashPassword,
isPasswordHashValid,
sha512
};
//# sourceMappingURL=hashes.js.map