From c795083232948b7c5fd045eb13039ba293426ae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Wed, 16 Aug 2023 16:54:05 +0200 Subject: [PATCH] crypto: remove default encoding from Hash/Hmac getDefaultEncoding() always returns 'buffer' in Node.js 20. It requires some careful justification but the default encoding can be eliminated from hash.js entirely. The reasoning is almost identical with that in https://github.com/nodejs/node/pull/49145 so I won't repeat it here. Refs: https://github.com/nodejs/node/pull/47182 PR-URL: https://github.com/nodejs/node/pull/49167 Reviewed-By: Yagiz Nizipli Reviewed-By: Filip Skokan --- lib/internal/crypto/hash.js | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/lib/internal/crypto/hash.js b/lib/internal/crypto/hash.js index c8e9af003086c8..57fcb63518d52d 100644 --- a/lib/internal/crypto/hash.js +++ b/lib/internal/crypto/hash.js @@ -14,7 +14,6 @@ const { } = internalBinding('crypto'); const { - getDefaultEncoding, getStringOption, jobPromise, normalizeHashName, @@ -95,8 +94,6 @@ Hash.prototype._flush = function _flush(callback) { }; Hash.prototype.update = function update(data, encoding) { - encoding = encoding || getDefaultEncoding(); - const state = this[kState]; if (state[kFinalized]) throw new ERR_CRYPTO_HASH_FINALIZED(); @@ -118,10 +115,9 @@ Hash.prototype.digest = function digest(outputEncoding) { const state = this[kState]; if (state[kFinalized]) throw new ERR_CRYPTO_HASH_FINALIZED(); - outputEncoding = outputEncoding || getDefaultEncoding(); - // Explicit conversion for backward compatibility. - const ret = this[kHandle].digest(`${outputEncoding}`); + // Explicit conversion of truthy values for backward compatibility. + const ret = this[kHandle].digest(outputEncoding && `${outputEncoding}`); state[kFinalized] = true; return ret; }; @@ -147,15 +143,16 @@ Hmac.prototype.update = Hash.prototype.update; Hmac.prototype.digest = function digest(outputEncoding) { const state = this[kState]; - outputEncoding = outputEncoding || getDefaultEncoding(); if (state[kFinalized]) { const buf = Buffer.from(''); - return outputEncoding === 'buffer' ? buf : buf.toString(outputEncoding); + if (outputEncoding && outputEncoding !== 'buffer') + return buf.toString(outputEncoding); + return buf; } - // Explicit conversion for backward compatibility. - const ret = this[kHandle].digest(`${outputEncoding}`); + // Explicit conversion of truthy values for backward compatibility. + const ret = this[kHandle].digest(outputEncoding && `${outputEncoding}`); state[kFinalized] = true; return ret; };