How to generate a SHA512 hash from a String in Python

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


import hashlib

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


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

The output will be:



Python hashlib
SHA-2 cryptographic hash functions

Share this article:

Recent Comments