How to generate a SHA512 hash from a String in Python

A SHA-512 can be generated in python using the hashlib library.

Syntax:

import hashlib

hash = hashlib.sha512( str( input ).encode("utf-8") ).hexdigest()

Example:

This example code creates a sha-512 from a string. The input is converted to a String. It is then encoded in utf-8 to get the characters, then the hashlib.sha512 method is called to hash the String. The hash is accessed using the hexdigest method, it will return the human readable version of the sha512.

import hashlib

input = 'test'
hash = hashlib.sha512( str( input ).encode("utf-8") ).hexdigest()
print(hash)

The output will be:

ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff

References:

Python hashlib
SHA-2 cryptographic hash functions

Share this article:

Recent Comments