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
-
#compute_digest(io, chunk_size: 1 << 16) ⇒ String
private
Calculates the SHA256 checksum of a given IO object.
-
#prefix_sha256(digest) ⇒ String?
private
Ensures a SHA256 digest is prefixed with 'sha256:'.
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.
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 = { 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 }, } .(total:, label:, message:) digest = Digest::SHA256.new until io.eof? .progress(by: chunk_size) digest << io.read(chunk_size) end .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.
50 51 52 |
# File 'lib/ollama/digester.rb', line 50 def prefix_sha256(digest) digest&.sub(/\A(?=\h{64}\z)/, 'sha256:') end |