# example of iterating a nonce in a hashing algorithm's input

from __future__ import print_function
import hashlib
import time
import sys

#text = "Hello 56600 Peeps!"
print("len:",len(sys.argv))
if len(sys.argv) < 2:
    print("USAGE: noncer [text_to_hash]")
    quit()

text = sys.argv[1]
print("Working Text is",text)
#target = 0x0000123456789ABCDEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF
#TESTNET target:
#target = 0x1fffff0000000000000000000000000000000000000000000000000000000000
#PLAYGROUND
target =  0x0000200000000000000000000000000000000000000000000000000000000000
print("Target: ", format(target, '064x'))

# checkpoint the current time
start_time = time.time()

# iterate nonce from 0 to 24
for nonce in range(100000000):
    # add the nonce to the end of the text
    input_data = text + str(nonce)

    # calculate the SHA-256 hash of the input (text+nonce)
    hash_data = hashlib.sha256(input_data.encode('utf-8')).hexdigest()
    ihash = int(hash_data,16)

    match = ihash < target # ???

    # show the input and hash result
    if match:
        print("Nonce: ", nonce, ": ", input_data, '=>', hash_data, " Match? ",match)
        # checkpoint how long it took to find a result
        end_time = time.time()
        elapsed_time = end_time - start_time
        print("Elapsed Time: %.4f seconds" % elapsed_time)
        quit()

