@@ -93,6 +93,19 @@ class Integrity {
93
93
hexDigest ( ) {
94
94
return parse ( this , { single : true } ) . hexDigest ( )
95
95
}
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
+ }
96
109
pickAlgorithm ( opts ) {
97
110
const pickAlgorithm = ( opts && opts . pickAlgorithm ) || getPrioritizedHash
98
111
const keys = Object . keys ( this )
@@ -205,9 +218,8 @@ function checkData (data, sri, opts) {
205
218
sri = parse ( sri , opts )
206
219
if ( ! Object . keys ( sri ) . length ) { return false }
207
220
const algorithm = sri . pickAlgorithm ( opts )
208
- const digests = sri [ algorithm ] || [ ]
209
221
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 )
211
223
}
212
224
213
225
module . exports . checkStream = checkStream
@@ -254,17 +266,8 @@ function integrityStream (opts) {
254
266
const newSri = parse ( hashes . map ( ( h , i ) => {
255
267
return `${ algorithms [ i ] } -${ h . digest ( 'base64' ) } ${ optString } `
256
268
} ) . 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 )
268
271
if ( typeof opts . size === 'number' && streamSize !== opts . size ) {
269
272
const err = new Error ( `stream size mismatch when checking ${ sri } .\n Wanted: ${ opts . size } \n Found: ${ streamSize } ` )
270
273
err . code = 'EBADSIZE'
0 commit comments