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.



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

def initialize(algorithm, bytes)
  @algorithm = algorithm
  @bytes = Internal.frozen_binary_string(bytes)
  validate_length!
end

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



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

def algorithm
  @algorithm
end

Instance Method Details

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



330
331
332
333
# File 'lib/pq_crypto/signature.rb', line 330

def ==(other)
  return false unless other.is_a?(PublicKey) && other.algorithm == algorithm
  Internal.constant_time_equal?(other.send(:bytes_for_native), @bytes)
end

#fingerprintObject



341
342
343
# File 'lib/pq_crypto/signature.rb', line 341

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

#hashObject



337
338
339
# File 'lib/pq_crypto/signature.rb', line 337

def hash
  fingerprint.hash
end

#to_bytesObject



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

def to_bytes
  @bytes.dup
end

#to_pqc_container_derObject



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

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

#to_pqc_container_pemObject



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

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

#to_spki_derObject



297
298
299
# File 'lib/pq_crypto/signature.rb', line 297

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

#to_spki_pemObject



301
302
303
# File 'lib/pq_crypto/signature.rb', line 301

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

#verify(message, signature, context: "".b) ⇒ Object



305
306
307
308
309
310
311
312
# File 'lib/pq_crypto/signature.rb', line 305

def verify(message, signature, context: "".b)
  context = Signature.send(:validate_context!, context)
  begin
    PQCrypto.__send__(Signature.send(:native_method_for, @algorithm, :verify), Internal.binary_string(message), Internal.binary_string(signature), @bytes, context)
  rescue ArgumentError => e
    raise InvalidKeyError, e.message
  end
end

#verify!(message, signature, context: "".b) ⇒ Object



314
315
316
317
# File 'lib/pq_crypto/signature.rb', line 314

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

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



319
320
321
# File 'lib/pq_crypto/signature.rb', line 319

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



323
324
325
326
327
328
# File 'lib/pq_crypto/signature.rb', line 323

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