Module: PQCrypto::Key

Defined in:
lib/pq_crypto/key.rb

Class Method Summary collapse

Class Method Details

.from_der(der, passphrase: nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/pq_crypto/key.rb', line 31

def from_der(der, passphrase: nil)
  public_key_from_spki_der(der)
rescue SerializationError => spki_error
  begin
    secret_key_from_pkcs8_der(der, passphrase: passphrase)
  rescue SerializationError => pkcs8_error
    raise SerializationError,
          "Unable to decode DER as SPKI or PKCS#8 (SPKI: #{spki_error.message}; PKCS#8: #{pkcs8_error.message})"
  end
end

.from_pem(pem, passphrase: nil) ⇒ Object



20
21
22
23
24
25
26
27
28
29
# File 'lib/pq_crypto/key.rb', line 20

def from_pem(pem, passphrase: nil)
  text = String(pem)
  if text.include?(SPKI::PEM_BEGIN)
    public_key_from_spki_pem(text)
  elsif text.include?(PKCS8::PEM_BEGIN) || text.include?(PKCS8::ENCRYPTED_PEM_BEGIN)
    secret_key_from_pkcs8_pem(text, passphrase: passphrase)
  else
    raise SerializationError, "Unsupported PEM label for PQCrypto::Key.from_pem"
  end
end

.generate(algorithm) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/pq_crypto/key.rb', line 6

def generate(algorithm)
  algorithm = resolve_algorithm!(algorithm)
  case AlgorithmRegistry.fetch(algorithm).fetch(:family)
  when :ml_kem
    KEM.generate(algorithm)
  when :ml_dsa
    Signature.generate(algorithm)
  when :ml_kem_hybrid
    HybridKEM.generate(algorithm)
  else
    raise UnsupportedAlgorithmError, "Unsupported key generation algorithm: #{algorithm.inspect}"
  end
end