Skip to content

Commit

Permalink
crypto: fix webcrypto digest() invalid algorithm
Browse files Browse the repository at this point in the history
PR-URL: #43431
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
panva authored and targos committed Jul 12, 2022
1 parent dedb22e commit 64a9dd7
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
25 changes: 20 additions & 5 deletions lib/internal/crypto/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ const {
prepareSecretKey,
} = require('internal/crypto/keys');

const {
lazyDOMException,
} = require('internal/util');

const {
Buffer,
} = require('buffer');
Expand Down Expand Up @@ -171,11 +175,22 @@ async function asyncDigest(algorithm, data) {
if (algorithm.length !== undefined)
validateUint32(algorithm.length, 'algorithm.length');

return jobPromise(new HashJob(
kCryptoJobAsync,
normalizeHashName(algorithm.name),
data,
algorithm.length));
switch (algorithm.name) {
case 'SHA-1':
// Fall through
case 'SHA-256':
// Fall through
case 'SHA-384':
// Fall through
case 'SHA-512':
return jobPromise(new HashJob(
kCryptoJobAsync,
normalizeHashName(algorithm.name),
data,
algorithm.length));
}

throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
}

module.exports = {
Expand Down
6 changes: 6 additions & 0 deletions test/parallel/test-webcrypto-digest.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,3 +168,9 @@ async function testDigest(size, name) {

await Promise.all(variations);
})().then(common.mustCall());

(async () => {
await assert.rejects(subtle.digest('RSA-OAEP', Buffer.alloc(1)), {
name: 'NotSupportedError',
});
})().then(common.mustCall());

0 comments on commit 64a9dd7

Please sign in to comment.