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;
	}
}
