Class: RbNaCl::Auth

Inherits:
Object
  • Object
show all
Defined in:
lib/rbnacl/auth.rb

Overview

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.

Constant Summary collapse

KEYBYTES =

Number of bytes in a valid key

0
BYTES =

Number of bytes in a valid authenticator

0

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ Auth

A new authenticator, ready for auth and verification

Parameters:

  • key (#to_str)

    the key used for authenticators, 32 bytes.



21
22
23
# File 'lib/rbnacl/auth.rb', line 21

def initialize(key)
  @key = Util.check_string(key, key_bytes, "#{self.class} key")
end

Class Method Details

.auth(key, message) ⇒ String

Compute authenticator for message

Parameters:

  • key (#to_str)

    the key used for the authenticator

  • message (#to_str)

    message to construct an authenticator for

Returns:

  • (String)

    The authenticator, as raw bytes



31
32
33
# File 'lib/rbnacl/auth.rb', line 31

def self.auth(key, message)
  new(key).auth(message)
end

.key_bytesInteger

The number of key bytes for this Auth class

Returns:

  • (Integer)

    number of key bytes



86
87
88
# File 'lib/rbnacl/auth.rb', line 86

def self.key_bytes
  self::KEYBYTES
end

.tag_bytesInteger

The number bytes in the tag or authenticator from this Auth class

Returns:

  • (Integer)

    number of tag bytes



100
101
102
# File 'lib/rbnacl/auth.rb', line 100

def self.tag_bytes
  self::BYTES
end

.verify(key, authenticator, message) ⇒ Boolean

Verifies the given authenticator with the message.

Parameters:

  • key (#to_str)

    the key used for the authenticator

  • authenticator (#to_str)

    to be checked

  • message (#to_str)

    the message to be authenticated

Returns:

  • (Boolean)

    Was it valid?

Raises:



45
46
47
# File 'lib/rbnacl/auth.rb', line 45

def self.verify(key, authenticator, message)
  new(key).verify(authenticator, message)
end

Instance Method Details

#auth(message) ⇒ String

Compute authenticator for message

Parameters:

  • message (#to_str)

    the message to authenticate

Returns:

  • (String)

    the authenticator as raw bytes



54
55
56
57
58
59
# File 'lib/rbnacl/auth.rb', line 54

def auth(message)
  authenticator = Util.zeros(tag_bytes)
  message = message.to_str
  compute_authenticator(authenticator, message)
  authenticator
end

#key_bytesInteger

The number of key bytes for this Auth instance

Returns:

  • (Integer)

    number of key bytes



93
94
95
# File 'lib/rbnacl/auth.rb', line 93

def key_bytes
  self.class.key_bytes
end

#primitiveSymbol

The crypto primitive for this authenticator instance

Returns:

  • (Symbol)

    The primitive used



79
80
81
# File 'lib/rbnacl/auth.rb', line 79

def primitive
  self.class.primitive
end

#tag_bytesInteger

The number of bytes in the tag or authenticator for this Auth instance

Returns:

  • (Integer)

    number of tag bytes



107
108
109
# File 'lib/rbnacl/auth.rb', line 107

def tag_bytes
  self.class.tag_bytes
end

#verify(authenticator, message) ⇒ Boolean

Verifies the given authenticator with the message.

Parameters:

  • authenticator (#to_str)

    to be checked

  • message (#to_str)

    the message to be authenticated

Returns:

  • (Boolean)

    Was it valid?

Raises:



70
71
72
73
74
# File 'lib/rbnacl/auth.rb', line 70

def verify(authenticator, message)
  auth = authenticator.to_s
  Util.check_length(auth, tag_bytes, "Provided authenticator")
  verify_message(auth, message) || fail(BadAuthenticatorError, "Invalid authenticator provided, message is corrupt")
end