Skip to content

Commit 7d71643

Browse files
crypto: runtime-deprecate calling Hmac.digest() more than once
Emit a DEP0206 deprecation warning when Hmac.digest() is called on an already-finalized instance. This behavior is inconsistent with Hash.digest() which throws ERR_CRYPTO_HASH_FINALIZED. Refs: #62838 PR-URL: https://github.com/nodejs/node/pull/REPLACEME
1 parent 5c78025 commit 7d71643

3 files changed

Lines changed: 21 additions & 1 deletion

File tree

doc/api/deprecations.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4565,9 +4565,12 @@ changes:
45654565
- version: v26.2.0
45664566
pr-url: https://github.com/nodejs/node/pull/63121
45674567
description: Documentation-only deprecation.
4568+
- version: REPLACEME
4569+
pr-url: https://github.com/nodejs/node/pull/REPLACEME
4570+
description: Runtime deprecation.
45684571
-->
45694572
4570-
Type: Documentation-only
4573+
Type: Runtime
45714574
45724575
Calling `hmac.digest()` more than once returns an empty buffer instead of
45734576
throwing an error. This behavior is inconsistent with `hash.digest()` and

lib/internal/crypto/hash.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ const maybeEmitDeprecationWarning = getDeprecationWarningEmitter(
8787
},
8888
);
8989

90+
const emitHmacDigestDeprecation = getDeprecationWarningEmitter(
91+
'DEP0206',
92+
'Calling Hmac.digest() more than once is deprecated.',
93+
);
94+
9095
function Hash(algorithm, options) {
9196
if (!new.target)
9297
return new Hash(algorithm, options);
@@ -183,6 +188,7 @@ Hmac.prototype.digest = function digest(outputEncoding) {
183188
const state = this[kState];
184189

185190
if (state[kFinalized]) {
191+
emitHmacDigestDeprecation();
186192
const buf = Buffer.from('');
187193
if (outputEncoding && outputEncoding !== 'buffer')
188194
return buf.toString(outputEncoding);

test/parallel/test-crypto-hmac.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ assert.throws(
3939
name: 'TypeError',
4040
});
4141

42+
// Verify runtime deprecation warning for calling digest() more than once.
43+
{
44+
common.expectWarning(
45+
'DeprecationWarning',
46+
'Calling Hmac.digest() more than once is deprecated.',
47+
'DEP0206');
48+
const h = crypto.createHmac('sha1', 'key').update('data');
49+
h.digest('hex');
50+
h.digest('hex');
51+
}
52+
4253
function testHmac(algo, key, data, expected) {
4354
// FIPS does not support MD5.
4455
if (crypto.getFips() && algo === 'md5')

0 commit comments

Comments
 (0)