mirror of
https://github.com/bitbaum/accountkit.git
synced 2026-12-04 21:49:05 +01:00
* 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
23 lines
1.0 KiB
JavaScript
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
|