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
  • Loading branch information
tniessen committed Aug 14, 2023
1 parent 634eb50 commit c8d479a
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 c8d479a

Please sign in to comment.