Skip to content

Instantly share code, notes, and snippets.

@snipsnipsnip
Created July 21, 2015 17:24
Show Gist options
  • Save snipsnipsnip/466d404b35b573f76e44 to your computer and use it in GitHub Desktop.
Save snipsnipsnip/466d404b35b573f76e44 to your computer and use it in GitHub Desktop.
io_hasher.rb: digests an IO
require 'digest'
class IOHasher
def initialize(digest=Digest::MD5.new, buf='\0' * 4096)
@digest = digest
@buf = buf
@chunk_size = buf.size
end
def hash_of(io)
@buf.clear
while io.read(@chunk_size, @buf) && !@buf.empty?
@digest << @buf
end
@digest.digest!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment