Class: SSHScan::Crypto::PublicKey

Inherits:
Object
  • Object
show all
Defined in:
lib/ssh_scan/public_key.rb

Overview

House methods helpful in analysing SSH public keys.

Instance Method Summary collapse

Constructor Details

#initialize(key_string) ⇒ PublicKey

Returns a new instance of PublicKey.



10
11
12
# File 'lib/ssh_scan/public_key.rb', line 10

def initialize(key_string)
  @key_string = key_string
end

Instance Method Details

#fingerprint_md5Object



36
37
38
# File 'lib/ssh_scan/public_key.rb', line 36

def fingerprint_md5
  SSHKey.fingerprint(@key_string)
end

#fingerprint_sha1Object



40
41
42
# File 'lib/ssh_scan/public_key.rb', line 40

def fingerprint_sha1
  SSHKey.sha1_fingerprint(@key_string)
end

#fingerprint_sha256Object



44
45
46
47
# File 'lib/ssh_scan/public_key.rb', line 44

def fingerprint_sha256
  # We're translating this to hex because the SSHKEY default isn't as useful for comparing with SSHFP records
  Base64.decode64(SSHKey.sha256_fingerprint(@key_string)).hexify(:delim => ":")
end

#lengthObject



32
33
34
# File 'lib/ssh_scan/public_key.rb', line 32

def length
  SSHKey.ssh_public_key_bits(@key_string)
end

#to_hashObject



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ssh_scan/public_key.rb', line 49

def to_hash
  {
    self.type => {
      "raw" => @key_string,
      "length" => self.length,
      "fingerprints" => {
        "md5" => self.fingerprint_md5,
        "sha1" => self.fingerprint_sha1,
        "sha256" => self.fingerprint_sha256
      }
    }
  }
end

#typeObject



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ssh_scan/public_key.rb', line 18

def type
  if @key_string.start_with?("ssh-rsa")
    return "rsa"
  elsif @key_string.start_with?("ssh-dss")
    return "dsa"
  elsif @key_string.start_with?("ecdsa-sha2-nistp256")
    return "ecdsa-sha2-nistp256"
  elsif @key_string.start_with?("ssh-ed25519")
    return "ed25519"
  else
    return "unknown"
  end 
end

#valid?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/ssh_scan/public_key.rb', line 14

def valid?
  SSHKey.valid_ssh_public_key?(@key_string)
end