Module: SubtitleIt::MovieHasher

Defined in:
lib/subtitle_it/movie_hasher.rb

Constant Summary collapse

CHUNK_SIZE =

in bytes

64 * 1024

Class Method Summary collapse

Class Method Details

.compute_haxx(filename) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/subtitle_it/movie_hasher.rb', line 7

def self.compute_haxx(filename)
  filesize = File.size(filename)
  haxx = filesize

  # Read 64 kbytes, divide up into 64 bits and add each
  # to hash. Do for beginning and end of file.
  File.open(filename, 'rb') do |f|    
    # Q = unsigned long long = 64 bit
    f.read(CHUNK_SIZE).unpack("Q*").each do |n|
      haxx = haxx + n & 0xffffffffffffffff # to remain as 64 bit number
    end

    f.seek([0, filesize - CHUNK_SIZE].max, IO::SEEK_SET)

    # And again for the end of the file
    f.read(CHUNK_SIZE).unpack("Q*").each do |n|
      haxx = haxx + n & 0xffffffffffffffff
    end
  end

  sprintf("%016x", haxx)
end