Skip to content

Commit

Permalink
crypto: remove default encoding from Hash/Hmac
Browse files Browse the repository at this point in the history
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
nodejs#49145 so I won't repeat it here.

Refs: nodejs#47182
PR-URL: nodejs#49167
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Filip Skokan <panva.ip@gmail.com>
  • Loading branch information
tniessen authored Aug 16, 2023
1 parent 556e95a commit 8ed4397
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions lib/internal/crypto/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const {
} = internalBinding('crypto');

const {
getDefaultEncoding,
getStringOption,
jobPromise,
normalizeHashName,
Expand Down Expand Up @@ -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();
Expand All @@ -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;
};
Expand All @@ -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;
};
Expand Down

0 comments on commit 8ed4397

Please sign in to comment.