import { argon2id, hash as argonHash, verify as argonVerify } from "argon2";

export async function hashString(target: string): Promise<string> {
	const salt = Buffer.from(crypto.getRandomValues(new Uint8Array(16))).toString(
		"hex",
	);
	const hash = await argonHash(target, {
		type: argon2id,
		salt: Buffer.from(salt, "hex"),
		hashLength: 32,
		timeCost: 3,
		memoryCost: 65536,
		parallelism: 1,
	});
	return hash;
}

export async function verifyHash({
	hash,
	target,
}: {
	hash: string;
	target: string;
}): Promise<boolean> {
	try {
		const isValid = await argonVerify(hash, `${target}`);
		return isValid;
	} catch (err) {
		return false;
	}
}
