Skip to content

Commit

Permalink
Use string keys
Browse files Browse the repository at this point in the history
  • Loading branch information
anakinj committed Mar 28, 2024
1 parent ff7831b commit 762bdb1
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions test/openssl/test_pkey.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ def test_from_data_with_invalid_alg
end

def test_s_from_data_rsa_with_n_e_and_d_given_as_integers
new_key = OpenSSL::PKey.from_data("RSA", n: 3161751493,
e: 65537,
d: 2064855961)
new_key = OpenSSL::PKey.from_data("RSA", "n" => 3161751493,
"e" => 65537,
"d" => 2064855961)

assert_instance_of OpenSSL::PKey::RSA, new_key
assert_equal true, new_key.private?
Expand All @@ -264,10 +264,10 @@ def test_s_from_data_rsa_with_n_e_and_d_given_as_integers
assert_equal OpenSSL::BN.new(2064855961), new_key.d
end

def test_s_from_data_rsa_with_n_e_and_d_given_as_strings
new_key = OpenSSL::PKey.from_data("RSA", "n" => OpenSSL::BN.new(3161751493),
"e" => OpenSSL::BN.new(65537),
"d" => OpenSSL::BN.new(2064855961))
def test_s_from_data_rsa_with_n_e_and_d_given_as_symbols
new_key = OpenSSL::PKey.from_data("RSA", n: OpenSSL::BN.new(3161751493),
e: OpenSSL::BN.new(65537),
d: OpenSSL::BN.new(2064855961))

assert_instance_of OpenSSL::PKey::RSA, new_key
assert_equal true, new_key.private?
Expand All @@ -277,8 +277,8 @@ def test_s_from_data_rsa_with_n_e_and_d_given_as_strings
end

def test_s_from_data_rsa_with_n_and_e_given
new_key = OpenSSL::PKey.from_data("RSA", n: OpenSSL::BN.new(3161751493),
e: OpenSSL::BN.new(65537))
new_key = OpenSSL::PKey.from_data("RSA", "n" => OpenSSL::BN.new(3161751493),
"e" => OpenSSL::BN.new(65537))

assert_instance_of OpenSSL::PKey::RSA, new_key
assert_equal false, new_key.private?
Expand All @@ -289,9 +289,9 @@ def test_s_from_data_rsa_with_n_and_e_given

def test_s_from_data_rsa_with_openssl_internal_names
source = Fixtures.pkey("rsa2048")
new_key = OpenSSL::PKey.from_data("RSA", n: source.n,
e: source.e,
d: source.d,
new_key = OpenSSL::PKey.from_data("RSA", "n" => source.n,
"e" => source.e,
"d" => source.d,
"rsa-factor1" => source.p,
"rsa-factor2" => source.q,
"rsa-exponent1" => source.dmp1,
Expand All @@ -311,14 +311,14 @@ def test_s_from_data_rsa_with_openssl_internal_names

def test_s_from_data_rsa_with_simple_names
source = Fixtures.pkey("rsa2048")
new_key = OpenSSL::PKey.from_data("RSA", n: source.n,
e: source.e,
d: source.d,
p: source.p,
q: source.q,
dmp1: source.dmp1,
dmq1: source.dmq1,
iqmp: source.iqmp)
new_key = OpenSSL::PKey.from_data("RSA", "n" => source.n,
"e" => source.e,
"d" => source.d,
"p" => source.p,
"q" => source.q,
"dmp1" => source.dmp1,
"dmq1" => source.dmq1,
"iqmp" => source.iqmp)

assert_equal source.n, new_key.n
assert_equal source.e, new_key.e
Expand All @@ -333,14 +333,14 @@ def test_s_from_data_rsa_with_simple_names

def test_s_from_data_rsa_with_invalid_parameter
assert_raise_with_message(OpenSSL::PKey::PKeyError, /Invalid parameter "invalid"/) do
OpenSSL::PKey.from_data("RSA", invalid: 1234)
OpenSSL::PKey.from_data("RSA", "invalid" => 1234)
end
end

def test_s_from_data_ec_pub_given_as_string
source = Fixtures.pkey("ec-prime256v1")
new_key = OpenSSL::PKey.from_data("EC", group: source.group.curve_name,
pub: source.public_key.to_bn.to_s(2))
new_key = OpenSSL::PKey.from_data("EC", "group" => source.group.curve_name,
"pub" => source.public_key.to_bn.to_s(2))
assert_instance_of OpenSSL::PKey::EC, new_key
assert_equal source.group.curve_name, new_key.group.curve_name
assert_equal source.public_key, new_key.public_key
Expand All @@ -349,8 +349,8 @@ def test_s_from_data_ec_pub_given_as_string

def test_s_from_data_ec_priv_given_as_bn
source = Fixtures.pkey("ec-prime256v1")
new_key = OpenSSL::PKey.from_data("EC", group: source.group.curve_name,
priv: source.private_key.to_bn)
new_key = OpenSSL::PKey.from_data("EC", "group" => source.group.curve_name,
"priv" => source.private_key.to_bn)
assert_instance_of OpenSSL::PKey::EC, new_key
assert_equal source.group.curve_name, new_key.group.curve_name
assert_equal source.private_key, new_key.private_key
Expand All @@ -359,8 +359,8 @@ def test_s_from_data_ec_priv_given_as_bn

def test_s_from_data_ec_priv_given_as_integer
source = Fixtures.pkey("ec-prime256v1")
new_key = OpenSSL::PKey.from_data("EC", group: source.group.curve_name,
priv: source.private_key.to_i)
new_key = OpenSSL::PKey.from_data("EC", "group" => source.group.curve_name,
"priv" => source.private_key.to_i)
assert_instance_of OpenSSL::PKey::EC, new_key
assert_equal source.group.curve_name, new_key.group.curve_name
assert_equal source.private_key, new_key.private_key
Expand All @@ -371,9 +371,9 @@ def test_s_from_data_ec_priv_and_pub_given_for_different_curves
[Fixtures.pkey("ec-prime256v1"),
Fixtures.pkey("ec-secp384r1"),
Fixtures.pkey("ec-secp521r1")].each do |source|
new_key = OpenSSL::PKey.from_data("EC", group: source.group.curve_name,
pub: source.public_key.to_bn.to_s(2),
priv: source.private_key.to_i)
new_key = OpenSSL::PKey.from_data("EC", "group" => source.group.curve_name,
"pub" => source.public_key.to_bn.to_s(2),
"priv" => source.private_key.to_i)
assert_instance_of OpenSSL::PKey::EC, new_key
assert_equal source.group.curve_name, new_key.group.curve_name
assert_equal source.private_key, new_key.private_key
Expand All @@ -383,23 +383,23 @@ def test_s_from_data_ec_priv_and_pub_given_for_different_curves

def test_s_from_data_ec_pub_given_as_integer
assert_raise_with_message(TypeError, "no implicit conversion of Integer into String") do
OpenSSL::PKey.from_data("EC", { group: "prime256v1", pub: 12345 })
OpenSSL::PKey.from_data("EC", { "group" => "prime256v1", "pub" => 12345 })
end
end

def test_s_from_data_ec_with_invalid_parameter
assert_raise_with_message(OpenSSL::PKey::PKeyError, /Invalid parameter "invalid"/) do
OpenSSL::PKey.from_data("EC", invalid: 1234)
OpenSSL::PKey.from_data("EC", "invalid" => 1234)
end
end

def test_s_from_data_dsa_with_all_supported_parameters
source = Fixtures.pkey("dsa1024")
new_key = OpenSSL::PKey.from_data("DSA", pub: source.params["pub_key"],
priv: source.params["priv_key"],
p: source.params["p"],
q: source.params["q"],
g: source.params["g"])
new_key = OpenSSL::PKey.from_data("DSA", "pub" => source.params["pub_key"],
"priv" => source.params["priv_key"],
"p" => source.params["p"],
"q" => source.params["q"],
"g" => source.params["g"])

assert_instance_of OpenSSL::PKey::DSA, new_key
assert_equal source.params, new_key.params
Expand All @@ -414,7 +414,7 @@ def test_s_from_data_dsa_with_gem_specific_keys

def test_s_from_data_dsa_with_invalid_parameter
assert_raise_with_message(OpenSSL::PKey::PKeyError, /Invalid parameter "invalid". Supported parameters: p, q, g, j/) do
OpenSSL::PKey.from_data("DSA", invalid: 1234)
OpenSSL::PKey.from_data("DSA", "invalid" => 1234)
end
end

Expand All @@ -428,7 +428,7 @@ def test_s_from_data_dh_with_all_supported_parameters

def test_s_from_data_dh_with_invalid_parameter
assert_raise_with_message(OpenSSL::PKey::PKeyError, /Invalid parameter "invalid"/) do
OpenSSL::PKey.from_data("DH", invalid: 1234)
OpenSSL::PKey.from_data("DH", "invalid" => 1234)
end
end

Expand All @@ -442,7 +442,7 @@ def test_s_from_data_ed25519
-----END PUBLIC KEY-----
EOF

key = OpenSSL::PKey.from_data("ED25519", pub: "\xD0\x8E\xA8\x96\xB6Fbi{$k\xAC\xB8\xA2V\xF4n\xC3\xD06}R\x8A\xE6I\xA7r\xF6D{W\x84")
key = OpenSSL::PKey.from_data("ED25519", "pub" => "\xD0\x8E\xA8\x96\xB6Fbi{$k\xAC\xB8\xA2V\xF4n\xC3\xD06}R\x8A\xE6I\xA7r\xF6D{W\x84")
assert_instance_of OpenSSL::PKey::PKey, key
assert_equal pub_pem, key.public_to_pem
end
Expand Down

0 comments on commit 762bdb1

Please sign in to comment.