Python - MD5 , SHA-1, SHA-256, & SHA-512 Hashing

Hashing is the transformation of any given key or string of characters into a new value. The new value is typically a short, fixed-length key that represents the original string and makes it simpler to find or use. Please note that once this hash is created and saved to the database, it cannot be changed back to your original password.


MD5 hash in Python

MD5 (Message-Digest algorithm) is one of the most popular hash functions. It produces a hash value of 128 bits. Originally, MD5 was designed to be a cryptographic hash function. However, it has a large number of known vulnerabilities. MD5 can still be used for checksums to check the integrity of data, but only against accidental corruption. It is still suitable for non-cryptographic uses, such as determining the partition of a specific key in a distributed database.

Example: Use hashlib.md5() method to generate an MD5 hash value from a String. 
# Python3 code to demonstrate the MD5
import hashlib
 
# initializing the string
str = "Secret text"
 
# encoding and printing the hash value.
result = hashlib.md5(str.encode()).hexdigest()
print(result)

Console output:

7f273c4aa46608a7d7d16340c673008b


SHA-1 hash in Python

Secure Hash Algorithm (SHA-1) is a cryptographic algorithm used in cryptography. SHA-1 takes an input and returns a 160-bit, 20-byte hash value called a message digest – usually a 40-digit (hexadecimal) number.

Example: Use hashlib.sha1() method to generate a SHA1 hash value from a String.
# Python 3 code to demonstrate the SHA1
import hashlib
 
# initializing string
str = "Secret Text"
 
# encoding and printing the sha1 hash value.
result = hashlib.sha1(str.encode()).hexdigest()
print(result)

Console output:

593c208facb53cb4650ac99eb92d58eb7c99a2d8


SHA-256 hash in Python

The Secure Hash Algorithm (SHA) is one example of a cryptographic hash function. A cryptographic hash is similar to a text or data file signature. The Secure Hash Algorithm SHA-256 (hash-256) generates a 256-bit fixed-size (32-byte) hash. Hashes are one-way functions, meaning they cannot be decoded back.

Example: Use hashlib.sha256() method to generate a SHA256 hash value from a String.
# Python 3 code to demonstrate the SHA256
import hashlib
 
# initializing string
str = "Your Secret"
 
# encoding and printing the sha256 hash value.
result = hashlib.sha256(str.encode()).hexdigest()
print(result)

Console output:

593c208facb53cb4650ac99eb92d58eb7c99a2d8


SHA-512 hash in Python

With the exception of using 1024 bit "blocks" and accepting input strings up to 2^128 bits long, SHA-512 and SHA-256 are extremely similar. Compared to Sha-256, SHA-512 features additional algorithmic changes.

Example: Use hashlib.sha512() method to generate a SHA512 hash value from a String.
# Python 3 code to demonstrate the SHA512
import hashlib
 
# initializing string
str = "Your Secret"
 
# encoding and printing the sha512 hash value.
result = hashlib.sha512(str.encode()).hexdigest()
print(result)

Console output:

84b14450eb0b947afa67f43714af99bd450f8155dba96a8a0a29a56f20af5c960883c27f54feffa88a5d248ca0488c3cc2995d19caadf8f7b8c7d2f103291c46

Comments

Popular posts from this blog

Learn Java 8 streams with an example - print odd/even numbers from Array and List

Java, Spring Boot Mini Project - Library Management System - Download

Java - Blowfish Encryption and decryption Example

Java - DES Encryption and Decryption example

Google Cloud Storage + Spring Boot - File Upload, Download, and Delete

ReactJS - Bootstrap - Buttons

Spring Boot 3 + Spring Security 6 + Thymeleaf - Registration and Login Example

File Upload, Download, And Delete - Azure Blob Storage + Spring Boot Example

Java - How to Count the Number of Occurrences of Substring in a String

Top 5 Java ORM tools - 2024