Skip to content

Commit

Permalink
feat(match): add integrity.match()
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Feb 6, 2018
1 parent 381c619 commit 3c49cc4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 13 deletions.
29 changes: 16 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,19 @@ class Integrity {
hexDigest () {
return parse(this, {single: true}).hexDigest()
}
match (integrity, opts) {
const other = parse(integrity, opts)
const algo = other.pickAlgorithm(opts)
return (
this[algo] &&
other[algo] &&
this[algo].find(hash =>
other[algo].find(otherhash =>
hash.digest === otherhash.digest
)
)
) || false
}
pickAlgorithm (opts) {
const pickAlgorithm = (opts && opts.pickAlgorithm) || getPrioritizedHash
const keys = Object.keys(this)
Expand Down Expand Up @@ -205,9 +218,8 @@ function checkData (data, sri, opts) {
sri = parse(sri, opts)
if (!Object.keys(sri).length) { return false }
const algorithm = sri.pickAlgorithm(opts)
const digests = sri[algorithm] || []
const digest = crypto.createHash(algorithm).update(data).digest('base64')
return digests.find(hash => hash.digest === digest) || false
return parse({algorithm, digest}).match(sri, opts)
}

module.exports.checkStream = checkStream
Expand Down Expand Up @@ -254,17 +266,8 @@ function integrityStream (opts) {
const newSri = parse(hashes.map((h, i) => {
return `${algorithms[i]}-${h.digest('base64')}${optString}`
}).join(' '), opts)
const match = (
// Integrity verification mode
opts.integrity &&
newSri[algorithm] &&
digests &&
digests.find(hash => {
return newSri[algorithm].find(newhash => {
return hash.digest === newhash.digest
})
})
)
// Integrity verification mode
const match = goodSri && newSri.match(sri, opts)
if (typeof opts.size === 'number' && streamSize !== opts.size) {
const err = new Error(`stream size mismatch when checking ${sri}.\n Wanted: ${opts.size}\n Found: ${streamSize}`)
err.code = 'EBADSIZE'
Expand Down
27 changes: 27 additions & 0 deletions test/integrity.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,33 @@ test('concat()', t => {
t.done()
})

test('match()', t => {
const sri = ssri.parse('sha1-foo sha512-bar')
t.similar(sri.match('sha1-foo'), {
algorithm: 'sha1',
digest: 'foo'
}, 'returns the matching hash')
t.similar(sri.match(ssri.parse('sha1-foo')), {
algorithm: 'sha1',
digest: 'foo'
}, 'accepts other Integrity objects')
t.similar(sri.match(ssri.parse('sha1-foo')), {
algorithm: 'sha1',
digest: 'foo'
}, 'accepts other Hash objects')
t.similar(sri.match({digest: 'foo', algorithm: 'sha1'}), {
algorithm: 'sha1',
digest: 'foo'
}, 'accepts Hash-like objects')
t.similar(sri.match('sha1-bar sha512-bar'), {
algorithm: 'sha512',
digest: 'bar'
}, 'returns the strongest match')
t.notOk(sri.match('sha512-foo'), 'falsy when match fails')
t.notOk(sri.match('sha384-foo'), 'falsy when match fails')
t.done()
})

test('pickAlgorithm()', t => {
const sri = ssri.parse('sha1-foo sha512-bar sha384-baz')
t.equal(sri.pickAlgorithm(), 'sha512', 'picked best algorithm')
Expand Down

0 comments on commit 3c49cc4

Please sign in to comment.