From f72e6584323988c72d3ad4384f929e6fa3dacd5c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kat=20March=C3=A1n?= Date: Thu, 23 Mar 2017 17:27:07 -0700 Subject: [PATCH] feat(verification): add opts.pickAlgorithm --- index.js | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index e2c7da9..ad10bdc 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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) @@ -186,6 +188,6 @@ function createCheckerStream (sri, opts) { return stream } -function getPrioritizedHashFunction (algo1, algo2) { - // Default implementaion is empty +function getPrioritizedHash (algo1, algo2) { + return algo1 }