2015-04-14 06:00:43 -04:00
|
|
|
module Gitlab
|
|
|
|
class KeyFingerprint
|
2017-08-03 08:19:11 -04:00
|
|
|
attr_reader :key, :ssh_key
|
2015-04-14 06:00:43 -04:00
|
|
|
|
2017-08-03 08:19:11 -04:00
|
|
|
# Unqualified MD5 fingerprint for compatibility
|
|
|
|
delegate :fingerprint, to: :ssh_key, allow_nil: true
|
2015-04-14 06:00:43 -04:00
|
|
|
|
|
|
|
def initialize(key)
|
|
|
|
@key = key
|
|
|
|
|
2017-08-03 08:19:11 -04:00
|
|
|
@ssh_key =
|
|
|
|
begin
|
|
|
|
Net::SSH::KeyFactory.load_data_public_key(key)
|
|
|
|
rescue Net::SSH::Exception, NotImplementedError
|
|
|
|
end
|
2015-04-14 06:00:43 -04:00
|
|
|
end
|
|
|
|
|
2017-08-03 08:19:11 -04:00
|
|
|
def valid?
|
|
|
|
ssh_key.present?
|
|
|
|
end
|
2015-04-14 06:00:43 -04:00
|
|
|
|
2017-08-03 08:19:11 -04:00
|
|
|
def type
|
|
|
|
return unless valid?
|
2015-04-14 06:00:43 -04:00
|
|
|
|
2017-08-03 08:19:11 -04:00
|
|
|
parts = ssh_key.ssh_type.split('-')
|
|
|
|
parts.shift if parts[0] == 'ssh'
|
2015-04-14 06:00:43 -04:00
|
|
|
|
2017-08-03 08:19:11 -04:00
|
|
|
parts[0].upcase
|
|
|
|
end
|
2015-04-14 06:00:43 -04:00
|
|
|
|
2017-08-03 08:19:11 -04:00
|
|
|
def bits
|
|
|
|
return unless valid?
|
|
|
|
|
|
|
|
case type
|
|
|
|
when 'RSA'
|
|
|
|
ssh_key.n.num_bits
|
|
|
|
when 'DSS', 'DSA'
|
|
|
|
ssh_key.p.num_bits
|
|
|
|
when 'ECDSA'
|
|
|
|
ssh_key.group.order.num_bits
|
|
|
|
when 'ED25519'
|
|
|
|
256
|
|
|
|
else
|
|
|
|
raise "Unsupported key type: #{type}"
|
|
|
|
end
|
2015-04-14 06:00:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|