2015-05-19 48 views
1

我有一个3GB文件,我需要使用ruby xxhash库进行校验和。该问题我得到一个内存不足的错误。Ruby - XXhash校验和大文件

这是导致该错误代码:

contents = File.read('some_file') 
checksum = XXhash.xxh64(contents, 123918230912) 

我知道库,如openssldigest/sha1有更新方法,像这样:

checksum = File.open('some_file', "rb") do |io| 
    dig = Digest::SHA1.new 
    buf = "" 
    dig.update(buf) while io.read(4096, buf) 
    dig 
end 

xxhash似乎不具备这样的方法。我如何能够使用xxhash校验大文件(3GB +)?

帮助赞赏。

回答

3

按照xxh64_stream方法接受一个I​​O对象自述,所以你可以做

File.open("some_file") do |f| 
    XXhash.xxh32_stream(f,12345678) 
end