All files / src/adapters pbkdf2.ts

91.66% Statements 11/12
80% Branches 4/5
100% Functions 4/4
100% Lines 11/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 421x 1x               1x 1x 1x 1x 1x                       8x 8x             8x   8x            
import { BaseHashingAdapter } from "../adapter";
import { pbkdf2 } from "crypto";
/**
 *
 *
 * @export
 * @class PBKDF2HashingAdapter
 * @extends {BaseHashingAdapter}
 */
export class PBKDF2HashingAdapter extends BaseHashingAdapter {
  readonly name = "pbkdf2";
  readonly iterations: number = 100_000;
  readonly keyLength: number = 512;
  readonly digest: string = "sha512";
 
  /**
   *
   *
   * @protected
   * @param {Buffer} password
   * @param {(Buffer | undefined)} salt
   * @return {Promise<Buffer>}  {Promise<Buffer>}
   * @memberof PBKDF2HashingAdapter
   */
  protected _hash(password: Buffer, salt: Buffer | undefined): Promise<Buffer> {
    return new Promise((resolve, reject) => {
      pbkdf2(
        password,
        salt ?? Buffer.alloc(0),
        this.iterations,
        this.keyLength,
        this.digest,
        (error, key) => {
          Iif (error) return reject(error);
 
          resolve(key);
        }
      );
    });
  }
}