Sha256: 68007b819cb65a32e25480257fa5fa2082b48469dda290be87d63034f95cfa8e

Contents?: true

Size: 1.86 KB

Versions: 16

Compression:

Stored size: 1.86 KB

Contents

require 'openssl'

module Net
  module SSH
    module Authentication
      # Public key fingerprinting utility module - internal not part of API.
      # This is included in pubkey classes and called from there. All RSA, DSA, and ECC keys
      # are supported.
      #
      #     require 'net/ssh'
      #     my_pubkey_text = File.read('/path/to/id_ed25519.pub')
      #        #=> "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDB2NBh4GJPPUN1kXPMu8b633Xcv55WoKC3OkBjFAbzJ alice@example.com"
      #     my_pubkey = Net::SSH::KeyFactory.load_data_public_key(my_pubkey_text)
      #        #=> #<Net::SSH::Authentication::ED25519::PubKey:0x00007fc8e91819b0
      #     my_pubkey.fingerprint
      #        #=> "2f:7f:97:21:76:a4:0f:38:c4:fe:d8:b4:6a:39:72:30"
      #     my_pubkey.fingerprint('SHA256')
      #        #=> "SHA256:u6mXnY8P1b0FODGp8mckqOB33u8+jvkSCtJbD5Q9klg"
      module PubKeyFingerprint # :nodoc:
        # Return the key's fingerprint.  Algorithm may be either +MD5+ (default),
        # or +SHA256+. For +SHA256+, fingerprints are in the same format
        # returned by OpenSSH's <tt>`ssh-add -l -E SHA256`</tt>, i.e.,
        # trailing base64 padding '=' characters are stripped and the
        # literal string +SHA256:+ is prepended.
        def fingerprint(algorithm = 'MD5')
          @fingerprint ||= {}
          @fingerprint[algorithm] ||= PubKeyFingerprint.fingerprint(to_blob, algorithm)
        end

        def self.fingerprint(blob, algorithm = 'MD5')
          case algorithm.to_s.upcase
          when 'MD5'
            OpenSSL::Digest.hexdigest(algorithm, blob).scan(/../).join(":")
          when 'SHA256'
            "SHA256:#{Base64.encode64(OpenSSL::Digest.digest(algorithm, blob)).chomp.gsub(/=+\z/, '')}"
          else
            raise OpenSSL::Digest::DigestError, "unsupported ssh key digest #{algorithm}"
          end
        end
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 5 rubygems

Version Path
harbr-2.8.1 vendor/bundle/ruby/3.2.0/gems/net-ssh-7.2.1/lib/net/ssh/authentication/pub_key_fingerprint.rb
net-ssh-7.2.1 lib/net/ssh/authentication/pub_key_fingerprint.rb
net-ssh-7.2.1.rc1 lib/net/ssh/authentication/pub_key_fingerprint.rb
honeybadger-5.4.0 vendor/bundle/ruby/3.2.0/gems/net-ssh-7.2.0/lib/net/ssh/authentication/pub_key_fingerprint.rb
honeybadger-5.3.0 vendor/bundle/ruby/3.2.0/gems/net-ssh-7.2.0/lib/net/ssh/authentication/pub_key_fingerprint.rb
net-ssh-7.2.0 lib/net/ssh/authentication/pub_key_fingerprint.rb
net-ssh-7.2.0.rc1 lib/net/ssh/authentication/pub_key_fingerprint.rb
net-ssh-7.2.0.beta1 lib/net/ssh/authentication/pub_key_fingerprint.rb
net-ssh-7.1.0 lib/net/ssh/authentication/pub_key_fingerprint.rb
net-ssh-7.1.0.beta2 lib/net/ssh/authentication/pub_key_fingerprint.rb
net-ssh-7.1.0.beta1 lib/net/ssh/authentication/pub_key_fingerprint.rb
net-ssh-clone-7.0.1 lib/net/ssh/authentication/pub_key_fingerprint.rb
net-ssh-7.0.1 lib/net/ssh/authentication/pub_key_fingerprint.rb
net-ssh-7.0.0 lib/net/ssh/authentication/pub_key_fingerprint.rb
net-ssh-7.0.0.beta1 lib/net/ssh/authentication/pub_key_fingerprint.rb
eximius-net-ssh-6.3.1 lib/net/ssh/authentication/pub_key_fingerprint.rb