@@ -51,15 +51,12 @@ class IntegrityStream extends MiniPass {
51
51
52
52
if ( ! this . sri ) {
53
53
this . algorithm = null
54
- } else if ( this . sri . isIntegrity ) {
55
- this . goodSri = ! this . sri . isEmpty ( )
56
-
57
- if ( this . goodSri ) {
58
- this . algorithm = this . sri . pickAlgorithm ( this . opts )
59
- }
60
54
} else if ( this . sri . isHash ) {
61
55
this . goodSri = true
62
56
this . algorithm = this . sri . algorithm
57
+ } else {
58
+ this . goodSri = ! this . sri . isEmpty ( )
59
+ this . algorithm = this . sri . pickAlgorithm ( this . opts )
63
60
}
64
61
65
62
this . digests = this . goodSri ? this . sri [ this . algorithm ] : null
@@ -184,8 +181,13 @@ class Hash {
184
181
if ( ! other ) {
185
182
return false
186
183
}
187
- if ( other instanceof Integrity ) {
188
- const algo = other . pickAlgorithm ( opts )
184
+ if ( other . isIntegrity ) {
185
+ const algo = other . pickAlgorithm ( opts , [ this . algorithm ] )
186
+
187
+ if ( ! algo ) {
188
+ return false
189
+ }
190
+
189
191
const foundHash = other [ algo ] . find ( hash => hash . digest === this . digest )
190
192
191
193
if ( foundHash ) {
@@ -323,8 +325,9 @@ class Integrity {
323
325
if ( ! other ) {
324
326
return false
325
327
}
326
- const algo = other . pickAlgorithm ( opts )
328
+ const algo = other . pickAlgorithm ( opts , Object . keys ( this ) )
327
329
return (
330
+ ! ! algo &&
328
331
this [ algo ] &&
329
332
other [ algo ] &&
330
333
this [ algo ] . find ( hash =>
@@ -335,12 +338,22 @@ class Integrity {
335
338
) || false
336
339
}
337
340
338
- pickAlgorithm ( opts ) {
341
+ // Pick the highest priority algorithm present, optionally also limited to a
342
+ // set of hashes found in another integrity. When limiting it may return
343
+ // nothing.
344
+ pickAlgorithm ( opts , hashes ) {
339
345
const pickAlgorithm = opts ?. pickAlgorithm || getPrioritizedHash
340
- const keys = Object . keys ( this )
341
- return keys . reduce ( ( acc , algo ) => {
342
- return pickAlgorithm ( acc , algo ) || acc
346
+ const keys = Object . keys ( this ) . filter ( k => {
347
+ if ( hashes ?. length ) {
348
+ return hashes . includes ( k )
349
+ }
350
+ return true
343
351
} )
352
+ if ( keys . length ) {
353
+ return keys . reduce ( ( acc , algo ) => pickAlgorithm ( acc , algo ) || acc )
354
+ }
355
+ // no intersection between this and hashes,
356
+ return null
344
357
}
345
358
}
346
359
@@ -550,7 +563,7 @@ function createIntegrity (opts) {
550
563
}
551
564
}
552
565
553
- const NODE_HASHES = new Set ( crypto . getHashes ( ) )
566
+ const NODE_HASHES = crypto . getHashes ( )
554
567
555
568
// This is a Best Effort™ at a reasonable priority for hash algos
556
569
const DEFAULT_PRIORITY = [
@@ -560,7 +573,7 @@ const DEFAULT_PRIORITY = [
560
573
'sha3' ,
561
574
'sha3-256' , 'sha3-384' , 'sha3-512' ,
562
575
'sha3_256' , 'sha3_384' , 'sha3_512' ,
563
- ] . filter ( algo => NODE_HASHES . has ( algo ) )
576
+ ] . filter ( algo => NODE_HASHES . includes ( algo ) )
564
577
565
578
function getPrioritizedHash ( algo1 , algo2 ) {
566
579
/* eslint-disable-next-line max-len */
0 commit comments