Class: RbNaCl::Hash::Blake2b

Inherits:
Object
  • Object
show all
Extended by:
Sodium
Defined in:
lib/rbnacl/hash/blake2b.rb

Overview

The Blake2b hash function

Blake2b is based on Blake, a SHA3 finalist which was snubbed in favor of Keccak, a much slower hash function but one sufficiently different from SHA2 to let the SHA3 judges panel sleep easy. Back in the real world, it'd be great if we can calculate hashes quickly if possible.

Blake2b provides for up to 64-bit digests and also supports a keyed mode similar to HMAC

Defined Under Namespace

Classes: State

Constant Summary collapse

EMPTY_PERSONAL =
("\0" * PERSONALBYTES).freeze
EMPTY_SALT =
("\0" * SALTBYTES).freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Sodium

primitive, sodium_constant, sodium_function, sodium_function_with_return_code, sodium_primitive, sodium_type

Constructor Details

#initialize(opts = {}) ⇒ RbNaCl::Hash::Blake2b

Create a new Blake2b hash object

Parameters:

  • opts (Hash) (defaults to: {})

    Blake2b configuration

Options Hash (opts):

  • :key (String)

    for Blake2b keyed mode

  • :digest_size (Integer)

    size of output digest in bytes

  • :salt (String)

    Provide a salt to support randomised hashing. This is mixed into the parameters block to start the hashing.

  • :personal (Personal)

    Provide personalisation string to allow pinning a hash for a particular purpose. This is mixed into the parameters block to start the hashing

Raises:



127
128
129
130
131
132
133
134
135
136
# File 'lib/rbnacl/hash/blake2b.rb', line 127

def initialize(opts = {})
  @key         = opts[:key]
  @key_size    = opts[:key_size]
  @digest_size = opts[:digest_size]
  @personal    = opts[:personal]
  @salt        = opts[:salt]

  @incycle  = false
  @instate  = nil
end

Class Method Details

.digest(message, options) ⇒ String

Calculate a Blake2b digest

Parameters:

  • message (String)

    Message to be hashed

  • options (Hash)

    Blake2b configuration

  • opts (Hash)

    a customizable set of options

Returns:

  • (String)

    Blake2b digest of the string as raw bytes

Raises:



60
61
62
63
64
65
66
67
# File 'lib/rbnacl/hash/blake2b.rb', line 60

def self.digest(message, options)
  opts = validate_opts(options)
  digest = Util.zeros(opts[:digest_size])
  generichash_blake2b(digest, opts[:digest_size], message, message.bytesize,
                      opts[:key], opts[:key_size], opts[:salt], opts[:personal]) ||
    raise(CryptoError, "Hashing failed!")
  digest
end

.new(opts = {}) ⇒ Object



109
110
111
112
# File 'lib/rbnacl/hash/blake2b.rb', line 109

def self.new(opts = {})
  opts = validate_opts(opts)
  super
end

Instance Method Details

#digestString

Finalize digest calculation, return cached digest if any

Returns:

  • (String)

    Blake2b digest of the string as raw bytes

Raises:



162
163
164
165
166
167
168
169
170
# File 'lib/rbnacl/hash/blake2b.rb', line 162

def digest
  raise(CryptoError, "No message to hash yet!") unless @incycle
  return @digest if @digest

  @digest = Util.zeros(@digest_size)
  self.class.generichash_blake2b_final(@instate.pointer, @digest, @digest_size) ||
    raise(CryptoError, "Hash finalization failed!")
  @digest
end

#resetObject

Initialize state for Blake2b hash calculation, this will be called automatically from #update if needed



140
141
142
143
144
145
146
147
# File 'lib/rbnacl/hash/blake2b.rb', line 140

def reset
  @instate.release if @instate
  @instate = State.new
  self.class.generichash_blake2b_init(@instate.pointer, @key, @key_size, @digest_size, @salt, @personal) ||
    raise(CryptoError, "Hash init failed!")
  @incycle = true
  @digest = nil
end

#update(message) ⇒ Object Also known as: <<

Reentrant version of Blake2b digest calculation method

Parameters:

  • message (String)

    Message to be hashed



152
153
154
155
156
# File 'lib/rbnacl/hash/blake2b.rb', line 152

def update(message)
  reset unless @incycle
  self.class.generichash_blake2b_update(@instate.pointer, message, message.bytesize) ||
    raise(CryptoError, "Hashing failed!")
end