Class: PQCrypto::KEM::SecretKey

Inherits:
Object
  • Object
show all
Defined in:
lib/pq_crypto/kem.rb

Direct Known Subclasses

HybridKEM::SecretKey

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(algorithm, bytes) ⇒ SecretKey

Returns a new instance of SecretKey.



150
151
152
153
154
# File 'lib/pq_crypto/kem.rb', line 150

def initialize(algorithm, bytes)
  @algorithm = algorithm
  @bytes = String(bytes).b
  validate_length!
end

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



148
149
150
# File 'lib/pq_crypto/kem.rb', line 148

def algorithm
  @algorithm
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



179
180
181
182
# File 'lib/pq_crypto/kem.rb', line 179

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



168
169
170
171
172
# File 'lib/pq_crypto/kem.rb', line 168

def decapsulate(ciphertext)
  PQCrypto.__send__(:native_ml_kem_decapsulate, String(ciphertext).b, @bytes)
rescue ArgumentError => e
  raise InvalidCiphertextError, e.message
end

#fingerprintObject



190
191
192
# File 'lib/pq_crypto/kem.rb', line 190

def fingerprint
  Digest::SHA256.digest(@bytes)
end

#hashObject



186
187
188
# File 'lib/pq_crypto/kem.rb', line 186

def hash
  fingerprint.hash
end

#to_bytesObject



156
157
158
# File 'lib/pq_crypto/kem.rb', line 156

def to_bytes
  @bytes.dup
end

#to_pqc_container_derObject



160
161
162
# File 'lib/pq_crypto/kem.rb', line 160

def to_pqc_container_der
  Serialization.secret_key_to_pqc_container_der(@algorithm, @bytes)
end

#to_pqc_container_pemObject



164
165
166
# File 'lib/pq_crypto/kem.rb', line 164

def to_pqc_container_pem
  Serialization.secret_key_to_pqc_container_pem(@algorithm, @bytes)
end

#wipe!Object



174
175
176
177
# File 'lib/pq_crypto/kem.rb', line 174

def wipe!
  PQCrypto.secure_wipe(@bytes)
  self
end