Skip to content

Commit 7c05781

Browse files
committed
Remove base64 runtime dependency
Starting in Ruby 3.3.0, `require "base64"` triggers a warning: > base64 was loaded from the standard library, but will no longer be > part of the default gems since Ruby 3.4.0. Add base64 to your Gemfile > or gemspec. Rather than introduce a new dependency, this commit removes the base64 requirement in favor of using `String#pack` and `String#unpack1` directly. (The base64 gem is just a thin wrapper around these methods.) I left base64 as a development dependency so that the existing tests could continue to use `Base64`, to confirm there were no regressions due to my changes.
1 parent 2e65064 commit 7c05781

File tree

4 files changed

+6
-5
lines changed

4 files changed

+6
-5
lines changed

lib/net/ssh/authentication/ed25519.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def self.read(datafull, password)
4646
raise ArgumentError.new("Expected #{MEND} at end of private key") unless datafull.end_with?(MEND)
4747

4848
datab64 = datafull[MBEGIN.size...-MEND.size]
49-
data = Base64.decode64(datab64)
49+
data = datab64.unpack1("m")
5050
raise ArgumentError.new("Expected #{MAGIC} at start of decoded private key") unless data.start_with?(MAGIC)
5151

5252
buffer = Net::SSH::Buffer.new(data[MAGIC.size + 1..-1])
@@ -134,7 +134,7 @@ def ssh_do_verify(sig, data, options = {})
134134

135135
def to_pem
136136
# TODO this is not pem
137-
ssh_type + Base64.encode64(@verify_key.to_bytes)
137+
ssh_type + [@verify_key.to_bytes].pack("m")
138138
end
139139
end
140140

lib/net/ssh/authentication/pub_key_fingerprint.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def self.fingerprint(blob, algorithm = 'MD5')
3232
when 'MD5'
3333
OpenSSL::Digest.hexdigest(algorithm, blob).scan(/../).join(":")
3434
when 'SHA256'
35-
"SHA256:#{Base64.encode64(OpenSSL::Digest.digest(algorithm, blob)).chomp.gsub(/=+\z/, '')}"
35+
"SHA256:#{[OpenSSL::Digest.digest(algorithm, blob)].pack('m').chomp.gsub(/=+\z/, '')}"
3636
else
3737
raise OpenSSL::Digest::DigestError, "unsupported ssh key digest #{algorithm}"
3838
end

lib/net/ssh/known_hosts.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,11 @@ def match(host, pattern)
241241
def known_host_hash?(hostlist, entries)
242242
if hostlist.size == 1 && hostlist.first =~ /\A\|1(\|.+){2}\z/
243243
chunks = hostlist.first.split(/\|/)
244-
salt = Base64.decode64(chunks[2])
244+
salt = chunks[2].unpack1("m")
245245
digest = OpenSSL::Digest.new('sha1')
246246
entries.each do |entry|
247247
hmac = OpenSSL::HMAC.digest(digest, salt, entry)
248-
return true if Base64.encode64(hmac).chomp == chunks[3]
248+
return true if [hmac].pack("m").chomp == chunks[3]
249249
end
250250
end
251251
false

net-ssh.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ Gem::Specification.new do |spec|
3838

3939
spec.add_development_dependency('rbnacl', '~> 7.1') unless ENV['NET_SSH_NO_RBNACL']
4040

41+
spec.add_development_dependency "base64"
4142
spec.add_development_dependency "bundler", ">= 1.17"
4243
spec.add_development_dependency "minitest", "~> 5.19"
4344
spec.add_development_dependency "mocha", "~> 2.1.0"

0 commit comments

Comments
 (0)