debian-mirror-gitlab/lib/gitlab/key_fingerprint.rb

49 lines
913 B
Ruby
Raw Normal View History

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