Class: PQCrypto::KEM::PublicKey
- Inherits:
-
Object
- Object
- PQCrypto::KEM::PublicKey
- Defined in:
- lib/pq_crypto/kem.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#algorithm ⇒ Object
readonly
Returns the value of attribute algorithm.
Instance Method Summary collapse
- #==(other) ⇒ Object (also: #eql?)
- #encapsulate ⇒ Object
- #encapsulate_to_bytes ⇒ Object
- #fingerprint ⇒ Object
- #hash ⇒ Object
-
#initialize(algorithm, bytes) ⇒ PublicKey
constructor
A new instance of PublicKey.
- #to_bytes ⇒ Object
- #to_pqc_container_der ⇒ Object
- #to_pqc_container_pem ⇒ Object
- #to_spki_der ⇒ Object
- #to_spki_pem ⇒ Object
- #valid? ⇒ Boolean
Constructor Details
#initialize(algorithm, bytes) ⇒ PublicKey
Returns a new instance of PublicKey.
175 176 177 178 179 180 |
# File 'lib/pq_crypto/kem.rb', line 175 def initialize(algorithm, bytes) @algorithm = algorithm @bytes = Internal.frozen_binary_string(bytes) validate_length! validate_structure! end |
Instance Attribute Details
#algorithm ⇒ Object (readonly)
Returns the value of attribute algorithm.
173 174 175 |
# File 'lib/pq_crypto/kem.rb', line 173 def algorithm @algorithm end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
224 225 226 227 |
# File 'lib/pq_crypto/kem.rb', line 224 def ==(other) return false unless other.is_a?(PublicKey) && other.algorithm == algorithm Internal.constant_time_equal?(other.send(:bytes_for_native), @bytes) end |
#encapsulate ⇒ Object
212 213 214 215 216 217 |
# File 'lib/pq_crypto/kem.rb', line 212 def encapsulate ciphertext, shared_secret = PQCrypto.__send__(KEM.send(:native_method_for, @algorithm, :encapsulate), @bytes) EncapsulationResult.new(ciphertext, shared_secret) rescue ArgumentError => e raise InvalidKeyError, e. end |
#encapsulate_to_bytes ⇒ Object
219 220 221 222 |
# File 'lib/pq_crypto/kem.rb', line 219 def encapsulate_to_bytes result = encapsulate [result.ciphertext, result.shared_secret] end |
#fingerprint ⇒ Object
235 236 237 |
# File 'lib/pq_crypto/kem.rb', line 235 def fingerprint Digest::SHA256.digest(@bytes) end |
#hash ⇒ Object
231 232 233 |
# File 'lib/pq_crypto/kem.rb', line 231 def hash fingerprint.hash end |
#to_bytes ⇒ Object
192 193 194 |
# File 'lib/pq_crypto/kem.rb', line 192 def to_bytes @bytes.dup end |
#to_pqc_container_der ⇒ Object
196 197 198 |
# File 'lib/pq_crypto/kem.rb', line 196 def to_pqc_container_der Serialization.public_key_to_pqc_container_der(@algorithm, @bytes) end |
#to_pqc_container_pem ⇒ Object
200 201 202 |
# File 'lib/pq_crypto/kem.rb', line 200 def to_pqc_container_pem Serialization.public_key_to_pqc_container_pem(@algorithm, @bytes) end |
#to_spki_der ⇒ Object
204 205 206 |
# File 'lib/pq_crypto/kem.rb', line 204 def to_spki_der SPKI.encode_der(@algorithm, @bytes) end |
#to_spki_pem ⇒ Object
208 209 210 |
# File 'lib/pq_crypto/kem.rb', line 208 def to_spki_pem SPKI.encode_pem(@algorithm, @bytes) end |
#valid? ⇒ Boolean
182 183 184 185 186 187 188 189 190 |
# File 'lib/pq_crypto/kem.rb', line 182 def valid? if @algorithm == :ml_kem_768_x25519_xwing mlkem_half = @bytes.byteslice(0, KEM.details(:ml_kem_768).fetch(:public_key_bytes)) return PQCrypto.__send__(:native_ml_kem_check_public_key, mlkem_half) end return true unless KEM.send(:checkable?, @algorithm) PQCrypto.__send__(KEM.send(:native_method_for, @algorithm, :check_public_key), @bytes) end |