All files / packages/utils/src/server crypto.ts

37.5% Statements 3/8
100% Branches 0/0
0% Functions 0/2
37.5% Lines 3/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29                    6x   6x               6x                
import * as crypto from "node:crypto";
import { promisify } from "node:util";
 
import type { UnauthorizedContext } from "~web/handlers/context";
 
type PasswordData = {
	hash: string;
	salt: string;
};
 
const promisifiedPbkdf2 = promisify(crypto.pbkdf2);
 
export const getHash = async (
	password: string,
	salt: string,
): Promise<string> => {
	const buffer = await promisifiedPbkdf2(password, salt, 1000, 64, "sha512");
	return buffer.toString("hex");
};
 
export const generatePasswordData = async (
	ctx: Pick<UnauthorizedContext, "getSalt">,
	password: string,
): Promise<PasswordData> => {
	const salt = ctx.getSalt();
	const hash = await getHash(password, salt);
	return { salt, hash };
};