Files
Cato d528260d49 feat(auth): CJS ./auth/password and one new-password field on reset (0.9.1) (#37)
* feat(auth): CommonJS build of ./auth/password; reset form asks for the new password once, in the same field as sign-up

* feat(auth): PasswordField as its own entry for products with their own pages

* fix(styles): the eye's room applies to any input in the box

* feat(auth): ./auth/hash, bcrypt over SHA-256 with verify-and-rehash of old hashes

* feat(auth): PasswordField accepts name and placeholder
2026-10-28 03:54:02 +01:00

23 lines
1.0 KiB
JavaScript

import { createHash } from "node:crypto";
export const PASSWORD_HASH_PREFIX = "$bs1$";
export const BCRYPT_ROUNDS = 12;
/** 44 characters whatever the password's length: always under bcrypt's 72 bytes. */
const digest = (plain) => createHash("sha256").update(plain, "utf8").digest("base64");
export function createPasswordHasher(bcrypt, rounds = BCRYPT_ROUNDS) {
return {
async hash(plain) {
return PASSWORD_HASH_PREFIX + (await bcrypt.hash(digest(plain), rounds));
},
async verify(plain, stored) {
if (typeof plain !== "string" || !stored)
return { valid: false, rehash: false };
if (stored.startsWith(PASSWORD_HASH_PREFIX)) {
const valid = await bcrypt.compare(digest(plain), stored.slice(PASSWORD_HASH_PREFIX.length));
return { valid, rehash: false };
}
const valid = await bcrypt.compare(plain, stored);
return { valid, rehash: valid };
},
};
}
//# sourceMappingURL=hash.js.map