Class: RbNaCl::Signatures::Ed25519::VerifyKey

Inherits:
Object
  • Object
show all
Extended by:
RbNaCl::Sodium
Includes:
KeyComparator, RbNaCl::Serializable
Defined in:
lib/rbnacl/signatures/ed25519/verify_key.rb

Overview

The public key counterpart to an Ed25519 SigningKey for producing digital signatures. Like the name says, VerifyKeys can be used to verify that a given digital signature is authentic.

For more information on the Ed25519 digital signature system, please see the SigningKey documentation.

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RbNaCl::Sodium

sodium_constant, sodium_function, sodium_function_with_return_code, sodium_primitive, sodium_type

Methods included from RbNaCl::Serializable

#inspect, #to_s, #to_str

Methods included from KeyComparator

#<=>, #==

Constructor Details

#initialize(key) ⇒ RbNaCl::VerifyKey

Create a new VerifyKey object from a public key.

Parameters:

  • key (String)

    Ed25519 public key



31
32
33
34
# File 'lib/rbnacl/signatures/ed25519/verify_key.rb', line 31

def initialize(key)
  @key = key.to_str
  Util.check_length(@key, Ed25519::VERIFYKEYBYTES, "key")
end

Class Method Details

.signature_bytesInteger

The size of signatures verified by the VerifyKey class

Returns:

  • (Integer)

    The number of bytes in a signature



92
93
94
# File 'lib/rbnacl/signatures/ed25519/verify_key.rb', line 92

def self.signature_bytes
  Ed25519::SIGNATUREBYTES
end

Instance Method Details

#primitiveSymbol

The crypto primitive this VerifyKey class uses for signatures

Returns:

  • (Symbol)

    The primitive



85
86
87
# File 'lib/rbnacl/signatures/ed25519/verify_key.rb', line 85

def primitive
  self.class.primitive
end

#signature_bytesInteger

The size of signatures verified by the VerifyKey instance

Returns:

  • (Integer)

    The number of bytes in a signature



99
100
101
# File 'lib/rbnacl/signatures/ed25519/verify_key.rb', line 99

def signature_bytes
  Ed25519::SIGNATUREBYTES
end

#to_bytesString

Return the raw key in byte format

Returns:

  • (String)

    raw key as bytes



78
79
80
# File 'lib/rbnacl/signatures/ed25519/verify_key.rb', line 78

def to_bytes
  @key
end

#verify(signature, message) ⇒ Boolean

Verify a signature for a given message

Raises if the signature is invalid.

Parameters:

  • signature (String)

    Alleged signature to be checked

  • message (String)

    Message to be authenticated

Returns:

  • (Boolean)

    was the signature authentic?

Raises:



47
48
49
50
51
# File 'lib/rbnacl/signatures/ed25519/verify_key.rb', line 47

def verify(signature, message)
  signature = signature.to_str
  Util.check_length(signature, signature_bytes, "signature")
  verify_attached(signature + message)
end

#verify_attached(signed_message) ⇒ Boolean

Verify a signature for a given signed message

Raises if the signature is invalid.

Parameters:

  • signed_message (String)

    Message combined with signature to be authenticated

Returns:

  • (Boolean)

    was the signature authentic?

Raises:



62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rbnacl/signatures/ed25519/verify_key.rb', line 62

def verify_attached(signed_message)
  raise LengthError, "Signed message can not be nil" if signed_message.nil?
  raise LengthError, "Signed message can not be shorter than a signature" if signed_message.bytesize <= signature_bytes

  buffer = Util.zeros(signed_message.bytesize)
  buffer_len = Util.zeros(FFI::Type::LONG_LONG.size)

  success = self.class.sign_ed25519_open(buffer, buffer_len, signed_message, signed_message.bytesize, @key)
  raise(BadSignatureError, "signature was forged/corrupt") unless success

  true
end