All files / node-user-accounts-boilerplate-nahid/crypt Crypt.js

63.64% Statements 7/11
66.67% Branches 4/6
50% Functions 2/4
63.64% Lines 7/11

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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77                                9x   2x     7x                                               7x       7x         7x                                         5x  
"use strict";
 
/**
 * Abstract class for taking care of password hashing and verification.
 */
class Crypt
{
 
  /**
   * Hash a password.
   * 
   * @param {string} password Password to hash
   * @return {string} Promise should return hash as string or error
   */
  async hash(password)
  {
    if (!password)
    {
      throw new Error('Empty password');
    }
 
    return await this.hashImplementation(password);
  }
 
  /**
   * Implementation of hash() minus checks.
   * 
   * @param {string} password_ Password to hash
   * @return {string} Promise should return hash as string or error
   * @abstract
   */
  async hashImplementation(password_)
  {
    throw new Error('TODO: Abstract method called');
  }
 
  /**
   * Verify a password against previously hashed password
   * 
   * @param {String} password Password to compare
   * @param {String} hash Previously hashed password
   * @return {boolean} Promise should return boolean result.
   */
  async verify(password, hash)
  {
    Iif (!password)
    {
      throw new Error('Empty password');
    }
    Iif (!hash)
    {
      throw new Error('Empty hash');
    }
 
    return await this.verifyImplementation(password, hash);
  }
 
  /**
   * Implementation of verify() minus checks.
   * 
   * Default implementation hashes new password and does string compare.
   * This'll work for weaker hashes like md5 but won't work for stronger ones
   * like scrypt.
   * 
   * @param {String} password Password to compare
   * @param {String} hash Previously hashed password
   * @return {boolean} Promise should return boolean result.
   */
  async verifyImplementation(password, hash)
  {
    return hash === await this.hash(password);
  }
 
}
 
module.exports = Crypt;