More hashing refactoring to work with byte chunks
Signed-off-by: Adam Vandenberg <flangy@gmail.com>
This commit is contained in:
parent
04855ddd48
commit
8ce7abce73
@ -154,16 +154,30 @@ 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
|
||||
|
||||
@ -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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user