Class: Botan::MAC

Inherits:
Object
  • Object
show all
Defined in:
lib/botan/mac.rb

Overview

Message Authentication Code

Examples

examples/mac.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(algo) ⇒ MAC

Returns a new instance of MAC.

Parameters:

  • algo (String)

    the MAC algorithm name

Raises:



19
20
21
22
23
24
25
26
# File 'lib/botan/mac.rb', line 19

def initialize(algo)
  flags = 0
  ptr = FFI::MemoryPointer.new(:pointer)
  Botan.call_ffi(:botan_mac_init, ptr, algo, flags)
  ptr = ptr.read_pointer
  raise Botan::Error, 'botan_mac_init returned NULL' if ptr.null?
  @ptr = FFI::AutoPointer.new(ptr, self.class.method(:destroy))
end

Class Method Details

.destroy(ptr) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



29
30
31
# File 'lib/botan/mac.rb', line 29

def self.destroy(ptr)
  LibBotan.botan_mac_destroy(ptr)
end

Instance Method Details

#digestObject



68
69
70
71
72
# File 'lib/botan/mac.rb', line 68

def digest
  out_buf = FFI::MemoryPointer.new(:uint8, output_length)
  Botan.call_ffi(:botan_mac_final, @ptr, out_buf)
  out_buf.read_bytes(out_buf.size)
end

#hexdigestObject



74
75
76
# File 'lib/botan/mac.rb', line 74

def hexdigest
  Botan.hex_encode(digest)
end

#inspectObject



78
79
80
# File 'lib/botan/mac.rb', line 78

def inspect
  Botan.inspect_ptr(self)
end

#key=(key) ⇒ Object

Sets the key for the MAC. This must be called before #update.

Parameters:

  • key (String)


55
56
57
# File 'lib/botan/mac.rb', line 55

def key=(key)
  Botan.call_ffi(:botan_mac_set_key, @ptr, key, key.bytesize)
end

#output_lengthInteger

Retrieve the output length of the MAC.

Returns:

  • (Integer)


45
46
47
48
49
# File 'lib/botan/mac.rb', line 45

def output_length
  length_ptr = FFI::MemoryPointer.new(:size_t)
  Botan.call_ffi(:botan_mac_output_length, @ptr, length_ptr)
  length_ptr.read(:size_t)
end

#resetself

Resets the instace back to a clean state, as if no key and input have been supplied.

Returns:

  • (self)


37
38
39
40
# File 'lib/botan/mac.rb', line 37

def reset
  Botan.call_ffi(:botan_mac_clear, @ptr)
  self
end

#update(data) ⇒ self Also known as: <<

Adds input to the MAC computation.

Parameters:

  • data (String)

Returns:

  • (self)


63
64
65
66
# File 'lib/botan/mac.rb', line 63

def update(data)
  Botan.call_ffi(:botan_mac_update, @ptr, data, data.bytesize)
  self
end