Skip to content

Commit 9b9a4a7

Browse files
Maumagnaguagnoanakinj
authored andcommitted
Prefer String#unpack1 (ruby#586)
String#unpack1 avoids the intermediate array created by String#unpack for single elements, while also making a call to Array#first/[0] unnecessary.
1 parent eb349a1 commit 9b9a4a7

File tree

8 files changed

+11
-11
lines changed

8 files changed

+11
-11
lines changed

lib/openssl/ssl.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ def initialize(svr, ctx)
494494
unless ctx.session_id_context
495495
# see #6137 - session id may not exceed 32 bytes
496496
prng = ::Random.new($0.hash)
497-
session_id = prng.bytes(16).unpack('H*')[0]
497+
session_id = prng.bytes(16).unpack1('H*')
498498
@ctx.session_id_context = session_id
499499
end
500500
@start_immediately = true

test/openssl/test_digest.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def test_digest_by_oid_and_name
6767
end
6868

6969
def encode16(str)
70-
str.unpack("H*").first
70+
str.unpack1("H*")
7171
end
7272

7373
def test_sha2

test/openssl/test_ns_spki.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ def test_build_data
3838
def test_decode_data
3939
spki = OpenSSL::Netscape::SPKI.new(@b64)
4040
assert_equal(@b64, spki.to_pem)
41-
assert_equal(@b64.unpack("m").first, spki.to_der)
41+
assert_equal(@b64.unpack1("m"), spki.to_der)
4242
assert_equal("MozillaIsMyFriend", spki.challenge)
4343
assert_equal(OpenSSL::PKey::RSA, spki.public_key.class)
4444

45-
spki = OpenSSL::Netscape::SPKI.new(@b64.unpack("m").first)
45+
spki = OpenSSL::Netscape::SPKI.new(@b64.unpack1("m"))
4646
assert_equal(@b64, spki.to_pem)
47-
assert_equal(@b64.unpack("m").first, spki.to_der)
47+
assert_equal(@b64.unpack1("m"), spki.to_der)
4848
assert_equal("MozillaIsMyFriend", spki.challenge)
4949
assert_equal(OpenSSL::PKey::RSA, spki.public_key.class)
5050
end

test/openssl/test_pkcs12.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ def test_create_with_mac_itr
181181
def test_new_with_no_keys
182182
# generated with:
183183
# openssl pkcs12 -certpbe PBE-SHA1-3DES -in <@mycert> -nokeys -export
184-
str = <<~EOF.unpack("m").first
184+
str = <<~EOF.unpack1("m")
185185
MIIGJAIBAzCCBeoGCSqGSIb3DQEHAaCCBdsEggXXMIIF0zCCBc8GCSqGSIb3
186186
DQEHBqCCBcAwggW8AgEAMIIFtQYJKoZIhvcNAQcBMBwGCiqGSIb3DQEMAQMw
187187
DgQIjv5c3OHvnBgCAggAgIIFiMJa8Z/w7errRvCQPXh9dGQz3eJaFq3S2gXD
@@ -230,7 +230,7 @@ def test_new_with_no_keys
230230
def test_new_with_no_certs
231231
# generated with:
232232
# openssl pkcs12 -inkey fixtures/openssl/pkey/rsa-1.pem -nocerts -export
233-
str = <<~EOF.unpack("m").first
233+
str = <<~EOF.unpack1("m")
234234
MIIJ7wIBAzCCCbUGCSqGSIb3DQEHAaCCCaYEggmiMIIJnjCCCZoGCSqGSIb3
235235
DQEHAaCCCYsEggmHMIIJgzCCCX8GCyqGSIb3DQEMCgECoIIJbjCCCWowHAYK
236236
KoZIhvcNAQwBAzAOBAjX5nN8jyRKwQICCAAEgglIBIRLHfiY1mNHpl3FdX6+

test/openssl/test_pkey_dsa.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def test_sign_verify
5858
signature = dsa512.sign("SHA256", data)
5959
assert_equal true, dsa512.verify("SHA256", signature, data)
6060

61-
signature0 = (<<~'end;').unpack("m")[0]
61+
signature0 = (<<~'end;').unpack1("m")
6262
MCwCFH5h40plgU5Fh0Z4wvEEpz0eE9SnAhRPbkRB8ggsN/vsSEYMXvJwjGg/
6363
6g==
6464
end;

test/openssl/test_pkey_ec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def test_sign_verify
107107
signature = p256.sign("SHA256", data)
108108
assert_equal true, p256.verify("SHA256", signature, data)
109109

110-
signature0 = (<<~'end;').unpack("m")[0]
110+
signature0 = (<<~'end;').unpack1("m")
111111
MEQCIEOTY/hD7eI8a0qlzxkIt8LLZ8uwiaSfVbjX2dPAvN11AiAQdCYx56Fq
112112
QdBp1B4sxJoA8jvODMMklMyBKVmudboA6A==
113113
end;

test/openssl/test_pkey_rsa.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ def test_sign_verify
8383
signature = rsa1024.sign("SHA256", data)
8484
assert_equal true, rsa1024.verify("SHA256", signature, data)
8585

86-
signature0 = (<<~'end;').unpack("m")[0]
86+
signature0 = (<<~'end;').unpack1("m")
8787
oLCgbprPvfhM4pjFQiDTFeWI9Sk+Og7Nh9TmIZ/xSxf2CGXQrptlwo7NQ28+
8888
WA6YQo8jPH4hSuyWIM4Gz4qRYiYRkl5TDMUYob94zm8Si1HxEiS9354tzvqS
8989
zS8MLW2BtNPuTubMxTItHGTnOzo9sUg0LAHVFt8kHG2NfKAw/gQ=

test/openssl/test_ssl_session.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def test_session
2222
assert_match(/\A-----BEGIN SSL SESSION PARAMETERS-----/, pem)
2323
assert_match(/-----END SSL SESSION PARAMETERS-----\Z/, pem)
2424
pem.gsub!(/-----(BEGIN|END) SSL SESSION PARAMETERS-----/, '').gsub!(/[\r\n]+/m, '')
25-
assert_equal(session.to_der, pem.unpack('m*')[0])
25+
assert_equal(session.to_der, pem.unpack1('m'))
2626
assert_not_nil(session.to_text)
2727
}
2828
end

0 commit comments

Comments
 (0)