summaryrefslogtreecommitdiff
path: root/lib/gitlab/key_fingerprint.rb
blob: d9a79f7c291292cd0480b637de10ff79f2761284 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module Gitlab
  class KeyFingerprint
    attr_reader :key, :ssh_key

    # Unqualified MD5 fingerprint for compatibility
    delegate :fingerprint, to: :ssh_key, allow_nil: true

    def initialize(key)
      @key = key

      @ssh_key =
        begin
          Net::SSH::KeyFactory.load_data_public_key(key)
        rescue Net::SSH::Exception, NotImplementedError
        end
    end

    def valid?
      ssh_key.present?
    end

    def type
      return unless valid?

      parts = ssh_key.ssh_type.split('-')
      parts.shift if parts[0] == 'ssh'

      parts[0].upcase
    end

    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
    end
  end
end