Class: Botan::Digest

Inherits:
Digest::Class
  • Object
show all
Defined in:
lib/botan/digest.rb

Overview

Class for calculating message digests using Botan's hash functions.

This should behave nearly identically to Digest and OpenSSL::Digest. Some differences are:

  • Algorithm names. Example: OpenSSL expects `RIPEMD160`, Botan uses `RIPEMD-160`.

  • OIDs. Not currently supported.

Examples

examples/digest.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(algo) ⇒ Digest

Returns a new instance of Digest

Raises:



30
31
32
33
34
35
36
37
38
# File 'lib/botan/digest.rb', line 30

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

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name



26
27
28
# File 'lib/botan/digest.rb', line 26

def name
  @name
end

Class Method Details

.digest(name, data) ⇒ Object



53
54
55
# File 'lib/botan/digest.rb', line 53

def self.digest(name, data)
  super(data, name)
end

Instance Method Details

#block_lengthInteger

Retrieve the block length for the hash.

Returns:

  • (Integer)


91
92
93
94
95
# File 'lib/botan/digest.rb', line 91

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

#digest_lengthInteger

Retrieve the length of the digest.

Returns:

  • (Integer)


100
101
102
103
104
# File 'lib/botan/digest.rb', line 100

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

#initialize_copy(source) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/botan/digest.rb', line 40

def initialize_copy(source)
  @name = source.name
  ptr = FFI::MemoryPointer.new(:pointer)
  Botan.call_ffi(:botan_hash_copy_state, ptr, source.ptr)
  ptr = ptr.read_pointer
  @ptr = FFI::AutoPointer.new(ptr, self.class.method(:destroy))
end

#resetself

Resets the instace back to a clean state, as if no data has been supplied.

Returns:

  • (self)


119
120
121
122
# File 'lib/botan/digest.rb', line 119

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

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

Adds input to the digest computation.

Parameters:

  • data (String)

Returns:

  • (self)


110
111
112
113
# File 'lib/botan/digest.rb', line 110

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