From 8ce7abce7311d443772b518b9699e06e7938dfa0 Mon Sep 17 00:00:00 2001 From: Jake Good Date: Sat, 27 Mar 2010 10:41:24 -0500 Subject: More hashing refactoring to work with byte chunks Signed-off-by: Adam Vandenberg --- Library/Homebrew/extend/pathname.rb | 28 +++++++++++++++++++++------- Library/Homebrew/formula.rb | 8 +------- 2 files changed, 22 insertions(+), 14 deletions(-) (limited to 'Library') diff --git a/Library/Homebrew/extend/pathname.rb b/Library/Homebrew/extend/pathname.rb index d4fa6d92e..2c3d5a025 100644 --- a/Library/Homebrew/extend/pathname.rb +++ b/Library/Homebrew/extend/pathname.rb @@ -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 diff --git a/Library/Homebrew/formula.rb b/Library/Homebrew/formula.rb index 2605cd7a8..60865cacc 100644 --- a/Library/Homebrew/formula.rb +++ b/Library/Homebrew/formula.rb @@ -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 -- cgit v1.2.3