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 78 79 80 81 82 | 1x 1x 26x 14x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 1x | /**
* @file MFKDF HOTP Factor Derivation
* @copyright Multifactor, Inc. 2022–2025
*
* @description
* Derive HOTP factor for multi-factor key derivation
*
* @author Vivek Nair (https://nair.me) <vivek@nair.me>
*/
const speakeasy = require('speakeasy')
const { decrypt } = require('../../crypt')
function mod (n, m) {
return ((n % m) + m) % m
}
/**
* Derive an MFKDF HOTP factor
*
* @example
* // setup key with hotp factor
* const setup = await mfkdf.setup.key([
* await mfkdf.setup.factors.hotp({ secret: Buffer.from('abcdefghijklmnopqrst') })
* ])
*
* // derive key with hotp factor
* const derive = await mfkdf.derive.key(setup.policy, {
* hotp: mfkdf.derive.factors.hotp(241063)
* })
*
* setup.key.toString('hex') // -> 01d0…2516
* derive.key.toString('hex') // -> 01d0…2516
*
* @param {number} code - The HOTP code from which to derive an MFKDF factor
* @returns {function(config:Object): Promise<MFKDFFactor>} Async function to generate MFKDF factor information
* @author Vivek Nair (https://nair.me) <vivek@nair.me>
* @since 0.12.0
* @memberof derive.factors
*/
function hotp (code) {
if (!Number.isInteger(code)) throw new TypeError('code must be an integer')
return async (params) => {
const target = mod(params.offset + code, 10 ** params.digits)
const buffer = Buffer.allocUnsafe(4)
buffer.writeUInt32BE(target, 0)
return {
type: 'hotp',
data: buffer,
params: async ({ key }) => {
const pad = Buffer.from(params.pad, 'base64')
const secret = decrypt(pad, key)
const code = parseInt(
speakeasy.hotp({
secret: secret.subarray(0, 20).toString('hex'),
encoding: 'hex',
counter: params.counter + 1,
algorithm: params.hash,
digits: params.digits
})
)
const offset = mod(target - code, 10 ** params.digits)
return {
hash: params.hash,
digits: params.digits,
pad: params.pad,
counter: params.counter + 1,
offset
}
},
output: async () => {
return {}
}
}
}
}
module.exports.hotp = hotp
|