Class: PQCrypto::KEM::PublicKey

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

Direct Known Subclasses

HybridKEM::PublicKey

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#algorithmObject (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

#encapsulateObject



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.message
end

#encapsulate_to_bytesObject



219
220
221
222
# File 'lib/pq_crypto/kem.rb', line 219

def encapsulate_to_bytes
  result = encapsulate
  [result.ciphertext, result.shared_secret]
end

#fingerprintObject



235
236
237
# File 'lib/pq_crypto/kem.rb', line 235

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

#hashObject



231
232
233
# File 'lib/pq_crypto/kem.rb', line 231

def hash
  fingerprint.hash
end

#to_bytesObject



192
193
194
# File 'lib/pq_crypto/kem.rb', line 192

def to_bytes
  @bytes.dup
end

#to_pqc_container_derObject



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_pemObject



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_derObject



204
205
206
# File 'lib/pq_crypto/kem.rb', line 204

def to_spki_der
  SPKI.encode_der(@algorithm, @bytes)
end

#to_spki_pemObject



208
209
210
# File 'lib/pq_crypto/kem.rb', line 208

def to_spki_pem
  SPKI.encode_pem(@algorithm, @bytes)
end

#valid?Boolean

Returns:

  • (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