Module: Ollama::Digester

Included in:
Commands::BlobExists, Commands::PushBlob
Defined in:
lib/ollama/digester.rb

Overview

Provides utility methods for computing SHA256 digests of binary streams. This module is designed as a mixin to provide consistent hashing logic across different components, ensuring that IO objects are handled safely and efficiently.

Instance Method Summary collapse

Instance Method Details

#compute_digest(io, chunk_size: 1 << 16) ⇒ String (private)

Calculates the SHA256 checksum of a given IO object.

The method ensures the stream is reset both before and after processing to maintain the state of the IO object. It reads the content in chunks to minimize memory footprint when dealing with large binary files.

Parameters:

  • io (IO)

    the binary data stream to be hashed

  • chunk_size (Integer) (defaults to: 1 << 16)

    the size of chunks to read from the IO stream (default: 16384)

Returns:

  • (String)

    the resulting SHA256 digest formatted as 'sha256:'



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ollama/digester.rb', line 20

def compute_digest(io, chunk_size: 1 << 16)
  io.binmode
  io.rewind
  label    = 'Computing Digest…'
  total    = io.size
  message  = {
    format: '%l %c/%t in %te, ETA %e @%E %s',
    '%c'  => { format: '%.2f %U', unit: ?B, prefix: :iec_uc },
    '%t'  => { format: '%2.2f %U', unit: ?B, prefix: :iec_uc },
    '%s'  => { frames: :braille181 },
  }
  infobar.(total:, label:, message:)
  digest = Digest::SHA256.new
  until io.eof?
    infobar.progress(by: chunk_size)
    digest << io.read(chunk_size)
  end
  infobar.newline
  'sha256:%s' % digest.hexdigest
ensure
  io.rewind
end

#prefix_sha256(digest) ⇒ String? (private)

Ensures a SHA256 digest is prefixed with 'sha256:'.

If the provided string consists of exactly 64 hexadecimal characters, the prefix is prepended. Otherwise, the original string is returned.

Parameters:

  • digest (String, nil)

    the digest to be prefixed

Returns:

  • (String, nil)

    the prefixed digest or the original value



50
51
52
# File 'lib/ollama/digester.rb', line 50

def prefix_sha256(digest)
  digest&.sub(/\A(?=\h{64}\z)/, 'sha256:')
end