mirror of https://github.com/wg-easy/wg-easy
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
786 B
37 lines
786 B
import argon2 from 'argon2';
|
|
import { deserialize } from '@phc/format';
|
|
|
|
/**
|
|
* Checks if `password` matches the hash.
|
|
*/
|
|
export function isPasswordValid(
|
|
password: string,
|
|
hash: string
|
|
): Promise<boolean> {
|
|
return argon2.verify(hash, password);
|
|
}
|
|
|
|
/**
|
|
* Hashes a password.
|
|
*/
|
|
export async function hashPassword(password: string): Promise<string> {
|
|
return argon2.hash(password);
|
|
}
|
|
|
|
/**
|
|
* Checks if the password hash is valid.
|
|
* This only checks if the hash is a valid PHC formatted string using argon2.
|
|
*/
|
|
export function isValidPasswordHash(hash: string): boolean {
|
|
try {
|
|
const obj = deserialize(hash);
|
|
|
|
if (obj.id !== 'argon2i' && obj.id !== 'argon2d' && obj.id !== 'argon2id') {
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|