Skip to content

Commit

Permalink
feat(hex): utility function for getting hex version of digest
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Apr 2, 2017
1 parent c8ddf48 commit a9f021c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +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 Generation
* [`fromData`](#from-data)
* [`fromStream`](#from-stream)
Expand Down Expand Up @@ -215,6 +216,17 @@ 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`

Called on an *individual* `IntegrityMetadata` object, will convert its `digest`
to a hex representation of the base64 data.

##### Example

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

#### <a name="from-data"></a> `> ssri.fromData(data, [opts]) -> Integrity`

Creates an `Integrity` object from either string or `Buffer` data, calculating
Expand Down
7 changes: 7 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ class IntegrityMetadata {
const rawOpts = match[3]
this.options = rawOpts ? rawOpts.slice(1).split('?') : []
}
hexDigest () {
return this.digest && (
Buffer.from
? Buffer.from(this.digest, 'base64')
: new Buffer(this.digest, 'base64')
).toString('hex')
}
toString (opts) {
if (opts && opts.strict) {
// Strict mode enforces the standard as close to the foot of the
Expand Down
11 changes: 11 additions & 0 deletions test/integrity.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ test('pickAlgorithm()', t => {
t.done()
})

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

test('semi-private', t => {
t.equal(ssri.Integrity, undefined, 'Integrity class is module-private.')
t.done()
Expand Down

0 comments on commit a9f021c

Please sign in to comment.