diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 6776c790d0a433..c335bde3c8f8c7 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -4565,9 +4565,12 @@ changes: - version: v26.2.0 pr-url: https://github.com/nodejs/node/pull/63121 description: Documentation-only deprecation. + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/REPLACEME + description: Runtime deprecation. --> -Type: Documentation-only +Type: Runtime Calling `hmac.digest()` more than once returns an empty buffer instead of throwing an error. This behavior is inconsistent with `hash.digest()` and diff --git a/lib/internal/crypto/hash.js b/lib/internal/crypto/hash.js index 5aec1614cb92e9..7d4adcb43ba51a 100644 --- a/lib/internal/crypto/hash.js +++ b/lib/internal/crypto/hash.js @@ -87,6 +87,11 @@ const maybeEmitDeprecationWarning = getDeprecationWarningEmitter( }, ); +const emitHmacDigestDeprecation = getDeprecationWarningEmitter( + 'DEP0206', + 'Calling Hmac.digest() more than once is deprecated.', +); + function Hash(algorithm, options) { if (!new.target) return new Hash(algorithm, options); @@ -183,6 +188,7 @@ Hmac.prototype.digest = function digest(outputEncoding) { const state = this[kState]; if (state[kFinalized]) { + emitHmacDigestDeprecation(); const buf = Buffer.from(''); if (outputEncoding && outputEncoding !== 'buffer') return buf.toString(outputEncoding); diff --git a/test/parallel/test-crypto-hmac.js b/test/parallel/test-crypto-hmac.js index afb5f74cbf076b..0d29ec6afa9b19 100644 --- a/test/parallel/test-crypto-hmac.js +++ b/test/parallel/test-crypto-hmac.js @@ -39,6 +39,17 @@ assert.throws( name: 'TypeError', }); +// Verify runtime deprecation warning for calling digest() more than once. +{ + common.expectWarning( + 'DeprecationWarning', + 'Calling Hmac.digest() more than once is deprecated.', + 'DEP0206'); + const h = crypto.createHmac('sha1', 'key').update('data'); + h.digest('hex'); + h.digest('hex'); +} + function testHmac(algo, key, data, expected) { // FIPS does not support MD5. if (crypto.getFips() && algo === 'md5')