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.



97
98
99
100
101
# File 'lib/pq_crypto/signature.rb', line 97

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

Instance Attribute Details

#algorithmObject (readonly)

Returns the value of attribute algorithm.



95
96
97
# File 'lib/pq_crypto/signature.rb', line 95

def algorithm
  @algorithm
end

Instance Method Details

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



130
131
132
# File 'lib/pq_crypto/signature.rb', line 130

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

#hashObject



136
137
138
# File 'lib/pq_crypto/signature.rb', line 136

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

#to_bytesObject



103
104
105
# File 'lib/pq_crypto/signature.rb', line 103

def to_bytes
  @bytes.dup
end

#to_pqc_container_derObject



107
108
109
# File 'lib/pq_crypto/signature.rb', line 107

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

#to_pqc_container_pemObject



111
112
113
# File 'lib/pq_crypto/signature.rb', line 111

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

#verify(message, signature) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/pq_crypto/signature.rb', line 115

def verify(message, signature)
  PQCrypto.__send__(:native_verify, String(message).b, String(signature).b, @bytes)
rescue PQCrypto::VerificationError
  false
rescue ArgumentError => e
  raise InvalidKeyError, e.message
end

#verify!(message, signature) ⇒ Object



123
124
125
126
127
128
# File 'lib/pq_crypto/signature.rb', line 123

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

  true
end