mirror of
https://github.com/eigent-ai/eigent.git
synced 2026-04-29 04:00:09 +00:00
Initial commit of eigent-main
This commit is contained in:
commit
723df5a03e
1144 changed files with 103478 additions and 0 deletions
55
package/@stackframe/stack-shared/dist/esm/utils/hashes.js
vendored
Normal file
55
package/@stackframe/stack-shared/dist/esm/utils/hashes.js
vendored
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue