Changes between Version 3 and Version 4 of HashSourceCodes


Ignore:
Timestamp:
Sep 30, 2008, 5:36:15 PM (16 years ago)
Author:
guest
Comment:

Ruby code improved to handle bigger (?) files. From http://github.com/johanlunds/down_sub/tree/lib/down_sub/movie_hasher.rb

Legend:

Unmodified
Added
Removed
Modified
  • HashSourceCodes

    v3 v4  
    634634#!ruby
    635635
    636 class MovieHasher
    637 
    638   HASH_CHUNK_SIZE = 64 * 1024 # in bytes
    639   HASH_SIZE = 8 # in bytes
    640  
     636module MovieHasher
     637
     638  CHUNK_SIZE = 64 * 1024 # in bytes
     639
    641640  def self.compute_hash(filename)
    642     file = File.open(filename, "rb")
    643641    filesize = File.size(filename)
    644    
    645642    hash = filesize
    646    
     643
    647644    # Read 64 kbytes, divide up into 64 bits and add each
    648645    # to hash. Do for beginning and end of file.
    649    
    650     # Q = unsigned long long (64 bit = HASH_SIZE)
    651     file.read(HASH_CHUNK_SIZE).unpack("Q*").each { |n| hash += n }
    652     file.seek([0, filesize - HASH_CHUNK_SIZE].max, IO::SEEK_SET)
    653     file.read(HASH_CHUNK_SIZE).unpack("Q*").each { |n| hash += n }
    654    
    655     file.close
    656    
    657     hash &= 0xffffffffffffffff # keep the first 8 bytes / 64 bits
    658    
    659     sprintf("%0" + (HASH_SIZE * 2).to_s +  "x", hash)
     646    File.open(filename, 'rb') do |f|   
     647      # Q = unsigned long long = 64 bit
     648      f.read(CHUNK_SIZE).unpack("Q*").each do |n|
     649        hash = hash + n & 0xffffffffffffffff # to remain as 64 bit number
     650      end
     651
     652      f.seek([0, filesize - CHUNK_SIZE].max, IO::SEEK_SET)
     653
     654      # And again for the end of the file
     655      f.read(CHUNK_SIZE).unpack("Q*").each do |n|
     656        hash = hash + n & 0xffffffffffffffff
     657      end
     658    end
     659
     660    sprintf("%016x", hash)
    660661  end
    661662end
     
    668669      assert_equal("8e245d9679d31e12", MovieHasher::compute_hash('breakdance.avi'))
    669670    end
     671
     672    def test_compute_hash_large_file
     673      assert_equal("61f7751fc2a72bfb", MovieHasher::compute_hash('dummy.bin'))
     674    end
    670675  end
    671676end