Module: CsrPeek::KeyFacts

Defined in:
lib/csr_peek/key_facts.rb

Overview

Pure, nil-safe extraction of the plain facts about a public key: its type, size, and curve. This runs once, at build time, against a key that may have failed to load (nil). Turning the key into flat data here is what lets the value objects answer every downstream question without ever touching the raise-prone key-loading path again.

Class Method Summary collapse

Class Method Details

.bits_of(pkey) ⇒ Object

Size in bits: RSA modulus, DSA prime, or EC curve degree. nil otherwise.



35
36
37
38
39
40
41
42
43
# File 'lib/csr_peek/key_facts.rb', line 35

def bits_of(pkey)
  case pkey
  when OpenSSL::PKey::RSA then pkey.n&.num_bits
  when OpenSSL::PKey::DSA then pkey.p&.num_bits
  when OpenSSL::PKey::EC then pkey.group&.degree
  end
rescue OpenSSL::PKey::PKeyError
  nil
end

.curve_of(pkey) ⇒ Object

The EC curve name (e.g. "prime256v1"), or nil for non-EC keys.



46
47
48
49
50
51
52
# File 'lib/csr_peek/key_facts.rb', line 46

def curve_of(pkey)
  return nil unless pkey.is_a?(OpenSSL::PKey::EC)

  pkey.group&.curve_name
rescue OpenSSL::PKey::PKeyError
  nil
end

.of(pkey) ⇒ Object

=> { type: String, bits: Integer or nil, curve: String or nil }



15
16
17
# File 'lib/csr_peek/key_facts.rb', line 15

def of(pkey)
  {type: type_of(pkey), bits: bits_of(pkey), curve: curve_of(pkey)}
end

.type_of(pkey) ⇒ Object

"RSA", "EC", "DSA", an upper-cased OID for Edwards keys ("ED25519"), or "unknown" when the key could not be loaded.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/csr_peek/key_facts.rb', line 21

def type_of(pkey)
  case pkey
  when nil then "unknown"
  when OpenSSL::PKey::RSA then "RSA"
  when OpenSSL::PKey::EC then "EC"
  when OpenSSL::PKey::DSA then "DSA"
  else
    (pkey.respond_to?(:oid) ? pkey.oid : pkey.class.name.split("::").last).to_s
  end
rescue OpenSSL::PKey::PKeyError, NoMethodError
  "unknown"
end