All files / src/classes/MFKDFDerivedKey crypto.js

100% Statements 3/3
100% Branches 2/2
100% Functions 1/1
100% Lines 3/3

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                    1x                                         19x   1x  
/**
 * @file Multi-Factor Derived Key Crypto Functions
 * @copyright Multifactor, Inc. 2022–2025
 *
 * @description
 * Cryptographic operations for a multi-factor derived key
 *
 * @author Vivek Nair (https://nair.me) <[email protected]>
 */
 
const { hkdfSync } = require('crypto')
 
/**
 * Create a 256-bit sub-key for specified purpose using HKDF
 *
 * @example
 * // setup multi-factor derived key
 * const key = await mfkdf.setup.key([ await mfkdf.setup.factors.password('password') ])
 *
 * // get sub-key for "eth"
 * const subkey = key.getSubkey('eth')
 * subkey.toString('hex') // -> 97cb…bac5
 *
 * @param {string} [purpose=''] - Unique purpose value for this sub-key
 * @param {string} [salt=''] - Unique salt value for this sub-key
 * @returns {Buffer} Derived sub-key
 * @author Vivek Nair (https://nair.me) <[email protected]>
 * @since 0.10.0
 * @memberOf MFKDFDerivedKey
 */
function getSubkey (purpose = '', salt = '') {
  return Buffer.from(hkdfSync('sha256', this.key, salt, purpose, 32))
}
module.exports.getSubkey = getSubkey