Skip to content
Sha-256 Converter in Python!
Profile icon

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 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 multiple but greater than greater than
  • splitIntoList(original, stringSize) (line 10): Returns the string original split into an array of strings, each the length of stringSize (length of original must be divisible by stringSize)
  • 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 true and the other is false
  • 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 1s become 0s, and the 0s become 1s.
  • 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 string by amount. Basically, each character is pushed amount 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 the string by amount. Like the right rotate function, each character is pushed to the right by amount, however, if one falls off, it is replaced by 0 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, with i 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 string text 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
You are viewing a single comment. View All
Profile icon

you have 5 upvotes, but you only have 4 cycles (you get one upvote for each cycle)