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.



92
93
94
95
96
# File 'lib/pq_crypto/kem.rb', line 92

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

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



90
91
92
# File 'lib/pq_crypto/kem.rb', line 90

def algorithm
  @algorithm
end

Instance Method Details

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



122
123
124
# File 'lib/pq_crypto/kem.rb', line 122

def ==(other)
  other.is_a?(PublicKey) && other.algorithm == algorithm && other.to_bytes == @bytes
end

#encapsulateObject



110
111
112
113
114
115
# File 'lib/pq_crypto/kem.rb', line 110

def encapsulate
  ciphertext, shared_secret = PQCrypto.__send__(:native_ml_kem_encapsulate, @bytes)
  EncapsulationResult.new(ciphertext, shared_secret)
rescue ArgumentError => e
  raise InvalidKeyError, e.message
end

#encapsulate_to_bytesObject



117
118
119
120
# File 'lib/pq_crypto/kem.rb', line 117

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

#hashObject



128
129
130
# File 'lib/pq_crypto/kem.rb', line 128

def hash
  [self.class, algorithm, @bytes].hash
end

#to_bytesObject



98
99
100
# File 'lib/pq_crypto/kem.rb', line 98

def to_bytes
  @bytes.dup
end

#to_pqc_container_derObject



102
103
104
# File 'lib/pq_crypto/kem.rb', line 102

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

#to_pqc_container_pemObject



106
107
108
# File 'lib/pq_crypto/kem.rb', line 106

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