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
@ -155,15 +155,29 @@ class Pathname
|
|||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def md5
|
def incremental_hash(hasher)
|
||||||
require 'digest'
|
incr_hash = hasher.new
|
||||||
incr_md5 = Digest::MD5.new
|
|
||||||
self.open('r') do |f|
|
self.open('r') do |f|
|
||||||
f.each_line do |line|
|
while(buf = f.read(1024))
|
||||||
incr_md5 << line
|
incr_hash << buf
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
if '1.9' <= RUBY_VERSION
|
if '1.9' <= RUBY_VERSION
|
||||||
|
|||||||
@ -326,13 +326,7 @@ private
|
|||||||
supplied=instance_variable_get("@#{type}")
|
supplied=instance_variable_get("@#{type}")
|
||||||
type=type.to_s.upcase
|
type=type.to_s.upcase
|
||||||
hasher = Digest.const_get(type)
|
hasher = Digest.const_get(type)
|
||||||
|
hash = fn.incremental_hash(hasher)
|
||||||
# Most are MD5 and it should be efficient.
|
|
||||||
if hasher == Digest::MD5
|
|
||||||
hash = fn.md5
|
|
||||||
else
|
|
||||||
hash = hasher.hexdigest(fn.read)
|
|
||||||
end
|
|
||||||
|
|
||||||
if supplied and not supplied.empty?
|
if supplied and not supplied.empty?
|
||||||
raise "#{type} mismatch\nExpected: #{hash}\nArchive: #{fn}" unless supplied.upcase == hash.upcase
|
raise "#{type} mismatch\nExpected: #{hash}\nArchive: #{fn}" unless supplied.upcase == hash.upcase
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user