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.



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

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

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



153
154
155
# File 'lib/pq_crypto/kem.rb', line 153

def algorithm
  @algorithm
end

Instance Method Details

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



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

def ==(other)
  return false unless other.is_a?(PublicKey) && other.algorithm == algorithm
  PQCrypto.__send__(:native_ct_equals, other.to_bytes, @bytes)
end

#encapsulateObject



181
182
183
184
185
186
# File 'lib/pq_crypto/kem.rb', line 181

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



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

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

#fingerprintObject



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

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

#hashObject



200
201
202
# File 'lib/pq_crypto/kem.rb', line 200

def hash
  fingerprint.hash
end

#to_bytesObject



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

def to_bytes
  @bytes.dup
end

#to_pqc_container_derObject



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

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

#to_pqc_container_pemObject



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

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

#to_spki_derObject



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

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

#to_spki_pemObject



177
178
179
# File 'lib/pq_crypto/kem.rb', line 177

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