Class: Nuckle::Internals::Blake3::Hasher

Inherits:
Object
  • Object
show all
Defined in:
lib/nuckle/internals/blake3.rb

Overview

Streaming hasher. Supports update/finalize/finalize_xof.

Instance Method Summary collapse

Constructor Details

#initialize(key_words, flags) ⇒ Hasher

Returns a new instance of Hasher.

Parameters:

  • key_words (Array<Integer>)

    8-word base key (IV or actual key)

  • flags (Integer)

    base flags (0, KEYED_HASH, or DERIVE_KEY_MATERIAL)



147
148
149
150
151
152
153
154
155
# File 'lib/nuckle/internals/blake3.rb', line 147

def initialize(key_words, flags)
  @key_words         = key_words.freeze
  @flags             = flags
  @cv_stack          = []
  @chunk_counter     = 0
  @chunk_cv          = key_words.dup
  @chunk_block       = String.new(encoding: Encoding::BINARY)
  @blocks_compressed = 0
end

Instance Method Details

#finalize(out_len = OUT_LEN) ⇒ Object

Return ‘out_len` bytes of output (default 32).



183
184
185
# File 'lib/nuckle/internals/blake3.rb', line 183

def finalize(out_len = OUT_LEN)
  build_root_output.read(out_len)
end

#finalize_xofObject

Return an Output that can be read incrementally via ‘read(n)`.



188
189
190
# File 'lib/nuckle/internals/blake3.rb', line 188

def finalize_xof
  build_root_output
end

#update(input) ⇒ Object

Feed more input. Returns self for chaining.



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/nuckle/internals/blake3.rb', line 158

def update(input)
  input  = input.b
  len    = input.bytesize
  offset = 0

  while offset < len
    if @chunk_block.bytesize == BLOCK_LEN
      if @blocks_compressed < 15
        flush_intermediate_block
      else
        flush_final_chunk_block
        push_and_merge_chunk
        start_new_chunk
      end
    end

    need = BLOCK_LEN - @chunk_block.bytesize
    take = [need, len - offset].min
    @chunk_block << input.byteslice(offset, take)
    offset += take
  end
  self
end