Skip to content

Instantly share code, notes, and snippets.

@RX14
Last active September 19, 2016 19:55
Show Gist options
  • Save RX14/35bb60f73d7feed5ff488d2a72380b38 to your computer and use it in GitHub Desktop.
Save RX14/35bb60f73d7feed5ff488d2a72380b38 to your computer and use it in GitHub Desktop.
module MediaManager
class VideoFile
CHUNK_SIZE = 64 * 1024
def self.compute_hash(file_path : String)
filesize = File.size(file_path)
hash = filesize
# Read 64 kbytes, divide up into 64 bits and add each
# to hash. Do for beginning and end of file.
File.open(file_path, "rb") do |f|
(CHUNK_SIZE / sizeof(UInt64)).times do
hash += f.read_bytes(UInt64, IO::ByteOrder::LittleEndian)
end
f.seek([0, filesize - CHUNK_SIZE].max)
# And again for the end of the file
(CHUNK_SIZE / sizeof(UInt64)).times do
hash += f.read_bytes(UInt64, IO::ByteOrder::LittleEndian)
end
end
sprintf("%016x", hash)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment