Class: Sodium::OneTimeAuth

Inherits:
Object
  • Object
show all
Includes:
Delegate
Defined in:
lib/sodium/one_time_auth.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Delegate

class_methods, included, #primitive

Constructor Details

#initialize(key) ⇒ OneTimeAuth

Returns a new instance of OneTimeAuth.



10
11
12
# File 'lib/sodium/one_time_auth.rb', line 10

def initialize(key)
  @key = self.class._key(key)
end

Class Method Details

.keyObject



6
7
8
# File 'lib/sodium/one_time_auth.rb', line 6

def self.key
  Sodium::Buffer.key self.implementation[:KEYBYTES]
end

Instance Method Details

#one_time_auth(message) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/sodium/one_time_auth.rb', line 14

def one_time_auth(message)
  message = self.class._message(message)

  Sodium::Buffer.empty self.implementation[:BYTES] do |authenticator|
    self.implementation.nacl(
      authenticator.to_ptr,
      message      .to_ptr,
      message      .bytesize,
      @key         .to_ptr
    ) or raise Sodium::CryptoError, 'failed to generate an authenticator'
  end
end

#verify(message, authenticator) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/sodium/one_time_auth.rb', line 27

def verify(message, authenticator)
  message       = self.class._message(message)
  authenticator = self.class._authenticator(authenticator)

  self.implementation.nacl_verify(
    authenticator.to_ptr,
    message      .to_ptr,
    message      .bytesize,
    @key         .to_ptr
  )
end