Class: FileChecksum

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant/util/file_checksum.rb

Constant Summary collapse

BUFFER_SIZE =
1024

Instance Method Summary collapse

Constructor Details

#initialize(path, digest_klass) ⇒ FileChecksum

Initializes an object to calculate the checksum of a file. The given “digest_klass“ should implement the “DigestClass“ interface. Note that the built-in Ruby digest classes duck type this properly: Digest::MD5, Digest::SHA1, etc.



17
18
19
20
# File 'lib/vagrant/util/file_checksum.rb', line 17

def initialize(path, digest_klass)
  @digest_klass = digest_klass
  @path   = path
end

Instance Method Details

#checksumString

This calculates the checksum of the file and returns it as a string.

Returns:

  • (String)


26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/vagrant/util/file_checksum.rb', line 26

def checksum
  digest= @digest_klass.new

  File.open(@path, "r") do |f|
    while !f.eof
      buf = f.readpartial(BUFFER_SIZE)
      digest.update(buf)
    end
  end

  return digest.hexdigest
end