Sorry, this does not answer your question, but may help.
First, when reading in a file, make sure that you pass the "rb" parameter. I see that you are not on windows, but if by chance your code ends up running on a Windows machine, your code will not work that way, especially when reading ruby files. Example:
crc32 = File.open('test.rb') { |f| Zlib.crc32 f.read } #=> 189072290 digest = Digest::CRC32.file('test.rb').digest!.to_i #=> 314435800 crc32 == digest #=> false crc32 = File.open('test.rb', "rb") { |f| Zlib.crc32 f.read } #=> 314435800 digest = Digest::CRC32.file('test.rb').digest!.to_i #=> 314435800 crc32 == digest #=> true
The above will work on all platforms and all rubies .. that I know about .. But this is not what you requested.
I am pretty sure that the hexdigest and digest methods in your example above work as they should, but ..
dig_file = Digest::CRC32.file('test.rb') test1 = dig_file.hexdigest #=> "333134343335383030" test2 = dig_file.digest #=> "314435800" def hexdigest_to_digest(h) h.unpack('a2'*(h.size/2)).collect {|i| i.hex.chr }.join end test3 = hexdigest_to_digest(test1) #=> "314435800"
So, I assume .to_i.to_s (16) discards the expected result, or your expected result might be wrong? Not sure, but all the best
source share