danielcavanagh/ruby-blake

Blake doesn't return same hash each time

Closed this issue · 3 comments

The following gives a different Blake result each time. With OpenSSL SHA256 it gives the same result each time. I would expect a Blake hash to give the same result every time, otherwise it's just a random number generator.

 require 'openssl'
 load 'blake.rb'
 def bin_to_hex(s_b) # convert binary to readable hex
     s_b.each_byte.map { |b| b.to_s(16).rjust(2,'0') }.join
 end
 def hex_to_bin(s_h) # convert readable hex to binary
     s_h.scan(/../).map { |x| x.hex.chr }.join
 end

 sha256 = OpenSSL::Digest::SHA256.new
 blake = BLAKE.new(256) # create BLAKE hash object

 teststring = "75aa61443feb8bbf2a8c450ad6379c92962072e3d"

 for i in 1..5 do
     block_data_bin = hex_to_bin teststring
     block_digest_bin = blake.digest(block_data_bin)
     block_digest_hex = bin_to_hex(block_digest_bin)
     puts "Calc hash: " + block_digest_hex
 end
 for i in 1..5 do
     block_data_bin = hex_to_bin teststring
     block_digest_bin = sha256.digest(block_data_bin)
     block_digest_hex = bin_to_hex(block_digest_bin)
     puts "Calc hash: " + block_digest_hex
 end

sorry!! i wrote this for a uni assignment and never actually used it fully (or even tested 256 bit outputs apparently...)

two bugs have been fixed and i've added some unit tests

cheers for the bug report :)

Thanks Daniel. This appears to be the only Blake hash library for Ruby I can find, and as it often the case with github you can't tell what the level and seriousness of the contribution is. I'll give it another whirl.

i wouldn't say it's particularly serious... the uni assignment wasn't rigorous and so i did the bare minimum to get a library implemented. but it is at least fully functioning, even if it potentially has bugs

in the assignment i really only utilised 512 bit hashes through the class form (eg. BLAKE.digest('string')) and so that was the only part of the library i actually tested. thus the bug you saw (plus another one i found in the process after writing the unit tests)

i would be confident now that 512 bit and 256 bit outputs work (but of course don't stake your life!)