gitlab-org--gitlab-foss/lib/gitlab/key_fingerprint.rb

49 lines
913 B
Ruby
Raw Normal View History

2015-04-14 10:00:43 +00:00
module Gitlab
class KeyFingerprint
attr_reader :key, :ssh_key
2015-04-14 10:00:43 +00:00
# Unqualified MD5 fingerprint for compatibility
delegate :fingerprint, to: :ssh_key, allow_nil: true
2015-04-14 10:00:43 +00:00
def initialize(key)
@key = key
@ssh_key =
begin
Net::SSH::KeyFactory.load_data_public_key(key)
rescue Net::SSH::Exception, NotImplementedError
end
2015-04-14 10:00:43 +00:00
end
def valid?
ssh_key.present?
end
2015-04-14 10:00:43 +00:00
def type
return unless valid?
2015-04-14 10:00:43 +00:00
parts = ssh_key.ssh_type.split('-')
parts.shift if parts[0] == 'ssh'
2015-04-14 10:00:43 +00:00
parts[0].upcase
end
2015-04-14 10:00:43 +00: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 10:00:43 +00:00
end
end
end