diff --git a/README.md b/README.md index 4649b4a..14e91bc 100644 --- a/README.md +++ b/README.md @@ -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) @@ -215,6 +216,17 @@ may intentionally deprioritize algorithms with known vulnerabilities. ssri.parse('sha1-WEakDigEST sha512-yzd8ELD1piyANiWnmdnpCL5F52f10UfUdEkHywVZeqTt0ymgrxR63Qz0GB7TKPoeeZQmWCaz7T1').pickAlgorithm() // sha512 ``` +#### `> 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' +``` + #### `> ssri.fromData(data, [opts]) -> Integrity` Creates an `Integrity` object from either string or `Buffer` data, calculating diff --git a/index.js b/index.js index d5b63cd..aee9def 100644 --- a/index.js +++ b/index.js @@ -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 diff --git a/test/integrity.js b/test/integrity.js index 5875ac6..4261526 100644 --- a/test/integrity.js +++ b/test/integrity.js @@ -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()