Skip to content

Commit

Permalink
feat(verification): add opts.pickAlgorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Mar 24, 2017
1 parent cae12c7 commit f72e658
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,13 @@ function checkData (data, sri, opts) {
fullSri[sri.algorithm] = [sri]
sri = fullSri
}
const pickAlgorithm = opts.pickAlgorithm || getPrioritizedHash
const algorithm = Object.keys(sri).reduce((acc, algo) => {
return getPrioritizedHashFunction(acc, algo) || acc
return pickAlgorithm(acc, algo) || acc
})
const digests = sri[algorithm].map(m => m.digest)
const digest = crypto.createHash(algorithm).update(data).digest('base64')
return digests.some(d => d === digest)
return digests.some(d => d === digest) && algorithm
}

module.exports.checkStream = checkStream
Expand Down Expand Up @@ -158,8 +159,9 @@ function createCheckerStream (sri, opts) {
fullSri[sri.algorithm] = [sri]
sri = fullSri
}
const pickAlgorithm = opts.pickAlgorithm || getPrioritizedHash
const algorithm = Object.keys(sri).reduce((acc, algo) => {
return getPrioritizedHashFunction(acc, algo) || acc
return pickAlgorithm(acc, algo) || acc
})
const digests = sri[algorithm].map(m => m.digest)
const hash = crypto.createHash(algorithm)
Expand All @@ -186,6 +188,6 @@ function createCheckerStream (sri, opts) {
return stream
}

function getPrioritizedHashFunction (algo1, algo2) {
// Default implementaion is empty
function getPrioritizedHash (algo1, algo2) {
return algo1
}

0 comments on commit f72e658

Please sign in to comment.