Changes between Version 3 and Version 4 of HashSourceCodes
- Timestamp:
- Sep 30, 2008, 5:36:15 PM (16 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
HashSourceCodes
v3 v4 634 634 #!ruby 635 635 636 class MovieHasher 637 638 HASH_CHUNK_SIZE = 64 * 1024 # in bytes 639 HASH_SIZE = 8 # in bytes 640 636 module MovieHasher 637 638 CHUNK_SIZE = 64 * 1024 # in bytes 639 641 640 def self.compute_hash(filename) 642 file = File.open(filename, "rb")643 641 filesize = File.size(filename) 644 645 642 hash = filesize 646 643 647 644 # Read 64 kbytes, divide up into 64 bits and add each 648 645 # 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) 660 661 end 661 662 end … … 668 669 assert_equal("8e245d9679d31e12", MovieHasher::compute_hash('breakdance.avi')) 669 670 end 671 672 def test_compute_hash_large_file 673 assert_equal("61f7751fc2a72bfb", MovieHasher::compute_hash('dummy.bin')) 674 end 670 675 end 671 676 end