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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(algorithm, bytes, seed: nil) ⇒ SecretKey

Returns a new instance of SecretKey.



227
228
229
230
231
232
233
# File 'lib/pq_crypto/kem.rb', line 227

def initialize(algorithm, bytes, seed: nil)
  @algorithm = algorithm
  @bytes = Internal.binary_string(bytes)
  @seed = seed.nil? ? nil : Internal.binary_string(seed)
  validate_length!
  validate_seed_length! if @seed
end

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



225
226
227
# File 'lib/pq_crypto/kem.rb', line 225

def algorithm
  @algorithm
end

Class Method Details

.from_seed(algorithm, seed) ⇒ Object



235
236
237
238
239
240
241
# File 'lib/pq_crypto/kem.rb', line 235

def self.from_seed(algorithm, seed)
  seed_bytes = Internal.binary_string(seed)
  _public_key, expanded = PQCrypto.__send__(KEM.send(:native_method_for, algorithm, :keypair_from_seed), seed_bytes)
  new(algorithm, expanded, seed: seed_bytes)
rescue ArgumentError => e
  raise InvalidKeyError, e.message
end

Instance Method Details

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



275
276
277
278
# File 'lib/pq_crypto/kem.rb', line 275

def ==(other)
  return false unless other.is_a?(SecretKey) && other.algorithm == algorithm
  Internal.constant_time_equal?(other.send(:bytes_for_native), @bytes)
end

#decapsulate(ciphertext) ⇒ Object



263
264
265
266
267
# File 'lib/pq_crypto/kem.rb', line 263

def decapsulate(ciphertext)
  PQCrypto.__send__(KEM.send(:native_method_for, @algorithm, :decapsulate), Internal.binary_string(ciphertext), @bytes)
rescue ArgumentError => e
  raise InvalidCiphertextError, e.message
end

#hashObject



282
283
284
# File 'lib/pq_crypto/kem.rb', line 282

def hash
  object_id.hash
end

#inspectObject



286
287
288
# File 'lib/pq_crypto/kem.rb', line 286

def inspect
  "#<#{self.class}:0x#{object_id.to_s(16)} algorithm=#{algorithm.inspect}>"
end

#to_bytesObject



243
244
245
# File 'lib/pq_crypto/kem.rb', line 243

def to_bytes
  @bytes.dup
end

#to_pkcs8_der(format: :expanded, passphrase: nil, iterations: PKCS8::ENCRYPTED_PKCS8_DEFAULT_ITERATIONS) ⇒ Object



255
256
257
# File 'lib/pq_crypto/kem.rb', line 255

def to_pkcs8_der(format: :expanded, passphrase: nil, iterations: PKCS8::ENCRYPTED_PKCS8_DEFAULT_ITERATIONS)
  PKCS8.encode_der(@algorithm, pkcs8_material(format), format: format, passphrase: passphrase, iterations: iterations)
end

#to_pkcs8_pem(format: :expanded, passphrase: nil, iterations: PKCS8::ENCRYPTED_PKCS8_DEFAULT_ITERATIONS) ⇒ Object



259
260
261
# File 'lib/pq_crypto/kem.rb', line 259

def to_pkcs8_pem(format: :expanded, passphrase: nil, iterations: PKCS8::ENCRYPTED_PKCS8_DEFAULT_ITERATIONS)
  PKCS8.encode_pem(@algorithm, pkcs8_material(format), format: format, passphrase: passphrase, iterations: iterations)
end

#to_pqc_container_derObject



247
248
249
# File 'lib/pq_crypto/kem.rb', line 247

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

#to_pqc_container_pemObject



251
252
253
# File 'lib/pq_crypto/kem.rb', line 251

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

#wipe!Object



269
270
271
272
273
# File 'lib/pq_crypto/kem.rb', line 269

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