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

What does Hashing mean?

A secure password hash is an encrypted sequence of characters obtained after applying certain algorithms and manipulations on user-provided password, which are generally very weak and easy to guess.
Please remember that once this password hash is generated and stored in the database, you can not convert it back to the original password.


MD5 hash in Python

The MD5 message-digest algorithm is a widely used hash function producing a 128-bit hash value. Although MD5 was initially designed to be used as a cryptographic hash function, it has been found to suffer from extensive vulnerabilities. It can still be used as a checksum to verify data integrity, but only against unintentional corruption. It remains suitable for other non-cryptographic purposes, for example for determining the partition for a particular key in a partitioned database.

Example: Python - MD5 hashing

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)

Output:




SHA-1 hash in Python

In cryptography, SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash function that takes an input and produces a 160-bit (20-byte) hash value known as a message digest – typically rendered as a hexadecimal number, 40 digits long. It was designed by the United States National Security Agency and is a U.S. Federal Information Processing Standard.

Use hashlib.sha1() method to generate a SHA1 hash value from a String.

Example: Python - SHA-1 hashing

# Python 3 code to demonstrate the SHA1
import hashlib
# initializing string
str = "Secret"
# encoding and printing the sha1 hash value.
result = hashlib.sha1(str.encode()).hexdigest()
print(result)

Output:




SHA-256 hash in Python

The SHA (Secure Hash Algorithm) is one of a number of cryptographic hash functions. A cryptographic hash is like a signature for a text or a data file. SHA-256 algorithm generates an almost-unique, fixed-size 256-bit (32-byte) hash. Hash is a one-way function – it cannot be decrypted back.

Use hashlib.sha256() method to generate a SHA256 hash value from a String.

Example: Python - SHA-256 hashing

# 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)

Output:




SHA-512 hash in Python

SHA-512 is a function of the cryptographic algorithm SHA-2, which is an evolution of the famous SHA-1.
SHA-512 is very close to Sha-256 except that it used 1024 bits "blocks", and accept as input a 2^128 bits maximum length string. SHA-512 also has other algorithmic modifications in comparison with Sha-256.

Use hashlib.sha512() method to generate a SHA512 hash value from a String.

Example: Python - SHA-512 hashing

# 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)

Output:



Popular posts from this blog

Spring boot video streaming example-HTML5

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

Spring Boot + Mockito simple application with 100% code coverage

Spring Boot + OpenCSV Export Data to CSV Example

Custom Exception Handling in Quarkus REST API

DataTable-Pagination example with Spring boot, jQuery and ajax

Registration and Login with Spring Boot + Spring Security + Thymeleaf

Node JS mini projects with source code - free download

Spring boot web project free download:User Registration System

Java - Blowfish Encryption and decryption Example