Generating a hash chain in Python 003
Hello fellow Steemers,
After having explained the provably-fair concept, and the SHA256 cryptographic algorithm, it’s time we generate our hash chain.
3) Generating a hash chain in Python

Requirements
I’m gonna use Python v3.5.2 and MySQL. You can of course adapt what will follow to your preferences : almost any language has a module for SHA256 encryption and your hash chain can be stored on any database system.
Modules required
import hmac
import MySQLdb
import string
import random
So we have hmac that is a built-in module that allows you to encrypt data in all the most popular algorithms. MySQLdb has a name that speaks for itself, it will manage connection to our database and storing data on it. string (string manipulation) and random (pseudo-random number generator) are modules I will use it only to generate our house secret (the password hashed into the original server seed).
Connecting to the database
conn = MySQLdb.connect("host", "user", "pass", "database")
cur = conn.cursor()
conn.autocommit(False)
# Code to generate the hash chain will be put here
cur.close()
conn.commit()
conn.close()
So here we open a connection to the MySQL database referenced as conn.
We then create a cursor (a buffer to receive instructions from / emit data to our SQL client - here MySQL). We then declare autocommit to False, meaning we don’t want data to be written on hard disk at each request or it can really slow down the process, but instead to start a transaction, give all our instructions to the SQL client, and then commit it (executing all the transaction at once).
In case we want to generate a really long hash chain, it would be better to commit every hundred thousands instructions or so, and not give a single huge transaction to execute to the SQL client.
Generating and storing the hash chain
possibleChars = string.ascii_uppercase + string.digits + string.ascii_lowercase
prevHash = ''.join(random.choice(possibleChars) for x in range(500))
hashChain = []
for i in range(1, 1001, 1):
newHash = hmac.new(prevHash.encode(), digestmod="sha256").hexdigest()
hashChain.append(newHash)
prevHash = newHash
sql = "INSERT INTO `HASHCHAIN` VALUES (%s, %s, 0)"
cur.execute(sql, (i, newHash))
So here we have the real creation of the hash chain and its storage into MySQL.
First we begin by generating the house secret :
- The possible chars will be ASCII characters (uppercase and lowercase) and digits,
- The length of the generated string will be of five hundred characters.
The best is for you to not know this house secret, and to make it as long as possible, to avoid it being guessed by players trying to find a collision.
Then, in this example we will generate a chain of 1000 elements :
- We encode the previous hash (for the first iteration, it is our house secret),
- We store it into a local variable hashChain,
- We add the insert query into the transaction we began with the SQL client.
Just run it, and you’ll have your first hash chain !
Thanks for reading me,
See you soon for the new series that will be about the core of the game (this time in NodeJS)
Follow back? https://steemit.com/entertainment/@a-0-0/fatima-2-trailer-video
welcome to steemit,
Get to know how to earn more on your posts and get more followers.
Make new friends and interact.
We can share our thoughts.
In the steemit school, we have ultimate contest challenges where you get to win and make investments.
You need support from a community to grow and earn easily on steemit.
I recommend one for you.
Meet over 3k + steemians.
Steemit is a business, we will teach you how to succeed
join the steemit school on discord https://discord.gg/pqWrzBn