Class: PQCrypto::Signature::PublicKey

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(algorithm, bytes) ⇒ PublicKey

Returns a new instance of PublicKey.



262
263
264
265
266
# File 'lib/pq_crypto/signature.rb', line 262

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

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



260
261
262
# File 'lib/pq_crypto/signature.rb', line 260

def algorithm
  @algorithm
end

Instance Method Details

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



310
311
312
313
# File 'lib/pq_crypto/signature.rb', line 310

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

#fingerprintObject



321
322
323
# File 'lib/pq_crypto/signature.rb', line 321

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

#hashObject



317
318
319
# File 'lib/pq_crypto/signature.rb', line 317

def hash
  fingerprint.hash
end

#to_bytesObject



268
269
270
# File 'lib/pq_crypto/signature.rb', line 268

def to_bytes
  @bytes.dup
end

#to_pqc_container_derObject



272
273
274
# File 'lib/pq_crypto/signature.rb', line 272

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

#to_pqc_container_pemObject



276
277
278
# File 'lib/pq_crypto/signature.rb', line 276

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

#to_spki_derObject



280
281
282
# File 'lib/pq_crypto/signature.rb', line 280

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

#to_spki_pemObject



284
285
286
# File 'lib/pq_crypto/signature.rb', line 284

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

#verify(message, signature) ⇒ Object



288
289
290
291
292
# File 'lib/pq_crypto/signature.rb', line 288

def verify(message, signature)
  PQCrypto.__send__(Signature.send(:native_method_for, @algorithm, :verify), String(message).b, String(signature).b, @bytes)
rescue ArgumentError => e
  raise InvalidKeyError, e.message
end

#verify!(message, signature) ⇒ Object



294
295
296
297
# File 'lib/pq_crypto/signature.rb', line 294

def verify!(message, signature)
  raise PQCrypto::VerificationError, "Verification failed" unless verify(message, signature)
  true
end

#verify_io(io, signature, chunk_size: 1 << 20, context: "".b) ⇒ Object



299
300
301
# File 'lib/pq_crypto/signature.rb', line 299

def verify_io(io, signature, chunk_size: 1 << 20, context: "".b)
  Signature.send(:_streaming_verify, self, io, signature, chunk_size, context)
end

#verify_io!(io, signature, chunk_size: 1 << 20, context: "".b) ⇒ Object



303
304
305
306
307
308
# File 'lib/pq_crypto/signature.rb', line 303

def verify_io!(io, signature, chunk_size: 1 << 20, context: "".b)
  unless verify_io(io, signature, chunk_size: chunk_size, context: context)
    raise PQCrypto::VerificationError, "Verification failed"
  end
  true
end