Sha-256 Converter in Python!
Hi! This is one of my first Replit Talk posts, so please let me know how it is!
What is Sha-256?
Sha-256 is a cryptographic hash function. It was created in 2001 by the US National Security Agency, also known as the NSA. 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())
This is very easy and fast, which made me wonder... what if I made my own sha256 program in Python without using libraries like hashlib?
So I created this repl. In total, it uses two external libraries, but only for printing to the console. The libraries it uses are:
os: for clearing the console between inputs
time: for logging the amount of time each operation took
The repl has 12 functions, which are explained below.
lowestMultiple(multiple, greaterThan)(line 4): Returns the lowest number that is a multiple of
multiplebut greater than
splitIntoList(original, stringSize)(line 10): Returns the string
originalsplit into an array of strings, each the length of
originalmust be divisible by
xorAddBinary(binary)(line 20): 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 is
trueand the other is
andAddBinary(binary)(line 33): Takes a list of binary strings,
binary, and adds them together with an
and. Each bit is only true if the value of both the corresponding input bits is true.
notBinary(binary)(line 45): Takes one binary string,
binary, and inverts it. The
0s, and the
padBinary(binary)(line 54): Takes an array of binary strings,
binary, and finds the length of the longest one. Then, it adds
0s to the beginning of the other strings to make them all the same length
addBinary(binary)(line 67): 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 91): Right rotates the
amount. Basically, each character is pushed
amountcharacters to the right. When a character falls off, it is moved to the beginning of the string.
rightShift(string, amount)(line 98): Right shifts the
amount. Like the right rotate function, each character is pushed to the right by
amount, however, if one falls off, it is replaced by
0in the beginning.
getNextWord(words)(line 101): Takes a list of words,
words, and uses that the calculate the next word in the sequence using the sha-256 algorithm. In pseudocode, with
ibeing 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 123): Converts the string of binary into hexadecimal.
textToSha256(text)(line 149): Converts the string
textinto a hash. This uses the above functions to convert this.
When you hit run, the console will allow you to enter text. When you press enter, it will convert it to sha256 and show how long the operation took.
Python SHA256 Converter ---------------------- Enter text to convert: (or enter 'break' to stop) >
The output looks like this:
Output: 982d9e3eb996f559e633f4d194def3761d909f5a3b647d1a851fead67c32c9d1 Duration: 0.3594856262207031 seconds. press [enter] to continue
982d9e3eb996f559e633f4d194def3761d909f5a3b647d1a851fead67c32c9d1 is the output hash, and the
0.3594856262207031 seconds is how long the operation took.
To stop the loop, enter
Thank you for reading! Feel free to test out the program below ;)