@@ -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
213225module . 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'
0 commit comments