Loving python

The network will throw out a challenge (which is actually a random string of numbers and alphabets), and then we will need to append a random guessed answer to the back of this challenge and pass it to a SHA256 hash to generate out a sequence of 0s

Example 1 : We hard code the challenge as D8H6tYUHFN98JJHnBB7yUih54

Example 1 :

We notice the challenge doesn’t change, however the answer is a random generated answer which we program the system to “guess”

Example 2: We now concatenate the challenge + the random attempt together and then hash it

We notice that each time we call the function testAttempt() we generate out a hash to guess. And we ran it many times but still were not able to find one answer that starts with 0.

Example 3 : We now put the condition that only if the guessed hash starts with 3 – zeros than we accept it as an answer

We see that our simple program managed to find a lot of answers. This also meant that any one answer can be accepted by the system as a valid answer.

Example 4 : Infinite loop until it finds the answer

My computer took 9 secs to find the first answer with 4 zeros. So once the difficulty keep increasing, it might be exponentially unlikely for my 1 pc to find the answer

This in a nutshell is how proof of work works. I will expand electricity to proof to the network i did work. 🙂

The code snippet below

import string

import random

import hashlib

import time

example_challenge = ‘D8H6tYUHFN98JJHnBB7yUih54’

def generation(challenge=example_challenge,size=25):

answer =”.join(random.choice(string.ascii_lowercase +

string.ascii_uppercase +

string.digits) for x in range(size))

#print challenge

#print answer

attempt = challenge+answer

return attempt, answer

#generation()

shaHash = hashlib.sha256()

def testAttempt():

Found = False

start = time.time()

while Found == False:

attempt, answer = generation()

shaHash.update(attempt)

solution = shaHash.hexdigest()

if solution.startswith(‘0000’):

timeTook = time.time() – start

print solution

print ‘time taken’, timeTook

Found=TRUE

print answer

testAttempt()

#for x in xrange(0,100000):

#testAttempt()