class RbNaCl::Auth
Secret Key Authenticators
These provide a means of verifying the integrity of a message, but only with the knowledge of a shared key. This can be a preshared key, or one that is derived through some cryptographic protocol.
Constants
- BYTES
Number of bytes in a valid authenticator
- KEYBYTES
Number of bytes in a valid key
Attributes
Public Class Methods
Compute authenticator for message
@param [#to_str] key the key used for the authenticator @param [#to_str] message message to construct an authenticator for
@return [String] The authenticator, as raw bytes
# File lib/rbnacl/auth.rb, line 33 def self.auth(key, message) new(key).auth(message) end
The number of key bytes for this Auth
class
@return [Integer] number of key bytes
# File lib/rbnacl/auth.rb, line 88 def self.key_bytes self::KEYBYTES end
A new authenticator, ready for auth and verification
@param [#to_str] key the key used for authenticators, 32 bytes.
# File lib/rbnacl/auth.rb, line 23 def initialize(key) @key = Util.check_string(key, key_bytes, "#{self.class} key") end
The number bytes in the tag or authenticator from this Auth
class
@return [Integer] number of tag bytes
# File lib/rbnacl/auth.rb, line 102 def self.tag_bytes self::BYTES end
Verifies the given authenticator with the message.
@param [#to_str] key the key used for the authenticator @param [#to_str] authenticator to be checked @param [#to_str] message the message to be authenticated
@raise [BadAuthenticatorError] if the tag isn't valid @raise [LengthError] if the tag is of the wrong length
@return [Boolean] Was it valid?
# File lib/rbnacl/auth.rb, line 47 def self.verify(key, authenticator, message) new(key).verify(authenticator, message) end
Public Instance Methods
Compute authenticator for message
@param [#to_str] message the message to authenticate
@return [String] the authenticator as raw bytes
# File lib/rbnacl/auth.rb, line 56 def auth(message) authenticator = Util.zeros(tag_bytes) message = message.to_str compute_authenticator(authenticator, message) authenticator end
The number of key bytes for this Auth
instance
@return [Integer] number of key bytes
# File lib/rbnacl/auth.rb, line 95 def key_bytes self.class.key_bytes end
The crypto primitive for this authenticator instance
@return [Symbol] The primitive used
# File lib/rbnacl/auth.rb, line 81 def primitive self.class.primitive end
The number of bytes in the tag or authenticator for this Auth
instance
@return [Integer] number of tag bytes
# File lib/rbnacl/auth.rb, line 109 def tag_bytes self.class.tag_bytes end
Verifies the given authenticator with the message.
@param [#to_str] authenticator to be checked @param [#to_str] message the message to be authenticated
@raise [BadAuthenticatorError] if the tag isn't valid @raise [LengthError] if the tag is of the wrong length
@return [Boolean] Was it valid?
# File lib/rbnacl/auth.rb, line 72 def verify(authenticator, message) auth = authenticator.to_s Util.check_length(auth, tag_bytes, "Provided authenticator") verify_message(auth, message) || raise(BadAuthenticatorError, "Invalid authenticator provided, message is corrupt") end
Private Instance Methods
# File lib/rbnacl/auth.rb, line 115 def compute_authenticator(_authenticator, _message) raise NotImplementedError end
# File lib/rbnacl/auth.rb, line 119 def verify_message(_authenticator, _message) raise NotImplementedError end