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.



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

#algorithmObject (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.message
end

#hashObject



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

def hash
  object_id.hash
end

#inspectObject



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_bytesObject



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_derObject



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_pemObject



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