All files / src/utils encryption.ts

100% Statements 17/17
100% Branches 2/2
100% Functions 3/3
100% Lines 14/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 233x 3x     3x 9x 9x 9x     3x 2x 1x 1x 1x     1x     3x 2x  
import * as bcrypt from 'bcryptjs';
import * as crypto from 'crypto';
import { PasswordType } from '../types/password-type';
 
export const bcryptPassword = async (password: string): Promise<string> => {
  const salt = await bcrypt.genSalt(10);
  const hash = await bcrypt.hash(password, salt);
  return hash;
};
 
export const hashPassword = (password: PasswordType, algorithm: string) => {
  if (typeof password === 'string') {
    const hash = crypto.createHash(algorithm);
    hash.update(password);
    return hash.digest('hex');
  }
 
  return password.digest;
};
 
export const verifyPassword = async (password: string, hash: string): Promise<boolean> =>
  bcrypt.compare(password, hash);