GeezerVison is showing github.com — crypto: runtime-deprecate calling Hmac.digest() more than once · nodejs/node@463e56f
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/deprecations.md +4-1Lines changed: 4 additions & 1 deletion - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -4562,12 +4562,15 @@ removed in a future version of Node.js. | 4562 | 4562 |
| 4563 | 4563 | <!-- YAML | 4564 | 4564 | changes: | | 4565 | + - version: REPLACEME | | 4566 | + pr-url: https://github.com/nodejs/node/pull/63624 | | 4567 | + description: Runtime deprecation. | 4565 | 4568 | - version: v26.2.0 | 4566 | 4569 | pr-url: https://github.com/nodejs/node/pull/63121 | 4567 | 4570 | description: Documentation-only deprecation. | 4568 | 4571 | --> | 4569 | 4572 |
| 4570 | | -Type: Documentation-only | | 4573 | +Type: Runtime | 4571 | 4574 |
| 4572 | 4575 | Calling `hmac.digest()` more than once returns an empty buffer instead of | 4573 | 4576 | throwing an error. This behavior is inconsistent with `hash.digest()` and | |
Collapse file Copy file name to clipboardExpand all lines: lib/internal/crypto/hash.js +6Lines changed: 6 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -87,6 +87,11 @@ const maybeEmitDeprecationWarning = getDeprecationWarningEmitter( | 87 | 87 | }, | 88 | 88 | ); | 89 | 89 |
| | 90 | +const emitHmacDigestDeprecation = getDeprecationWarningEmitter( | | 91 | + 'DEP0206', | | 92 | + 'Calling Hmac.digest() more than once is deprecated.', | | 93 | +); | | 94 | + | 90 | 95 | function Hash(algorithm, options) { | 91 | 96 | if (!new.target) | 92 | 97 | return new Hash(algorithm, options); | @@ -187,6 +192,7 @@ Hmac.prototype.digest = function digest(outputEncoding) { | 187 | 192 | const state = this[kState]; | 188 | 193 |
| 189 | 194 | if (state[kFinalized]) { | | 195 | + emitHmacDigestDeprecation(); | 190 | 196 | const buf = Buffer.from(''); | 191 | 197 | if (outputEncoding && outputEncoding !== 'buffer') | 192 | 198 | return buf.toString(outputEncoding); | |
Collapse file Copy file name to clipboard +19Lines changed: 19 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +if (!common.hasCrypto) | | 5 | + common.skip('missing crypto'); | | 6 | + | | 7 | +const assert = require('assert'); | | 8 | +const crypto = require('crypto'); | | 9 | + | | 10 | +common.expectWarning({ | | 11 | + DeprecationWarning: { | | 12 | + DEP0181: 'crypto.Hmac constructor is deprecated.', | | 13 | + }, | | 14 | +}); | | 15 | + | | 16 | +const Hmac = crypto.Hmac; | | 17 | +const instance = crypto.Hmac('sha256', 'Node'); | | 18 | +assert(instance instanceof Hmac, 'Hmac is expected to return a new instance' + | | 19 | + ' when called without `new`'); |
Collapse file Copy file name to clipboard +56Lines changed: 56 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +if (!common.hasCrypto) | | 5 | + common.skip('missing crypto'); | | 6 | + | | 7 | +const assert = require('assert'); | | 8 | +const crypto = require('crypto'); | | 9 | + | | 10 | +common.expectWarning({ | | 11 | + DeprecationWarning: { | | 12 | + DEP0206: 'Calling Hmac.digest() more than once is deprecated.', | | 13 | + }, | | 14 | +}); | | 15 | + | | 16 | +// Verify runtime deprecation warning for calling digest() more than once. | | 17 | +{ | | 18 | + const h = crypto.createHmac('sha1', 'key').update('data'); | | 19 | + h.digest('hex'); | | 20 | + h.digest('hex'); | | 21 | +} | | 22 | + | | 23 | +// Check initialized -> uninitialized state transition after calling digest(). | | 24 | +{ | | 25 | + const expected = | | 26 | + '\u0010\u0041\u0052\u00c5\u00bf\u00dc\u00a0\u007b\u00c6\u0033' + | | 27 | + '\u00ee\u00bd\u0046\u0019\u009f\u0002\u0055\u00c9\u00f4\u009d'; | | 28 | + { | | 29 | + const h = crypto.createHmac('sha1', 'key').update('data'); | | 30 | + assert.deepStrictEqual(h.digest('buffer'), Buffer.from(expected, 'latin1')); | | 31 | + assert.deepStrictEqual(h.digest('buffer'), Buffer.from('')); | | 32 | + } | | 33 | + { | | 34 | + const h = crypto.createHmac('sha1', 'key').update('data'); | | 35 | + assert.strictEqual(h.digest('latin1'), expected); | | 36 | + assert.strictEqual(h.digest('latin1'), ''); | | 37 | + } | | 38 | +} | | 39 | + | | 40 | +// Check initialized -> uninitialized state transition after calling digest(). | | 41 | +// Calls to update() omitted intentionally. | | 42 | +{ | | 43 | + const expected = | | 44 | + '\u00f4\u002b\u00b0\u00ee\u00b0\u0018\u00eb\u00bd\u0045\u0097' + | | 45 | + '\u00ae\u0072\u0013\u0071\u001e\u00c6\u0007\u0060\u0084\u003f'; | | 46 | + { | | 47 | + const h = crypto.createHmac('sha1', 'key'); | | 48 | + assert.deepStrictEqual(h.digest('buffer'), Buffer.from(expected, 'latin1')); | | 49 | + assert.deepStrictEqual(h.digest('buffer'), Buffer.from('')); | | 50 | + } | | 51 | + { | | 52 | + const h = crypto.createHmac('sha1', 'key'); | | 53 | + assert.strictEqual(h.digest('latin1'), expected); | | 54 | + assert.strictEqual(h.digest('latin1'), ''); | | 55 | + } | | 56 | +} |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-crypto-hmac.js -52Lines changed: 0 additions & 52 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -7,13 +7,6 @@ if (!common.hasCrypto) { | 7 | 7 | const assert = require('assert'); | 8 | 8 | const crypto = require('crypto'); | 9 | 9 |
| 10 | | -{ | 11 | | - const Hmac = crypto.Hmac; | 12 | | - const instance = crypto.Hmac('sha256', 'Node'); | 13 | | - assert(instance instanceof Hmac, 'Hmac is expected to return a new instance' + | 14 | | - ' when called without `new`'); | 15 | | -} | 16 | | - | 17 | 10 | assert.throws( | 18 | 11 | () => crypto.createHmac(null), | 19 | 12 | { | @@ -411,41 +404,6 @@ assert.strictEqual( | 411 | 404 | crypto.createHmac('sha256', 'w00t').digest('ucs2'), | 412 | 405 | crypto.createHmac('sha256', 'w00t').digest().toString('ucs2')); | 413 | 406 |
| 414 | | -// Check initialized -> uninitialized state transition after calling digest(). | 415 | | -{ | 416 | | - const expected = | 417 | | - '\u0010\u0041\u0052\u00c5\u00bf\u00dc\u00a0\u007b\u00c6\u0033' + | 418 | | - '\u00ee\u00bd\u0046\u0019\u009f\u0002\u0055\u00c9\u00f4\u009d'; | 419 | | - { | 420 | | - const h = crypto.createHmac('sha1', 'key').update('data'); | 421 | | - assert.deepStrictEqual(h.digest('buffer'), Buffer.from(expected, 'latin1')); | 422 | | - assert.deepStrictEqual(h.digest('buffer'), Buffer.from('')); | 423 | | - } | 424 | | - { | 425 | | - const h = crypto.createHmac('sha1', 'key').update('data'); | 426 | | - assert.strictEqual(h.digest('latin1'), expected); | 427 | | - assert.strictEqual(h.digest('latin1'), ''); | 428 | | - } | 429 | | -} | 430 | | - | 431 | | -// Check initialized -> uninitialized state transition after calling digest(). | 432 | | -// Calls to update() omitted intentionally. | 433 | | -{ | 434 | | - const expected = | 435 | | - '\u00f4\u002b\u00b0\u00ee\u00b0\u0018\u00eb\u00bd\u0045\u0097' + | 436 | | - '\u00ae\u0072\u0013\u0071\u001e\u00c6\u0007\u0060\u0084\u003f'; | 437 | | - { | 438 | | - const h = crypto.createHmac('sha1', 'key'); | 439 | | - assert.deepStrictEqual(h.digest('buffer'), Buffer.from(expected, 'latin1')); | 440 | | - assert.deepStrictEqual(h.digest('buffer'), Buffer.from('')); | 441 | | - } | 442 | | - { | 443 | | - const h = crypto.createHmac('sha1', 'key'); | 444 | | - assert.strictEqual(h.digest('latin1'), expected); | 445 | | - assert.strictEqual(h.digest('latin1'), ''); | 446 | | - } | 447 | | -} | 448 | | - | 449 | 407 | { | 450 | 408 | assert.throws( | 451 | 409 | () => crypto.createHmac('sha7', 'key'), | @@ -460,13 +418,3 @@ assert.strictEqual( | 460 | 418 | crypto.createHmac('sha256', keyObject).update('foo').digest(), | 461 | 419 | ); | 462 | 420 | } | 463 | | - | 464 | | -{ | 465 | | - crypto.Hmac('sha256', 'Node'); | 466 | | - common.expectWarning({ | 467 | | - DeprecationWarning: [ | 468 | | - ['crypto.Hmac constructor is deprecated.', | 469 | | - 'DEP0181'], | 470 | | - ] | 471 | | - }); | 472 | | -} |
|