All files / src crypto.ts

0% Statements 0/14
0% Branches 0/1
0% Functions 0/1
0% Lines 0/14

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                                           
import crypto from 'crypto';
 
export function generateSalt() {
  return crypto.randomBytes(10).toString('hex');
}
 
export async function hashPassword(password: string, salt: string) {
  // concatenate the password and salt
  const passwordWithSalt = password + salt;
 
  // use native crypto to hash the password
  const hashedPassword = await crypto.subtle.digest(
    'SHA-256',
    new TextEncoder().encode(passwordWithSalt)
  );
 
  // convert the hashed password to a hex string
  return Array.from(new Uint8Array(hashedPassword))
    .map((b) => b.toString(16).padStart(2, '0'))
    .join('');
}