Skip to content

Commit 46e1b14

Browse files
crypto: runtime deprecate multiple Hmac.digest() calls (DEP0200)
1 parent 6e2188e commit 46e1b14

3 files changed

Lines changed: 42 additions & 0 deletions

File tree

doc/api/deprecations.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4558,6 +4558,21 @@ that have proven unresolveable. See [caveats of asynchronous customization hooks
45584558
`module.registerHooks()` as soon as possible as `module.register()` will be
45594559
removed in a future version of Node.js.
45604560
4561+
### DEP0206: Calling `digest()` on an already-finalized `Hmac` instance
4562+
4563+
<!-- YAML
4564+
changes:
4565+
- version: REPLACEME
4566+
pr-url: https://github.com/nodejs/node/pull/XXXXX
4567+
description: Runtime-deprecation.
4568+
-->
4569+
4570+
Type: Runtime
4571+
4572+
Calling [`hmac.digest()`][] more than once returns an empty buffer instead of
4573+
throwing an error. This behavior is inconsistent with [`hash.digest()`][] and
4574+
may lead to subtle bugs. Use the result of the first call instead.
4575+
45614576
[DEP0142]: #dep0142-repl_builtinlibs
45624577
[NIST SP 800-38D]: https://nvlpubs.nist.gov/nistpubs/Legacy/SP/nistspecialpublication800-38d.pdf
45634578
[RFC 6066]: https://tools.ietf.org/html/rfc6066#section-3

lib/internal/crypto/hash.js

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

90+
const emitHmacDoubleDigestDeprecation = getDeprecationWarningEmitter(
91+
'DEP0206',
92+
'Calling digest() on an already-finalized Hmac instance is deprecated.',
93+
undefined,
94+
false,
95+
);
96+
9097
function Hash(algorithm, options) {
9198
if (!new.target)
9299
return new Hash(algorithm, options);
@@ -183,6 +190,7 @@ Hmac.prototype.digest = function digest(outputEncoding) {
183190
const state = this[kState];
184191

185192
if (state[kFinalized]) {
193+
emitHmacDoubleDigestDeprecation();
186194
const buf = Buffer.from('');
187195
if (outputEncoding && outputEncoding !== 'buffer')
188196
return buf.toString(outputEncoding);
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
if (!common.hasCrypto)
5+
common.skip('missing crypto');
6+
7+
const assert = require('assert');
8+
const { createHmac } = require('crypto');
9+
10+
common.expectWarning(
11+
'DeprecationWarning',
12+
'Calling digest() on an already-finalized Hmac instance is deprecated.',
13+
'DEP0206',
14+
);
15+
16+
const hmac = createHmac('sha256', 'key').update('data');
17+
hmac.digest();
18+
const second = hmac.digest();
19+
assert.deepStrictEqual(second, Buffer.from(''));

0 commit comments

Comments
 (0)