Refresh HiveGrid Cloud reference snapshot
This commit is contained in:
1 parent
5e99e14772
commit
7f88026f79
5955 files changed
+1177325
-188362
No files matched your search
@@ -0,0 +1,59 @@
|
||||
import bcrypt from "bcryptjs";
|
||||
|
||||
const ARGON2_PREFIX_RE = /^\$argon2(?:i|d|id)\$/;
|
||||
const BCRYPT_PREFIX_RE = /^\$2[abxy]\$/;
|
||||
const BCRYPT_MAX_PASSWORD_BYTES = 72;
|
||||
const ARGON2_OPTIONS = {
|
||||
memoryCost: 19_456,
|
||||
timeCost: 2,
|
||||
parallelism: 1,
|
||||
} as const;
|
||||
|
||||
// This hash uses the same Argon2id parameters as the web password hasher. It
|
||||
// is intentionally not tied to a real account and is used only to make the
|
||||
// unknown/passwordless-account paths perform a real password verification.
|
||||
export const DUMMY_PASSWORD_HASH =
|
||||
"$argon2id$v=19$m=19456,t=2,p=1$CGZ841jS/Rerhh41+cREiw$piaCzFMbzHjq946K2t0iN7iMI/389iUpLNm2HROGO4Y";
|
||||
|
||||
let argon2ModulePromise: Promise<typeof import("argon2")> | undefined;
|
||||
|
||||
function loadArgon2() {
|
||||
argon2ModulePromise ??= import("argon2");
|
||||
return argon2ModulePromise;
|
||||
}
|
||||
|
||||
export function isLegacyBcryptHash(passwordHash: string): boolean {
|
||||
return BCRYPT_PREFIX_RE.test(passwordHash);
|
||||
}
|
||||
|
||||
export async function hashCurrentPassword(password: string): Promise<string> {
|
||||
const argon2 = await loadArgon2();
|
||||
return await argon2.hash(password, { ...ARGON2_OPTIONS, type: argon2.argon2id });
|
||||
}
|
||||
|
||||
/**
|
||||
* Verify current Argon2 hashes and legacy bcrypt hashes without allowing an
|
||||
* unknown/corrupt hash format to escape as an authentication error detail.
|
||||
* Argon2 is loaded lazily so non-login API processes do not pay its native
|
||||
* module startup cost.
|
||||
*/
|
||||
export async function verifySupportedPassword(password: string, passwordHash: string): Promise<boolean> {
|
||||
try {
|
||||
if (ARGON2_PREFIX_RE.test(passwordHash)) {
|
||||
const argon2 = await loadArgon2();
|
||||
return await argon2.verify(passwordHash, password);
|
||||
}
|
||||
if (BCRYPT_PREFIX_RE.test(passwordHash)) {
|
||||
// bcrypt silently truncates after 72 bytes. Reject an overlong legacy
|
||||
// credential so a different suffix cannot authenticate as the same key.
|
||||
if (Buffer.byteLength(password, "utf8") > BCRYPT_MAX_PASSWORD_BYTES) {
|
||||
return false;
|
||||
}
|
||||
return await bcrypt.compare(password, passwordHash);
|
||||
}
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
Reference in new issue
Block a user