Re: Ruby Quiz - Challenge #2 - Calculate the Bitcoin Genesis Block Hash (SHA-256)


(Frank J. Cameron) #1

Challenge #2 - Calculate the Bitcoin Genesis Block Hash (SHA-256)

$ ruby lib/002.rb
Run options: --seed 62016
# Running:
.
Finished in 0.002718s, 367.9076 runs/s, 367.9076 assertions/s.
1 runs, 1 assertions, 0 failures, 0 errors, 0 skips

$ cat lib/002.rb
require_relative '../002/test.rb'
require 'digest'
class RubyQuizTest
  def calculate(v, p, m, t, b, n)
    Digest::SHA256.digest(
      Digest::SHA256.digest(
        [v].pack('V') \
        + [p].pack('H*').reverse \
        + [m].pack('H*').reverse \
        + [t].pack('V') \
        + [b].pack('H*').reverse \
        + [n].pack('V')
      )
    ).reverse.unpack('H*')[0]
  end
end
RubyQuizTest.new('fjc')