Skip to content

Commit

Permalink
feat(hexDigest): added hexDigest method to Integrity objects too
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Apr 3, 2017
1 parent fd23e1b commit 85208ba
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Integrity](https://w3c.github.io/webappsec/specs/subresourceintegrity/) hashes.
* [`Integrity#concat`](#integrity-concat)
* [`Integrity#toString`](#integrity-to-string)
* [`Integrity#pickAlgorithm`](#integrity-pick-algorithm)
* [`IntegrityMetadata#hexDigest`](#integrity-metadata-hex-digest)
* [`Integrity#hexDigest`](#integrity-hex-digest)
* Integrity Generation
* [`fromHex`](#from-hex)
* [`fromData`](#from-data)
Expand Down Expand Up @@ -217,15 +217,16 @@ may intentionally deprioritize algorithms with known vulnerabilities.
ssri.parse('sha1-WEakDigEST sha512-yzd8ELD1piyANiWnmdnpCL5F52f10UfUdEkHywVZeqTt0ymgrxR63Qz0GB7TKPoeeZQmWCaz7T1').pickAlgorithm() // sha512
```

#### <a name="integrity-metadata-hex-digest"></a> `> IntegrityMetadata#hexDigest() -> String`
#### <a name="integrity-hex-digest"></a> `> Integrity#hexDigest() -> String`

Called on an *individual* `IntegrityMetadata` object, will convert its `digest`
to a hex representation of the base64 data.
`Integrity` is assumed to be either a single-hash `Integrity` instance, or an
`IntegrityMetadata` instance. Returns its `digest`, converted to a hex
representation of the base64 data.

##### Example

```javascript
ssri.parse('sha1-deadbeef', {single: true}).hexDigest() // '75e69d6de79f'
ssri.parse('sha1-deadbeef').hexDigest() // '75e69d6de79f'
```

#### <a name="from-hex"></a> `> ssri.fromHex(hexDigest, algorithm, [opts]) -> Integrity`
Expand Down
3 changes: 3 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ class Integrity {
: stringify(integrity, opts)
return parse(`${this.toString(opts)} ${other}`, opts)
}
hexDigest () {
return parse(this, {single: true}).hexDigest()
}
pickAlgorithm (opts) {
const pickAlgorithm = (opts && opts.pickAlgorithm) || getPrioritizedHash
return Object.keys(this).reduce((acc, algo) => {
Expand Down
11 changes: 8 additions & 3 deletions test/integrity.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,15 @@ test('pickAlgorithm()', t => {
t.done()
})

test('IntegrityMetadata.hexDigest()', t => {
const sri = ssri.parse('sha512-bar', {single: true})
test('hexDigest()', t => {
t.equal(
sri.hexDigest(),
ssri.parse('sha512-foo').hexDigest(),
(
Buffer.from ? Buffer.from('foo', 'base64') : new Buffer('foo', 'base64')
).toString('hex'),
'returned hex version of base64 digest')
t.equal(
ssri.parse('sha512-bar', {single: true}).hexDigest(),
(
Buffer.from ? Buffer.from('bar', 'base64') : new Buffer('bar', 'base64')
).toString('hex'),
Expand Down

0 comments on commit 85208ba

Please sign in to comment.