Last active
September 19, 2016 19:55
-
-
Save RX14/35bb60f73d7feed5ff488d2a72380b38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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