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

key[R]

Public Class Methods

auth(key, message) click to toggle source

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
key_bytes() click to toggle source

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
new(key) click to toggle source

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
tag_bytes() click to toggle source

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
verify(key, authenticator, message) click to toggle source

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

auth(message) click to toggle source

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
key_bytes() click to toggle source

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
primitive() click to toggle source

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
tag_bytes() click to toggle source

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
verify(authenticator, message) click to toggle source

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

compute_authenticator(_authenticator, _message) click to toggle source
# File lib/rbnacl/auth.rb, line 115
def compute_authenticator(_authenticator, _message)
  raise NotImplementedError
end
verify_message(_authenticator, _message) click to toggle source
# File lib/rbnacl/auth.rb, line 119
def verify_message(_authenticator, _message)
  raise NotImplementedError
end