An implementation of the SHA-256 hashing function written in Python to understand the basic principles and function of it.
The hash.py
file contains the actual code, example.py
is just example usage. Run hash.textToSha256(message)
to use.
What is Sha-256? Sha-256 is a cryptographic hash function. It was created in 2001 by the US National Security Agency. This hashing function is one-way, meaning that once you create a hash from a string, you can't create the string from the hash. Each hash is 64 characters long, and includes numbers and letters. 'sha' stands for 'Secure Hash Algorithm'.
More information can be found on Wikipedia.
Sha-256 in Python (with hashlib) In python, there is a built-in library for handling hashing like this, called hashlib. A very simple hashing program can look like this:
import hashlib
print(hashlib.sha256("Message here".encode('ASCII')).hexdigest())
Custom Implementation
The helper functions used are explained below:
lowestMultiple(multiple, greaterThan)
(line 1): Returns the lowest number that is a multiple ofmultiple
but greater thangreater than
splitIntoList(original, stringSize)
(line 7): Returns the stringoriginal
split into an array of strings, each the length ofstringSize
(length oforiginal
must be divisible bystringSize
)xorAddBinary(binary)
(line 17): Takes a list of binary strings,binary
, and adds them together with an 'exclusive or'. Each bit is only true if the value of each corresponding bit differs, so if one istrue
and the other isfalse
andAddBinary(binary)
(line 30): Takes a list of binary strings,binary
, and adds them together with anand
. Each bit is only true if the value of both the corresponding input bits is true.notBinary(binary)
(line 42): Takes one binary string,binary
, and inverts it. The1
s become0
s, and the0
s become1
s.padBinary(binary)
(line 51): Takes an array of binary strings,binary
, and finds the length of the longest one. Then, it adds0
s to the beginning of the other strings to make them all the same lengthaddBinary(binary)
(line 64): Takes an array of binary strings,binary
, and adds them with module 2^32. Each binary string is 32 in length, and so is the output.rightRotate(string, amount)
(line 88): Right rotates thestring
byamount
. Basically, each character is pushedamount
characters to the right. When a character falls off, it is moved to the beginning of the string.rightShift(string, amount)
(line 95): Right shifts thestring
byamount
. Like the right rotate function, each character is pushed to the right byamount
, however, if one falls off, it is replaced by0
in the beginning.getNextWord(words)
(line 98): Takes a list of words,words
, and uses that the calculate the next word in the sequence using the sha-256 algorithm. In pseudocode, withi
being the index you are trying to find:
s0 := (w[i-15] rightrotate 7) xor (w[i-15] rightrotate 18) xor (w[i-15] rightshift 3)
s1 := (w[i- 2] rightrotate 17) xor (w[i- 2] rightrotate 19) xor (w[i- 2] rightshift 10)
w[i] := w[i-16] + s0 + w[i-7] + s1
binaryToHex(binary)
(line 120): Converts the string of binary into hexadecimal.textToSha256(text)
(line 146): Converts the stringtext
into a hash. This uses the above functions to convert this.