All files / src integrity.js

100% Statements 26/26
100% Branches 0/0
100% Functions 5/5
100% Lines 26/26

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 671x                     541x   541x   541x 923x     541x         541x   541x 541x 541x   541x         923x   923x 923x   923x         923x   923x 923x 923x 923x 923x   923x         923x   923x   923x     1x  
const { createHash } = require('crypto')
 
/**
 * Extracts the signable content from a policy object.
 *
 * @param {Object} [policy] - MFKDF policy object
 * @returns {Buffer} The extracted data
 * @since 2.0.0
 * @async
 */
async function extract (policy) {
  const hash = createHash('sha256')
 
  hash.update(await extractPolicyCore(policy))
 
  for (const factor of policy.factors) {
    hash.update(await extractFactor(factor))
  }
 
  return hash.digest()
}
 
// Extracts the core signable content from a policy object.
async function extractPolicyCore (policy) {
  const hash = createHash('sha256')
 
  hash.update(policy.$id)
  hash.update(policy.threshold.toString())
  hash.update(policy.salt)
 
  return hash.digest()
}
 
// Extracts the signable content from a factor object.
async function extractFactor (factor) {
  const hash = createHash('sha256')
 
  hash.update(await extractFactorCore(factor))
  hash.update(await extractFactorParams(factor))
 
  return hash.digest()
}
 
// Extracts the core signable content from a factor object.
async function extractFactorCore (factor) {
  const hash = createHash('sha256')
 
  hash.update(factor.id)
  hash.update(factor.type)
  hash.update(factor.pad)
  hash.update(factor.salt)
  hash.update(factor.secret)
 
  return hash.digest()
}
 
// Extracts the signable content from a factor's params object.
async function extractFactorParams (factor) {
  const hash = createHash('sha256')
 
  hash.update(JSON.stringify(factor.params))
 
  return hash.digest()
}
 
module.exports.extract = extract