diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 68dd5a4a36561c..e1da58ccfb696b 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -4562,12 +4562,15 @@ removed in a future version of Node.js. -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 857753c2b39f9c..8c496374d281e0 100644 --- a/lib/internal/crypto/hash.js +++ b/lib/internal/crypto/hash.js @@ -87,6 +87,13 @@ const maybeEmitDeprecationWarning = getDeprecationWarningEmitter( }, ); +const emitHmacDoubleDigestDeprecation = getDeprecationWarningEmitter( + 'DEP0206', + 'Calling digest() on an already-finalized Hmac instance is deprecated.', + undefined, + false, +); + function Hash(algorithm, options) { if (!new.target) return new Hash(algorithm, options); @@ -183,6 +190,7 @@ Hmac.prototype.digest = function digest(outputEncoding) { const state = this[kState]; if (state[kFinalized]) { + emitHmacDoubleDigestDeprecation(); const buf = Buffer.from(''); if (outputEncoding && outputEncoding !== 'buffer') return buf.toString(outputEncoding); diff --git a/test/parallel/test-crypto-dep0206.js b/test/parallel/test-crypto-dep0206.js new file mode 100644 index 00000000000000..62f10e6db20b91 --- /dev/null +++ b/test/parallel/test-crypto-dep0206.js @@ -0,0 +1,19 @@ +'use strict'; + +const common = require('../common'); +if (!common.hasCrypto) + common.skip('missing crypto'); + +const assert = require('assert'); +const { createHmac } = require('crypto'); + +common.expectWarning({ + DeprecationWarning: { + DEP0206: 'Calling digest() on an already-finalized Hmac instance is deprecated.', + }, +}); + +const hmac = createHmac('sha256', 'key').update('data'); +hmac.digest(); +const second = hmac.digest(); +assert.deepStrictEqual(second, Buffer.from('')); diff --git a/test/parallel/test-crypto-hmac.js b/test/parallel/test-crypto-hmac.js index afb5f74cbf076b..57b2b5db344ba3 100644 --- a/test/parallel/test-crypto-hmac.js +++ b/test/parallel/test-crypto-hmac.js @@ -7,6 +7,12 @@ if (!common.hasCrypto) { const assert = require('assert'); const crypto = require('crypto'); +common.expectWarning({ + DeprecationWarning: { + DEP0206: 'Calling digest() on an already-finalized Hmac instance is deprecated.', + }, +}); + { const Hmac = crypto.Hmac; const instance = crypto.Hmac('sha256', 'Node');