Skip to content

Commit 64a9dd7

Browse files
panvatargos
authored andcommitted
crypto: fix webcrypto digest() invalid algorithm
PR-URL: #43431 Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent dedb22e commit 64a9dd7

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

lib/internal/crypto/hash.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ const {
2828
prepareSecretKey,
2929
} = require('internal/crypto/keys');
3030

31+
const {
32+
lazyDOMException,
33+
} = require('internal/util');
34+
3135
const {
3236
Buffer,
3337
} = require('buffer');
@@ -171,11 +175,22 @@ async function asyncDigest(algorithm, data) {
171175
if (algorithm.length !== undefined)
172176
validateUint32(algorithm.length, 'algorithm.length');
173177

174-
return jobPromise(new HashJob(
175-
kCryptoJobAsync,
176-
normalizeHashName(algorithm.name),
177-
data,
178-
algorithm.length));
178+
switch (algorithm.name) {
179+
case 'SHA-1':
180+
// Fall through
181+
case 'SHA-256':
182+
// Fall through
183+
case 'SHA-384':
184+
// Fall through
185+
case 'SHA-512':
186+
return jobPromise(new HashJob(
187+
kCryptoJobAsync,
188+
normalizeHashName(algorithm.name),
189+
data,
190+
algorithm.length));
191+
}
192+
193+
throw lazyDOMException('Unrecognized name.', 'NotSupportedError');
179194
}
180195

181196
module.exports = {

test/parallel/test-webcrypto-digest.js

+6
Original file line numberDiff line numberDiff line change
@@ -168,3 +168,9 @@ async function testDigest(size, name) {
168168

169169
await Promise.all(variations);
170170
})().then(common.mustCall());
171+
172+
(async () => {
173+
await assert.rejects(subtle.digest('RSA-OAEP', Buffer.alloc(1)), {
174+
name: 'NotSupportedError',
175+
});
176+
})().then(common.mustCall());

0 commit comments

Comments
 (0)