Class: SSHKeyHub::Processor::KeyProcessor

Inherits:
Object
  • Object
show all
Defined in:
lib/ssh_key_hub/processor/key_processor.rb

Overview

SSH public key analyzer

Instance Method Summary collapse

Instance Method Details

#key_bits(key, pkey = nil) ⇒ Integer

Returns key size in bits

Parameters:

  • key (String)

    public key data

  • pkey (OpenSSL::PKey) (defaults to: nil)

    loaded ssh key object (optional)

Returns:

  • (Integer)

    key size in bits



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/ssh_key_hub/processor/key_processor.rb', line 17

def key_bits(key, pkey = nil)
  pkey ||= Net::SSH::KeyFactory.load_data_public_key(key) rescue nil
  case pkey
  when OpenSSL::PKey::RSA
    return pkey.n.num_bits
  when OpenSSL::PKey::DSA
    return pkey.pub_key.num_bits
  when OpenSSL::PKey::EC
    return pkey.group.degree
  else
    # Currently Ed25519 keys aren't supported by Ruby's OpenSSL library
    puts "[KeyProcessor] Unknown key type for: #{key}"
    return -1
  end
end

#key_type(key, pkey = nil) ⇒ Symbol

Returns key type, currently :RSA, :DSA, :EC, :UNKNOWN

Parameters:

  • key (String)

    public key data

  • pkey (OpenSSL::PKey) (defaults to: nil)

    loaded ssh key object (optional)

Returns:

  • (Symbol)

    key type, currently :RSA, :DSA, :EC, :UNKNOWN



9
10
11
12
13
# File 'lib/ssh_key_hub/processor/key_processor.rb', line 9

def key_type(key, pkey = nil)
  pkey ||= Net::SSH::KeyFactory.load_data_public_key(key) rescue nil
  return :UNKNOWN if pkey.nil?
  :#{pkey.class.name.split('::').last}"
end

#key_type_and_bits(key, pkey = nil) ⇒ Array<Symbol, Integer>

Returns Array of key type and size in bits, eg. [:RSA, 4096]

Parameters:

  • key (String)

    public key data

  • pkey (OpenSSL::PKey) (defaults to: nil)

    loaded ssh key object (optional)

Returns:

  • (Array<Symbol, Integer>)

    Array of key type and size in bits, eg. [:RSA, 4096]



35
36
37
38
# File 'lib/ssh_key_hub/processor/key_processor.rb', line 35

def key_type_and_bits(key, pkey = nil)
  pkey ||= Net::SSH::KeyFactory.load_data_public_key(key) rescue nil
  [key_type(key, pkey), key_bits(key, pkey)]
end