Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion doc/api/deprecations.md
Original file line number Diff line number Diff line change
Expand Up @@ -4562,12 +4562,15 @@ removed in a future version of Node.js.

<!-- YAML
changes:
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/63162
description: Runtime deprecation.
- version: REPLACEME
pr-url: https://github.com/nodejs/node/pull/63121
description: Documentation-only 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
Expand Down
8 changes: 8 additions & 0 deletions lib/internal/crypto/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions test/parallel/test-crypto-dep0206.js
Original file line number Diff line number Diff line change
@@ -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(''));
6 changes: 6 additions & 0 deletions test/parallel/test-crypto-hmac.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
'use strict';
const common = require('../common');
if (!common.hasCrypto) {
Expand All @@ -7,6 +7,12 @@
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');
Expand Down
Loading