Class: PQCrypto::KEM::SecretKey
- Inherits:
-
Object
- Object
- PQCrypto::KEM::SecretKey
- 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?)
- #decapsulate(ciphertext) ⇒ Object
- #hash ⇒ Object
-
#initialize(algorithm, bytes) ⇒ SecretKey
constructor
A new instance of SecretKey.
- #inspect ⇒ Object
- #to_bytes ⇒ Object
- #to_pkcs8_der(format: :expanded) ⇒ Object
- #to_pkcs8_pem(format: :expanded) ⇒ Object
- #to_pqc_container_der ⇒ Object
- #to_pqc_container_pem ⇒ Object
- #wipe! ⇒ Object
Constructor Details
#initialize(algorithm, bytes) ⇒ SecretKey
Returns a new instance of SecretKey.
219 220 221 222 223 |
# File 'lib/pq_crypto/kem.rb', line 219 def initialize(algorithm, bytes) @algorithm = algorithm @bytes = String(bytes).b validate_length! end |
Instance Attribute Details
#algorithm ⇒ Object (readonly)
Returns the value of attribute algorithm.
217 218 219 |
# File 'lib/pq_crypto/kem.rb', line 217 def algorithm @algorithm end |
Instance Method Details
#==(other) ⇒ Object Also known as: eql?
270 271 272 273 |
# File 'lib/pq_crypto/kem.rb', line 270 def ==(other) return false unless other.is_a?(SecretKey) && other.algorithm == algorithm PQCrypto.__send__(:native_ct_equals, other.to_bytes, @bytes) end |
#decapsulate(ciphertext) ⇒ Object
259 260 261 262 263 |
# File 'lib/pq_crypto/kem.rb', line 259 def decapsulate(ciphertext) PQCrypto.__send__(KEM.send(:native_method_for, @algorithm, :decapsulate), String(ciphertext).b, @bytes) rescue ArgumentError => e raise InvalidCiphertextError, e. end |
#hash ⇒ Object
277 278 279 |
# File 'lib/pq_crypto/kem.rb', line 277 def hash object_id.hash end |
#inspect ⇒ Object
281 282 283 |
# File 'lib/pq_crypto/kem.rb', line 281 def inspect "#<#{self.class}:0x#{object_id.to_s(16)} algorithm=#{algorithm.inspect}>" end |
#to_bytes ⇒ Object
225 226 227 |
# File 'lib/pq_crypto/kem.rb', line 225 def to_bytes @bytes.dup end |
#to_pkcs8_der(format: :expanded) ⇒ Object
237 238 239 240 241 242 243 244 245 246 |
# File 'lib/pq_crypto/kem.rb', line 237 def to_pkcs8_der(format: :expanded) case format when :expanded PKCS8.encode_der(@algorithm, @bytes, format: :expanded) when :seed, :both raise SerializationError, "PKCS#8 #{format.inspect} export from KEM::SecretKey requires original seed material" else raise SerializationError, "Unsupported PKCS#8 private key format: #{format.inspect}" end end |
#to_pkcs8_pem(format: :expanded) ⇒ Object
248 249 250 251 252 253 254 255 256 257 |
# File 'lib/pq_crypto/kem.rb', line 248 def to_pkcs8_pem(format: :expanded) case format when :expanded PKCS8.encode_pem(@algorithm, @bytes, format: :expanded) when :seed, :both raise SerializationError, "PKCS#8 #{format.inspect} export from KEM::SecretKey requires original seed material" else raise SerializationError, "Unsupported PKCS#8 private key format: #{format.inspect}" end end |
#to_pqc_container_der ⇒ Object
229 230 231 |
# File 'lib/pq_crypto/kem.rb', line 229 def to_pqc_container_der Serialization.secret_key_to_pqc_container_der(@algorithm, @bytes) end |
#to_pqc_container_pem ⇒ Object
233 234 235 |
# File 'lib/pq_crypto/kem.rb', line 233 def to_pqc_container_pem Serialization.secret_key_to_pqc_container_pem(@algorithm, @bytes) end |
#wipe! ⇒ Object
265 266 267 268 |
# File 'lib/pq_crypto/kem.rb', line 265 def wipe! PQCrypto.secure_wipe(@bytes) self end |