More hashing refactoring to work with byte chunks

Signed-off-by: Adam Vandenberg <flangy@gmail.com>
This commit is contained in:
Jake Good 2010-03-27 10:41:24 -05:00 committed by Adam Vandenberg
parent 04855ddd48
commit 8ce7abce73
2 changed files with 22 additions and 14 deletions

View File

@ -155,15 +155,29 @@ class Pathname
nil
end
def md5
require 'digest'
incr_md5 = Digest::MD5.new
def incremental_hash(hasher)
incr_hash = hasher.new
self.open('r') do |f|
f.each_line do |line|
incr_md5 << line
while(buf = f.read(1024))
incr_hash << buf
end
end
incr_md5.hexdigest
incr_hash.hexdigest
end
def md5
require 'digest/md5'
incremental_hash(Digest::MD5)
end
def sha1
require 'digest/sha1'
incremental_hash(Digest::SHA1)
end
def sha2
require 'digest/sha2'
incremental_hash(Digest::SHA2)
end
if '1.9' <= RUBY_VERSION

View File

@ -326,13 +326,7 @@ private
supplied=instance_variable_get("@#{type}")
type=type.to_s.upcase
hasher = Digest.const_get(type)
# Most are MD5 and it should be efficient.
if hasher == Digest::MD5
hash = fn.md5
else
hash = hasher.hexdigest(fn.read)
end
hash = fn.incremental_hash(hasher)
if supplied and not supplied.empty?
raise "#{type} mismatch\nExpected: #{hash}\nArchive: #{fn}" unless supplied.upcase == hash.upcase