Skip to content

Commit 3e3d908

Browse files
danbevMylesBorins
authored andcommitted
src: add encoding_type variable in WritePrivateKey
This commit adds a local variable named encoding_type which is set to the value of the Maybe using ToChecked(). The motivation for this is the code for ToChecked() could be executed multiple times depending on path taken at runtime. I also think this improves readability, or at least it is as readable as before this change. PR-URL: #34181 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: David Carlier <[email protected]>
1 parent daf2abf commit 3e3d908

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/node_crypto.cc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3151,7 +3151,8 @@ static MaybeLocal<Value> WritePrivateKey(
31513151

31523152
bool err;
31533153

3154-
if (config.type_.ToChecked() == kKeyEncodingPKCS1) {
3154+
PKEncodingType encoding_type = config.type_.ToChecked();
3155+
if (encoding_type == kKeyEncodingPKCS1) {
31553156
// PKCS#1 is only permitted for RSA keys.
31563157
CHECK_EQ(EVP_PKEY_id(pkey), EVP_PKEY_RSA);
31573158

@@ -3171,7 +3172,7 @@ static MaybeLocal<Value> WritePrivateKey(
31713172
CHECK_NULL(config.cipher_);
31723173
err = i2d_RSAPrivateKey_bio(bio.get(), rsa.get()) != 1;
31733174
}
3174-
} else if (config.type_.ToChecked() == kKeyEncodingPKCS8) {
3175+
} else if (encoding_type == kKeyEncodingPKCS8) {
31753176
if (config.format_ == kKeyFormatPEM) {
31763177
// Encode PKCS#8 as PEM.
31773178
err = PEM_write_bio_PKCS8PrivateKey(
@@ -3191,7 +3192,7 @@ static MaybeLocal<Value> WritePrivateKey(
31913192
nullptr, nullptr) != 1;
31923193
}
31933194
} else {
3194-
CHECK_EQ(config.type_.ToChecked(), kKeyEncodingSEC1);
3195+
CHECK_EQ(encoding_type, kKeyEncodingSEC1);
31953196

31963197
// SEC1 is only permitted for EC keys.
31973198
CHECK_EQ(EVP_PKEY_id(pkey), EVP_PKEY_EC);

0 commit comments

Comments
 (0)