From 792db92de439d9331202256f37e46e9002939b1d Mon Sep 17 00:00:00 2001 From: Louis-Dominique Dubeau Date: Wed, 25 Jul 2018 10:30:05 -0400 Subject: [PATCH] fix: allow passing an algorithm name to digest The examples on [MDN][1] show strings passed as the first argument. I've tested that it actually works in browsers. The [specification][2] shows `AlgorithmIdentifier` as the first argument to `digest` and `AlgorithmIdentifier` is defined as `typedef (object or DOMString) AlgorithmIdentifier;` [1]: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest [2]: https://www.w3.org/TR/WebCryptoAPI/#subtlecrypto-interface --- src/SubtleCrypto.js | 2 +- test/SubtleCryptoSpec.js | 53 +++++++++++++++++++++++++++++----------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/SubtleCrypto.js b/src/SubtleCrypto.js index ce5919a..a5729e9 100644 --- a/src/SubtleCrypto.js +++ b/src/SubtleCrypto.js @@ -176,7 +176,7 @@ class SubtleCrypto { return new Promise((resolve, reject) => { try { - let result = normalizedAlgorithm.digest(algorithm, data) + let result = normalizedAlgorithm.digest(normalizedAlgorithm, data) return resolve(result) } catch (error) { return reject(error) diff --git a/test/SubtleCryptoSpec.js b/test/SubtleCryptoSpec.js index 956975a..23dffd1 100644 --- a/test/SubtleCryptoSpec.js +++ b/test/SubtleCryptoSpec.js @@ -596,25 +596,50 @@ describe('SubtleCrypto', () => { }) describe('with valid arguments', () => { - let promise, error + describe('passing the algorithm as an object', () => { + let promise, error + + beforeEach(() => { + let algorithm = { name: 'SHA-256' } + promise = crypto.subtle.digest(algorithm, new Buffer('whatever')) + promise.then(digest => result = digest) + promise.catch(err => error = err) + }) - beforeEach(() => { - let algorithm = { name: 'SHA-256' } - promise = crypto.subtle.digest(algorithm, new Buffer('whatever')) - promise.then(digest => result = digest) - promise.catch(err => error = err) - }) + it('should return a promise', () => { + promise.should.be.instanceof(Promise) + }) - it('should return a promise', () => { - promise.should.be.instanceof(Promise) - }) + it('should resolve an ArrayBuffer', () => { + result.should.be.instanceof(ArrayBuffer) + }) - it('should resolve an ArrayBuffer', () => { - result.should.be.instanceof(ArrayBuffer) + it('should not reject the promise', () => { + expect(error).to.be.undefined + }) }) - it('should not reject the promise', () => { - expect(error).to.be.undefined + describe('passing the algorithm as a string', () => { + let promise, error + + beforeEach(() => { + let algorithm = 'SHA-256' + promise = crypto.subtle.digest(algorithm, new Buffer('whatever')) + promise.then(digest => result = digest) + promise.catch(err => error = err) + }) + + it('should return a promise', () => { + promise.should.be.instanceof(Promise) + }) + + it('should resolve an ArrayBuffer', () => { + result.should.be.instanceof(ArrayBuffer) + }) + + it('should not reject the promise', () => { + expect(error).to.be.undefined + }) }) }) })