Sha256: bf348437badedff7b31841995a8aece51e5a305b875e08909fb0746ac623e4ce

Contents?: true

Size: 1.79 KB

Versions: 19

Compression:

Stored size: 1.79 KB

Contents

require 'openssl'
require 'sshkey'
require 'base64'

module SSHScan
  # All cryptography related methods.
  module Crypto
    # House methods helpful in analysing SSH public keys.
    class PublicKey
      def initialize(key)
        @key = key
      end

      # Is the current key known to be in our known bad key list
      # @return [Boolean] true if this {SSHScan::Crypto::PublicKey}
      #   instance's key is also in {SSHScan::Crypto}'s
      #   bad_public_keys, otherwise false
      def bad_key?
        SSHScan::Crypto.bad_public_keys.each do |other_key|
          if self.fingerprint_sha256 == other_key.fingerprint_sha256
            return true
          end
        end

        return false
      end

      # Generate MD5 fingerprint for this {SSHScan::Crypto::PublicKey} instance.
      # @return [String] formatted MD5 fingerprint
      def fingerprint_md5
        OpenSSL::Digest::MD5.hexdigest(::Base64.decode64(@key)).scan(/../).join(':')
      end

      # Generate SHA1 fingerprint for this {SSHScan::Crypto::PublicKey} instance.
      # @return [String] formatted SHA1 fingerprint
      def fingerprint_sha1
        OpenSSL::Digest::SHA1.hexdigest(::Base64.decode64(@key)).scan(/../).join(':')
      end

      # Generate SHA256 fingerprint for this {SSHScan::Crypto::PublicKey} instance.
      # @return [String] formatted SHA256 fingerprint
      def fingerprint_sha256
        OpenSSL::Digest::SHA256.hexdigest(::Base64.decode64(@key)).scan(/../).join(':')
      end
    end

    def self.bad_public_keys
      bad_keys = []

      Dir.glob("data/ssh-badkeys/host/*.key").each do |file_path|
        file = File.read(File.expand_path(file_path))
        key = SSHKey.new(file)
        bad_keys << SSHScan::Crypto::PublicKey.new(key.ssh_public_key.split[1])
      end

      return bad_keys
    end

  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
ssh_scan-0.0.38 lib/ssh_scan/crypto.rb
ssh_scan-0.0.38.pre lib/ssh_scan/crypto.rb
ssh_scan-0.0.37 lib/ssh_scan/crypto.rb
ssh_scan-0.0.36 lib/ssh_scan/crypto.rb
ssh_scan-0.0.35 lib/ssh_scan/crypto.rb
ssh_scan-0.0.34 lib/ssh_scan/crypto.rb
ssh_scan-0.0.33 lib/ssh_scan/crypto.rb
ssh_scan-0.0.32 lib/ssh_scan/crypto.rb
ssh_scan-0.0.31 lib/ssh_scan/crypto.rb
ssh_scan-0.0.30 lib/ssh_scan/crypto.rb
ssh_scan-0.0.29 lib/ssh_scan/crypto.rb
ssh_scan-0.0.28 lib/ssh_scan/crypto.rb
ssh_scan-0.0.27 lib/ssh_scan/crypto.rb
ssh_scan-0.0.26 lib/ssh_scan/crypto.rb
ssh_scan-0.0.25 lib/ssh_scan/crypto.rb
ssh_scan-0.0.24 lib/ssh_scan/crypto.rb
ssh_scan-0.0.23 lib/ssh_scan/crypto.rb
ssh_scan-0.0.22 lib/ssh_scan/crypto.rb
ssh_scan-0.0.21 lib/ssh_scan/crypto.rb