SHA hashes are also extremely commonly used, alongside MD5 hashes. The early implementation of SHA hashes started with SHA1, which is less frequently used now due to the weakness of the hash. SHA1 was followed up with SHA128, which was then replaced by SHA256.
Generating SHA hashes within Python is also extremely simple by using the imported module. With simple tweaks, we can change whether we would like to generate an SHA1, SHA128, or SHA256 hash.
The following are three different scripts that allow us to generate the different SHA hashes:
Here is the script of SHA1:
import hashlib message = raw_input("Enter the string you would like to hash: ") sha = hashlib.sha1(message) sha1 = sha.hexdigest() print sha1
Here is the script of SHA128:
import hashlib message = raw_input("Enter the string you would like to hash: ") sha = hashlib.sha128(message) sha128 = sha.hexdigest() print sha128
Here is the script of SHA256:
import hashlib message = raw_input("Enter the string you would like to hash: ") sha = hashlib.sha256(message) sha256 = sha.hexdigest() print sha256
The hashlib
module once again does the bulk of the work for us here. We can utilize the features within the module.
We start by importing the module by using:
import hashlib
We then need to prompt for the string to encode using SHA. We ask the user for input rather than using hard-coding, so that the script can be used over and over again. This can be done with:
message = raw_input("Enter the string you would like to hash: )
Once we have the string, we can start the encoding process. The next part depends on the SHA encoding that you would like to use:
sha = hashlib.sha*(message)
We need to replace *
with either 1
, 128
, or 256
. Once we have the message SHA-encoded, we need to use the hexdigest()
function once again so the output becomes readable.
We do this with:
sha*=sha.hexdigest()
Once the output has become readable, we simply need to print the hash output:
print sha*