Skip to content

Commit 08197aa

Browse files
tniessenUlisesGascon
authored andcommittedSep 10, 2023
crypto: remove default encoding from sign/verify
getDefaultEncoding() always returns 'buffer' in Node.js 20. It requires some careful justification but the default encoding can be eliminated from sig.js entirely. In Sign.prototype.update, we can safely remove the conditional assignment of getDefaultEncoding() to encoding. This is because SignUpdate() in crypto_sig.cc internally calls node::crypto::Decode, which returns UTF8 for falsy encoding values. In other words, with the conditional assignment, StringBytes::Write() ultimately receives the encoding BUFFER, and without the conditional assignment, it receives the encoding UTF8. However, StringBytes::Write() treats both encodings identically, so there is no need to deviate from the internal default encoding UTF8. In Sign.prototype.sign, we can also safely remove the conditional assignment of getDefaultEncoding() to encoding. Whether encoding is falsy or 'buffer' makes no difference. In Verify.prototype.verify, we can also safely remove the conditional assignment of getDefaultEncoding() to sigEncoding. This is because the function passes the sigEncoding to getArrayBufferOrView(), which passes it to Buffer.from(). If sigEncoding is 'buffer', getArrayBufferOrView() instead passes 'utf8' to Buffer.from(). Because the default encoding of Buffer.from() is 'utf8', passing a falsy encoding to getArrayBufferOrView() instead of 'buffer' results in the same behavior. Refs: #47182 PR-URL: #49145 Reviewed-By: Filip Skokan <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 173aed4 commit 08197aa

File tree

1 file changed

+0
-6
lines changed

1 file changed

+0
-6
lines changed
 

‎lib/internal/crypto/sig.js

-6
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const {
3434

3535
const {
3636
getArrayBufferOrView,
37-
getDefaultEncoding,
3837
kHandle,
3938
} = require('internal/crypto/util');
4039

@@ -70,8 +69,6 @@ Sign.prototype._write = function _write(chunk, encoding, callback) {
7069
};
7170

7271
Sign.prototype.update = function update(data, encoding) {
73-
encoding = encoding || getDefaultEncoding();
74-
7572
if (typeof data === 'string') {
7673
validateEncoding(data, encoding);
7774
} else if (!isArrayBufferView(data)) {
@@ -131,7 +128,6 @@ Sign.prototype.sign = function sign(options, encoding) {
131128
const ret = this[kHandle].sign(data, format, type, passphrase, rsaPadding,
132129
pssSaltLength, dsaSigEnc);
133130

134-
encoding = encoding || getDefaultEncoding();
135131
if (encoding && encoding !== 'buffer')
136132
return ret.toString(encoding);
137133

@@ -216,8 +212,6 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) {
216212
passphrase,
217213
} = preparePublicOrPrivateKey(options, true);
218214

219-
sigEncoding = sigEncoding || getDefaultEncoding();
220-
221215
// Options specific to RSA
222216
const rsaPadding = getPadding(options);
223217
const pssSaltLength = getSaltLength(options);

0 commit comments

Comments
 (0)
Please sign in to comment.