Class: FileChecksum
- Inherits:
-
Object
- Object
- FileChecksum
- Defined in:
- lib/vagrant/util/file_checksum.rb
Constant Summary collapse
- BUFFER_SIZE =
1024
Instance Method Summary collapse
-
#checksum ⇒ String
This calculates the checksum of the file and returns it as a string.
-
#initialize(path, digest_klass) ⇒ FileChecksum
constructor
Initializes an object to calculate the checksum of a file.
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
#checksum ⇒ String
This calculates the checksum of the file and returns it as a 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 |