Re: Ruby Quiz - Challenge #5 - Crypto Mining - Find the Winning Lucky Number - Nonce (=Number used ONCE) for the Proof-of-Work (PoW) Hash (SHA-256)
Gerald Bauer <[email protected]>
| Newsgroups | gmane.comp.lang.ruby.general |
|---|---|
| Message-ID | <CAAxEZd8rxuO53PmyqvT7edaHvTWYYgyBVJbw6eKSKqpuAy--tw@mail.gmail.com> |
Hello Ruby Quiz Friends,
For your reference here's the test solution for Ruby Quiz #5 -
Crypto Mining - Find the Winning Lucky Number - Nonce
(=Number used ONCE) for the Proof-of-Work (PoW) Hash (SHA-256) [1]:
def compute_nonce( data )
nonce = 0
loop do
hash = Digest::SHA256.hexdigest( "#{nonce}#{data}" )
break if hash.start_with?( "0000" ) # bingo!
nonce += 1 # keep trying and
trying ...
end
nonce
end
Happy crypto mining w/ ruby. Cheers. Prost.
[1] https://github.com/planetruby/quiz/tree/master/005
Unsubscribe: <mailto:[email protected]?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-talk>