Module: PQCrypto::Key
- Defined in:
- lib/pq_crypto/key.rb
Class Method Summary collapse
- .from_der(der, passphrase: nil, require_encrypted: false) ⇒ Object
- .from_pem(pem, passphrase: nil, require_encrypted: false) ⇒ Object
- .generate(algorithm) ⇒ Object
Class Method Details
.from_der(der, passphrase: nil, require_encrypted: false) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/pq_crypto/key.rb', line 32 def from_der(der, passphrase: nil, require_encrypted: false) require_encrypted = Internal.strict_boolean!(require_encrypted, name: "require_encrypted") public_key_from_spki_der(der) rescue SerializationError => spki_error begin secret_key_from_pkcs8_der(der, passphrase: passphrase, require_encrypted: require_encrypted) rescue SerializationError => pkcs8_error raise SerializationError, "Unable to decode DER as SPKI or PKCS#8 (SPKI: #{spki_error.}; PKCS#8: #{pkcs8_error.})" end end |
.from_pem(pem, passphrase: nil, require_encrypted: false) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/pq_crypto/key.rb', line 20 def from_pem(pem, passphrase: nil, require_encrypted: false) require_encrypted = Internal.strict_boolean!(require_encrypted, name: "require_encrypted") 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, require_encrypted: require_encrypted) 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 |