Skip to content

Instantly share code, notes, and snippets.

@flazz
Created December 9, 2010 22:54
Show Gist options
  • Select an option

  • Save flazz/735465 to your computer and use it in GitHub Desktop.

Select an option

Save flazz/735465 to your computer and use it in GitHub Desktop.
require 'digest/sha1'
def buf_sha file, buf_size
d = Digest::SHA1.new
buf = String.new
open(file) do |io|
d.update buf while io.read(buf_size, buf)
end
d
end
file = 'data'
MiB = 1024 ** 2
# uncomment the line below to generate the data file
# `dd if=/dev/urandom of=#{file} bs=#{MiB} count=550`
puts "Digest::SHA1#file:\t#{Digest::SHA1.file(file)}"
buf_size = 512 * MiB
puts "updated < 512 MiB:\t#{buf_sha(file, buf_size - 1)}"
puts "updated = 512 MiB:\t#{buf_sha(file, buf_size)}"
puts "updated = file size:\t#{buf_sha(file, File.size(file))}"
@edsu
Copy link

edsu commented Sep 27, 2011

ed@curry:/735465$ sha1sum data
43a8c9e911fdbacf382f45287ef7e592672f184c data
ed@curry:
/735465$ ruby sha1-bug.rb
550+0 records in
550+0 records out
576716800 bytes (577 MB) copied, 104.668 s, 5.5 MB/s
Digest::SHA1#file: 56a35cadf6e96329cade8ab027153d431c6e4d5c
updated < 512 MiB: 56a35cadf6e96329cade8ab027153d431c6e4d5c
updated = 512 MiB: 5b765e82c4813b0ada85f841269e8386a499b10e
updated = file size: 5b765e82c4813b0ada85f841269e8386a499b10e

@edsu
Copy link

edsu commented Sep 27, 2011

color me confused

@flazz
Copy link
Author

flazz commented Sep 28, 2011 via email

@edsu
Copy link

edsu commented Sep 28, 2011

At any rate I've got a merge request for your bagit library which I think addresses large file support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment