Skip to content

Commit f247ec3

Browse files
committed
pkey: change PKey::{RSA,DSA,DH}#params to use nil for missing parameters
The returned Hash from these methods contain 0 in place of a missing parameter in the key, for example: pkey = OpenSSL::PKey.read(OpenSSL::PKey::RSA.new(2048).public_to_pem) pp pkey.params #=> # {"n"=>#<OpenSSL::BN 286934673421[...snip]>, # "e"=>#<OpenSSL::BN 65537>, # "d"=>#<OpenSSL::BN 0>, # "p"=>#<OpenSSL::BN 0>, # "q"=>#<OpenSSL::BN 0>, # "dmp1"=>#<OpenSSL::BN 0>, # "dmq1"=>#<OpenSSL::BN 0>, # "iqmp"=>#<OpenSSL::BN 0>} Let's use nil instead, which is more appropriate for indicating a missing value.
1 parent c14178f commit f247ec3

File tree

4 files changed

+7
-7
lines changed

4 files changed

+7
-7
lines changed

lib/openssl/pkey.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def public_key
4242
# The hash has keys 'p', 'q', 'g', 'pub_key', and 'priv_key'.
4343
def params
4444
%w{p q g pub_key priv_key}.map { |name|
45-
[name, send(name) || 0.to_bn]
45+
[name, send(name)]
4646
}.to_h
4747
end
4848

@@ -174,7 +174,7 @@ def public_key
174174
# The hash has keys 'p', 'q', 'g', 'pub_key', and 'priv_key'.
175175
def params
176176
%w{p q g pub_key priv_key}.map { |name|
177-
[name, send(name) || 0.to_bn]
177+
[name, send(name)]
178178
}.to_h
179179
end
180180

@@ -360,7 +360,7 @@ def public_key
360360
# The hash has keys 'n', 'e', 'd', 'p', 'q', 'dmp1', 'dmq1', and 'iqmp'.
361361
def params
362362
%w{n e d p q dmp1 dmq1 iqmp}.map { |name|
363-
[name, send(name) || 0.to_bn]
363+
[name, send(name)]
364364
}.to_h
365365
end
366366

test/openssl/test_pkey_dh.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,9 @@ def test_params
137137
assert_kind_of(OpenSSL::BN, dh.g)
138138
assert_equal(dh.g, dh.params["g"])
139139
assert_nil(dh.pub_key)
140-
assert_equal(0, dh.params["pub_key"])
140+
assert_nil(dh.params["pub_key"])
141141
assert_nil(dh.priv_key)
142-
assert_equal(0, dh.params["priv_key"])
142+
assert_nil(dh.params["priv_key"])
143143

144144
dhkey = OpenSSL::PKey.generate_key(dh)
145145
assert_equal(dh.params["p"], dhkey.params["p"])

test/openssl/test_pkey_dsa.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ def test_params
248248
assert_equal(key.pub_key, pubkey.pub_key)
249249
assert_equal(key.pub_key, pubkey.params["pub_key"])
250250
assert_nil(pubkey.priv_key)
251-
assert_equal(0, pubkey.params["priv_key"])
251+
assert_nil(pubkey.params["priv_key"])
252252
end
253253

254254
def test_dup

test/openssl/test_pkey_rsa.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ def test_params
595595
assert_equal(key.e, pubkey.e)
596596
[:d, :p, :q, :dmp1, :dmq1, :iqmp].each do |name|
597597
assert_nil(pubkey.send(name))
598-
assert_equal(0, pubkey.params[name.to_s])
598+
assert_nil(pubkey.params[name.to_s])
599599
end
600600
end
601601

0 commit comments

Comments
 (0)