Skip to content

Instantly share code, notes, and snippets.

@3zcurdia
Created January 27, 2018 18:14
Show Gist options
  • Save 3zcurdia/58a1e1cfe93d123a59e8a3ee82843713 to your computer and use it in GitHub Desktop.
Save 3zcurdia/58a1e1cfe93d123a59e8a3ee82843713 to your computer and use it in GitHub Desktop.
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