Skip to content

Commit

Permalink
Prefer String#unpack1 (#586)
Browse files Browse the repository at this point in the history
String#unpack1 avoids the intermediate array created by String#unpack
for single elements, while also making a call to Array#first/[0]
unnecessary.
  • Loading branch information
Maumagnaguagno authored Aug 31, 2023
1 parent 2dc1253 commit 8eb0715
Show file tree
Hide file tree
Showing 8 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/openssl/ssl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ def initialize(svr, ctx)
unless ctx.session_id_context
# see #6137 - session id may not exceed 32 bytes
prng = ::Random.new($0.hash)
session_id = prng.bytes(16).unpack('H*')[0]
session_id = prng.bytes(16).unpack1('H*')
@ctx.session_id_context = session_id
end
@start_immediately = true
Expand Down
2 changes: 1 addition & 1 deletion test/openssl/test_digest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def test_digest_by_oid_and_name
end

def encode16(str)
str.unpack("H*").first
str.unpack1("H*")
end

def test_sha2
Expand Down
6 changes: 3 additions & 3 deletions test/openssl/test_ns_spki.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ def test_build_data
def test_decode_data
spki = OpenSSL::Netscape::SPKI.new(@b64)
assert_equal(@b64, spki.to_pem)
assert_equal(@b64.unpack("m").first, spki.to_der)
assert_equal(@b64.unpack1("m"), spki.to_der)
assert_equal("MozillaIsMyFriend", spki.challenge)
assert_equal(OpenSSL::PKey::RSA, spki.public_key.class)

spki = OpenSSL::Netscape::SPKI.new(@b64.unpack("m").first)
spki = OpenSSL::Netscape::SPKI.new(@b64.unpack1("m"))
assert_equal(@b64, spki.to_pem)
assert_equal(@b64.unpack("m").first, spki.to_der)
assert_equal(@b64.unpack1("m"), spki.to_der)
assert_equal("MozillaIsMyFriend", spki.challenge)
assert_equal(OpenSSL::PKey::RSA, spki.public_key.class)
end
Expand Down
4 changes: 2 additions & 2 deletions test/openssl/test_pkcs12.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ def test_create_with_mac_itr
def test_new_with_no_keys
# generated with:
# openssl pkcs12 -certpbe PBE-SHA1-3DES -in <@mycert> -nokeys -export
str = <<~EOF.unpack("m").first
str = <<~EOF.unpack1("m")
MIIGJAIBAzCCBeoGCSqGSIb3DQEHAaCCBdsEggXXMIIF0zCCBc8GCSqGSIb3
DQEHBqCCBcAwggW8AgEAMIIFtQYJKoZIhvcNAQcBMBwGCiqGSIb3DQEMAQMw
DgQIjv5c3OHvnBgCAggAgIIFiMJa8Z/w7errRvCQPXh9dGQz3eJaFq3S2gXD
Expand Down Expand Up @@ -230,7 +230,7 @@ def test_new_with_no_keys
def test_new_with_no_certs
# generated with:
# openssl pkcs12 -inkey fixtures/openssl/pkey/rsa-1.pem -nocerts -export
str = <<~EOF.unpack("m").first
str = <<~EOF.unpack1("m")
MIIJ7wIBAzCCCbUGCSqGSIb3DQEHAaCCCaYEggmiMIIJnjCCCZoGCSqGSIb3
DQEHAaCCCYsEggmHMIIJgzCCCX8GCyqGSIb3DQEMCgECoIIJbjCCCWowHAYK
KoZIhvcNAQwBAzAOBAjX5nN8jyRKwQICCAAEgglIBIRLHfiY1mNHpl3FdX6+
Expand Down
2 changes: 1 addition & 1 deletion test/openssl/test_pkey_dsa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def test_sign_verify
signature = dsa512.sign("SHA256", data)
assert_equal true, dsa512.verify("SHA256", signature, data)

signature0 = (<<~'end;').unpack("m")[0]
signature0 = (<<~'end;').unpack1("m")
MCwCFH5h40plgU5Fh0Z4wvEEpz0eE9SnAhRPbkRB8ggsN/vsSEYMXvJwjGg/
6g==
end;
Expand Down
2 changes: 1 addition & 1 deletion test/openssl/test_pkey_ec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def test_sign_verify
signature = p256.sign("SHA256", data)
assert_equal true, p256.verify("SHA256", signature, data)

signature0 = (<<~'end;').unpack("m")[0]
signature0 = (<<~'end;').unpack1("m")
MEQCIEOTY/hD7eI8a0qlzxkIt8LLZ8uwiaSfVbjX2dPAvN11AiAQdCYx56Fq
QdBp1B4sxJoA8jvODMMklMyBKVmudboA6A==
end;
Expand Down
2 changes: 1 addition & 1 deletion test/openssl/test_pkey_rsa.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def test_sign_verify
signature = rsa1024.sign("SHA256", data)
assert_equal true, rsa1024.verify("SHA256", signature, data)

signature0 = (<<~'end;').unpack("m")[0]
signature0 = (<<~'end;').unpack1("m")
oLCgbprPvfhM4pjFQiDTFeWI9Sk+Og7Nh9TmIZ/xSxf2CGXQrptlwo7NQ28+
WA6YQo8jPH4hSuyWIM4Gz4qRYiYRkl5TDMUYob94zm8Si1HxEiS9354tzvqS
zS8MLW2BtNPuTubMxTItHGTnOzo9sUg0LAHVFt8kHG2NfKAw/gQ=
Expand Down
2 changes: 1 addition & 1 deletion test/openssl/test_ssl_session.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_session
assert_match(/\A-----BEGIN SSL SESSION PARAMETERS-----/, pem)
assert_match(/-----END SSL SESSION PARAMETERS-----\Z/, pem)
pem.gsub!(/-----(BEGIN|END) SSL SESSION PARAMETERS-----/, '').gsub!(/[\r\n]+/m, '')
assert_equal(session.to_der, pem.unpack('m*')[0])
assert_equal(session.to_der, pem.unpack1('m'))
assert_not_nil(session.to_text)
}
end
Expand Down

0 comments on commit 8eb0715

Please sign in to comment.