@@ -19,6 +19,7 @@ Integrity](https://w3c.github.io/webappsec/specs/subresourceintegrity/) hashes.
19
19
* [ ` parse ` ] ( #parse )
20
20
* [ ` stringify ` ] ( #stringify )
21
21
* [ ` Integrity#concat ` ] ( #integrity-concat )
22
+ * [ ` Integrity#merge ` ] ( #integrity-merge )
22
23
* [ ` Integrity#toString ` ] ( #integrity-to-string )
23
24
* [ ` Integrity#toJSON ` ] ( #integrity-to-json )
24
25
* [ ` Integrity#match ` ] ( #integrity-match )
@@ -184,6 +185,45 @@ const mobileIntegrity = ssri.fromData(fs.readFileSync('./index.mobile.js'))
184
185
desktopIntegrity .concat (mobileIntegrity)
185
186
```
186
187
188
+ #### <a name =" integrity-merge " ></a > ` > Integrity#merge(otherIntegrity, [opts]) `
189
+
190
+ Safely merges another IntegrityLike or integrity string into an ` Integrity `
191
+ object.
192
+
193
+ If the other integrity value has any algorithms in common with the current
194
+ object, then the hash digests must match, or an error is thrown.
195
+
196
+ Any new hashes will be added to the current object's set.
197
+
198
+ This is useful when an integrity value may be upgraded with a stronger
199
+ algorithm, you wish to prevent accidentally supressing integrity errors by
200
+ overwriting the expected integrity value.
201
+
202
+ ##### Example
203
+
204
+ ``` javascript
205
+ const data = fs .readFileSync (' data.txt' )
206
+
207
+ // integrity.txt contains 'sha1-X1UT+IIv2+UUWvM7ZNjZcNz5XG4='
208
+ // because we were young, and didn't realize sha1 would not last
209
+ const expectedIntegrity = ssri .parse (fs .readFileSync (' integrity.txt' , ' utf8' ))
210
+ const match = ssri .checkData (data, expectedIntegrity, {
211
+ algorithms: [' sha512' , ' sha1' ]
212
+ })
213
+ if (! match) {
214
+ throw new Error (' data corrupted or something!' )
215
+ }
216
+
217
+ // get a stronger algo!
218
+ if (match && match .algorithm !== ' sha512' ) {
219
+ const updatedIntegrity = ssri .fromData (data, { algorithms: [' sha512' ] })
220
+ expectedIntegrity .merge (updatedIntegrity)
221
+ fs .writeFileSync (' integrity.txt' , expectedIntegrity .toString ())
222
+ // file now contains
223
+ // 'sha1-X1UT+IIv2+UUWvM7ZNjZcNz5XG4= sha512-yzd8ELD1piyANiWnmdnpCL5F52f10UfUdEkHywVZeqTt0ymgrxR63Qz0GB7TKPoeeZQmWCaz7T1+9vBnypkYWg=='
224
+ }
225
+ ```
226
+
187
227
#### <a name =" integrity-to-string " ></a > ` > Integrity#toString([opts]) -> String `
188
228
189
229
Returns the string representation of an ` Integrity ` object. All hash entries
0 commit comments