stashing code
This commit is contained in:
33
apps/frontend/src/lib/domains/auth/domain/hash.ts
Normal file
33
apps/frontend/src/lib/domains/auth/domain/hash.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import argon2 from "argon2";
|
||||
|
||||
export async function hashPassword(password: string): Promise<string> {
|
||||
const salt = Buffer.from(crypto.getRandomValues(new Uint8Array(16))).toString(
|
||||
"hex",
|
||||
);
|
||||
|
||||
const hash = await argon2.hash(password, {
|
||||
type: argon2.argon2id,
|
||||
salt: Buffer.from(salt, "hex"),
|
||||
hashLength: 32,
|
||||
timeCost: 3,
|
||||
memoryCost: 65536,
|
||||
parallelism: 1,
|
||||
});
|
||||
|
||||
return hash;
|
||||
}
|
||||
|
||||
export async function verifyPassword({
|
||||
hash,
|
||||
password,
|
||||
}: {
|
||||
hash: string;
|
||||
password: string;
|
||||
}): Promise<boolean> {
|
||||
try {
|
||||
const isValid = await argon2.verify(hash, `${password}`);
|
||||
return isValid;
|
||||
} catch (err) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user