All files / crypto PasswordEncoder.ts

100% Statements 11/11
100% Branches 2/2
100% Functions 4/4
100% Lines 9/9

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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 531x 1x             1x               4x                     5x 5x 5x                       2x                 4x    
import crypto from 'crypto';
import { Service } from '../di/di';
 
/**
 * @class PasswordEncoder
 * @description A service for hashing and comparing passwords.
 */
@Service()
export class PasswordEncoder {
  private readonly pepper: string;
 
  /**
   * @constructor
   * @description Initializes the pepper from environment variables or a default value.
   */
  constructor() {
    this.pepper = process.env.PASSWORD_PEPPER || 'default-pepper';
  }
 
  /**
   * @method hash
   * @description Hashes a password with a salt and pepper.
   * @param {string} password - The password to hash.
   * @param {string} salt - The salt to use for hashing.
   * @returns {string} The hashed password.
   */
  hash(password: string, salt: string): string {
    const hash = crypto.createHmac('sha512', salt);
    hash.update(password + this.pepper);
    return hash.digest('hex');
  }
 
  /**
   * @method compare
   * @description Compares a password to a hashed password.
   * @param {string} password - The password to compare.
   * @param {string} salt - The salt used to hash the password.
   * @param {string} hashedPassword - The hashed password to compare against.
   * @returns {boolean} True if the password matches the hash, false otherwise.
   */
  compare(password: string, salt: string, hashedPassword: string): boolean {
    return this.hash(password, salt) === hashedPassword;
  }
 
  /**
   * @method generateSalt
   * @description Generates a random salt.
   * @returns {string} The generated salt.
   */
  generateSalt(): string {
    return crypto.randomBytes(16).toString('hex');
  }
}