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.



265
266
267
268
269
# File 'lib/pq_crypto/signature.rb', line 265

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

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



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

def algorithm
  @algorithm
end

Instance Method Details

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



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

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

#fingerprintObject



327
328
329
# File 'lib/pq_crypto/signature.rb', line 327

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

#hashObject



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

def hash
  fingerprint.hash
end

#to_bytesObject



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

def to_bytes
  @bytes.dup
end

#to_pqc_container_derObject



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

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

#to_pqc_container_pemObject



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

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

#to_spki_derObject



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

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

#to_spki_pemObject



287
288
289
# File 'lib/pq_crypto/signature.rb', line 287

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

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



291
292
293
294
295
296
297
298
# File 'lib/pq_crypto/signature.rb', line 291

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



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

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



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

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



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

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