All files / src/classes/MFKDFDerivedKey mfdpg.js

100% Statements 8/8
100% Branches 0/0
100% Functions 1/1
100% Lines 8/8

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                    1x 1x                                                                 16x 16x 16x 16x 16x   1x  
/**
 * @file Multi-Factor Derived Key Crypto Functions
 * @copyright Multifactor, Inc. 2022–2025
 *
 * @description
 * Multi-Factor Deterministic Password Generator (MFDPG2)
 *
 * @author Vivek Nair (https://nair.me) <[email protected]>
 */
 
const RandExp = require('randexp')
const rand = require('random-seed')
 
/**
 * Generate a policy-compliant password for a given purpose.
 *
 * @example
 * const setup = await mfkdf.setup.key([
 *   await mfkdf.setup.factors.password('password1', {
 *     id: 'password1'
 *   })
 * ])
 * const password = setup.derivePassword(
 *   'example.com',
 *   'salt',
 *   /[a-zA-Z]{6,10}/
 * )
 *
 * const password2 = setup.derivePassword(
 *   'example.com',
 *   'salt',
 *   /[a-zA-Z]{6,10}/
 * )
 * password.should.equal(password2)
 *
 * @param {string} purpose - Unique purpose value for this password
 * @param {string} salt - Unique salt value for this salt
 * @param {string} regex - Regular expression defining password policy
 * @returns {string} Derived password
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 2.0.0
 * @memberOf MFKDFDerivedKey
 */
function derivePassword (purpose, salt, regex) {
  const passwordKey = this.getSubkey(purpose, salt)
  const dfa = new RandExp(regex)
  const rng = rand.create(passwordKey.toString('hex'))
  dfa.randInt = rng.intBetween
  return dfa.gen()
}
module.exports.derivePassword = derivePassword