Sha-256 Converter in Python!
Hi! This is one of my first Replit Talk posts, so please let me know how it is!
Python Sha-256 Converter
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 inputstime
: for logging the amount of time each operation took
Explanation
The repl has 12 functions, which are explained below.
lowestMultiple(multiple, greaterThan)
(line 4): Returns the lowest number that is a multiple ofmultiple
but greater thangreater than
splitIntoList(original, stringSize)
(line 10): Returns the stringoriginal
split into an array of strings, each the length ofstringSize
(length oforiginal
must be divisible bystringSize
)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 istrue
and the other isfalse
andAddBinary(binary)
(line 33): 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 45): Takes one binary string,binary
, and inverts it. The1
s become0
s, and the0
s become1
s.padBinary(binary)
(line 54): 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 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 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 98): 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 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, 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 123): Converts the string of binary into hexadecimal.textToSha256(text)
(line 149): Converts the stringtext
into a hash. This uses the above functions to convert this.
The Program
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
The 982d9e3eb996f559e633f4d194def3761d909f5a3b647d1a851fead67c32c9d1
is the output hash, and the 0.3594856262207031 seconds
is how long the operation took.
To stop the loop, enter break
.
Thank you for reading! Feel free to test out the program below ;)
- LoveTheBears
Wow I have a medium bit of exprience coding and fixing issues. So if anyone wants to give me tips to help in the long run. Let me know. I might need the tips and you might be able to help me. May God Bless you and keep you well. Be nice and be kind and be strong together and help one another and then they will do the same to you. Never think you are bad. People have some advantages and disadvantages and do not compare yourself to someone else. Compare to yourself and try doing better than the last time. I hope people will do the same. I am trying to help. Thank you for being with me when I am coding. It really changes perspective and it will help many people who are dealing with nightmares. It really changes peopleś mindset. There are many fields in life and there are manty jobs to offer near your city so you can stay near family and help them when needed. It will really help and thanks for being with me when I code. Bye and Have a nice day. #choosetobehappy #youareloved. Do not think you are not loved. You are loved even if you have a disability or problems. Just pray for everyone who deals with that and maybe you will help them when they go to college and when they get bullied.Bye. Sorry for making this long.
wow i strugle using libraries to use SHA256. I cant even comprehend remaking it!
P O G
never have i seen a sha-256 post that they actually made it themselves on replit and this is pog
Very intuitive!
Well done! This is what I would call a high quality post on replit.
I think I can explain this... ☝