Created
January 27, 2018 18:14
-
-
Save 3zcurdia/58a1e1cfe93d123a59e8a3ee82843713 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest' | |
NUM_ZEROES = 6 | |
class Block | |
def find_nonce(message) | |
nonce = "HELP I'M TRAPPED IN A NONCE FACTORY" | |
count = 0 | |
until valid_nonce?(nonce, message) | |
print '.' if count % 10_000 == 0 | |
nonce = nonce.succ | |
count += 1 | |
end | |
puts "\nTotal: #{count}" | |
nonce | |
end | |
def valid_nonce?(nonce, message) | |
hash(message + nonce).start_with?("0" * NUM_ZEROES) | |
end | |
private | |
def hash(message) | |
Digest::SHA256.hexdigest(message) | |
end | |
end | |
block = Block.new | |
nonce = block.find_nonce("lorem ipsum") | |
puts "Nonce: #{nonce}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment