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.



94
95
96
97
98
# File 'lib/pq_crypto/kem.rb', line 94

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

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



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

def algorithm
  @algorithm
end

Instance Method Details

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



124
125
126
127
# File 'lib/pq_crypto/kem.rb', line 124

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

#encapsulateObject



112
113
114
115
116
117
# File 'lib/pq_crypto/kem.rb', line 112

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



119
120
121
122
# File 'lib/pq_crypto/kem.rb', line 119

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

#fingerprintObject



135
136
137
# File 'lib/pq_crypto/kem.rb', line 135

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

#hashObject



131
132
133
# File 'lib/pq_crypto/kem.rb', line 131

def hash
  fingerprint.hash
end

#to_bytesObject



100
101
102
# File 'lib/pq_crypto/kem.rb', line 100

def to_bytes
  @bytes.dup
end

#to_pqc_container_derObject



104
105
106
# File 'lib/pq_crypto/kem.rb', line 104

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

#to_pqc_container_pemObject



108
109
110
# File 'lib/pq_crypto/kem.rb', line 108

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