Skip to content

Commit 3c49cc4

Browse files
committed
feat(match): add integrity.match()
1 parent 381c619 commit 3c49cc4

File tree

2 files changed

+43
-13
lines changed

2 files changed

+43
-13
lines changed

index.js

+16-13
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,19 @@ class Integrity {
9393
hexDigest () {
9494
return parse(this, {single: true}).hexDigest()
9595
}
96+
match (integrity, opts) {
97+
const other = parse(integrity, opts)
98+
const algo = other.pickAlgorithm(opts)
99+
return (
100+
this[algo] &&
101+
other[algo] &&
102+
this[algo].find(hash =>
103+
other[algo].find(otherhash =>
104+
hash.digest === otherhash.digest
105+
)
106+
)
107+
) || false
108+
}
96109
pickAlgorithm (opts) {
97110
const pickAlgorithm = (opts && opts.pickAlgorithm) || getPrioritizedHash
98111
const keys = Object.keys(this)
@@ -205,9 +218,8 @@ function checkData (data, sri, opts) {
205218
sri = parse(sri, opts)
206219
if (!Object.keys(sri).length) { return false }
207220
const algorithm = sri.pickAlgorithm(opts)
208-
const digests = sri[algorithm] || []
209221
const digest = crypto.createHash(algorithm).update(data).digest('base64')
210-
return digests.find(hash => hash.digest === digest) || false
222+
return parse({algorithm, digest}).match(sri, opts)
211223
}
212224

213225
module.exports.checkStream = checkStream
@@ -254,17 +266,8 @@ function integrityStream (opts) {
254266
const newSri = parse(hashes.map((h, i) => {
255267
return `${algorithms[i]}-${h.digest('base64')}${optString}`
256268
}).join(' '), opts)
257-
const match = (
258-
// Integrity verification mode
259-
opts.integrity &&
260-
newSri[algorithm] &&
261-
digests &&
262-
digests.find(hash => {
263-
return newSri[algorithm].find(newhash => {
264-
return hash.digest === newhash.digest
265-
})
266-
})
267-
)
269+
// Integrity verification mode
270+
const match = goodSri && newSri.match(sri, opts)
268271
if (typeof opts.size === 'number' && streamSize !== opts.size) {
269272
const err = new Error(`stream size mismatch when checking ${sri}.\n Wanted: ${opts.size}\n Found: ${streamSize}`)
270273
err.code = 'EBADSIZE'

test/integrity.js

+27
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,33 @@ test('concat()', t => {
7878
t.done()
7979
})
8080

81+
test('match()', t => {
82+
const sri = ssri.parse('sha1-foo sha512-bar')
83+
t.similar(sri.match('sha1-foo'), {
84+
algorithm: 'sha1',
85+
digest: 'foo'
86+
}, 'returns the matching hash')
87+
t.similar(sri.match(ssri.parse('sha1-foo')), {
88+
algorithm: 'sha1',
89+
digest: 'foo'
90+
}, 'accepts other Integrity objects')
91+
t.similar(sri.match(ssri.parse('sha1-foo')), {
92+
algorithm: 'sha1',
93+
digest: 'foo'
94+
}, 'accepts other Hash objects')
95+
t.similar(sri.match({digest: 'foo', algorithm: 'sha1'}), {
96+
algorithm: 'sha1',
97+
digest: 'foo'
98+
}, 'accepts Hash-like objects')
99+
t.similar(sri.match('sha1-bar sha512-bar'), {
100+
algorithm: 'sha512',
101+
digest: 'bar'
102+
}, 'returns the strongest match')
103+
t.notOk(sri.match('sha512-foo'), 'falsy when match fails')
104+
t.notOk(sri.match('sha384-foo'), 'falsy when match fails')
105+
t.done()
106+
})
107+
81108
test('pickAlgorithm()', t => {
82109
const sri = ssri.parse('sha1-foo sha512-bar sha384-baz')
83110
t.equal(sri.pickAlgorithm(), 'sha512', 'picked best algorithm')

0 commit comments

Comments
 (0)