hashlib module

To calculate the cryptographic hash value in Python, “hashlib” Module is used. The hashlib gives the following cryptographic hash functions to discover the hash output of a text as follows:

  • sha3_224 – 28 bit Digest-Size
  • sha3_256 – 32 bit Digest-Size
  • sha3_384 – 48 bit Digest-Size
  • sha3_512 – 64 bit Digest-Size

This module actualizes a typical interface to various secure hash and message digest calculations. Included are the FIPS secure hash calculations SHA1, SHA224, SHA256, SHA384, and SHA512 just as RSA’s MD5 calculation (characterized in Internet RFC 1321). Earlier calculations were called message digests, but today it is secure hash.

Python has a bountiful help for hash code calculations through the library module hashlib. You can utilize the “hashlib.algorithms_available” to get the rundown of all accessible hash calculations in your variant of Python.

Hashlib provides the following constant attributes:

  • “hashlib.algorithms_guaranteed” – A set which contains all the hashing algorithms which are guaranteed to be supported by this module on all platforms.
  • “hashlib.algorithms_available” –  A set that contains all the names of the hashing algorithms that are currently available in the interpreter.

Python3




import hashlib
 
print(hashlib.algorithms_guaranteed)
print(hashlib.algorithms_available)


Output :

{‘blake2b’, ‘shake_256’, ‘sha512’, ‘sha3_224’, ‘sha384’, ‘sha3_512’, ‘sha3_256’, ‘sha3_384’, ‘md5’, ‘sha256’, ‘sha224’, ‘sha1’, ‘blake2s’, ‘shake_128’}
 

{‘SHA512’, ‘md5’, ‘blake2s’, ‘sha512’, ‘DSA-SHA’, ‘whirlpool’, ‘sha224’, ‘sha3_256’, ‘DSA’, ‘blake2b’, ‘MD5’, ‘SHA256’, ‘ecdsa-with-SHA1’, ‘dsaWithSHA’, ‘sha384’, ‘md4’, ‘sha3_384’, ‘MD4’, ‘sha3_512’, ‘sha256’, ‘RIPEMD160’, ‘ripemd160’, ‘shake_256’, ‘SHA’, ‘sha3_224’, ‘dsaEncryption’, ‘SHA224’, ‘sha’, ‘SHA1’, ‘sha1’, ‘shake_128’, ‘SHA384’}

Constant attributes

  • hash.digest_size: The size of the subsequent hash in bytes.
  • hash.block_size: The inside square size of the hash calculation in bytes.
  • hash.name: The sanctioned name of this hash, consistently lowercase and always appropriate as a boundary to new() to make another hash of this sort.

Methods in hashlib

  • hash.update(data): Update the hash object with the bytes-like object.
  • hash.digest(): Return the condensation of the information went to the update() method up until now. This is a bytes object of size digest_size and can contain bytes ranging in number from 0 to 255.
  • hash.hexdigest(): Like digest() aside from the digest is returned as a string object of twofold length, containing just hexadecimal digits.
  • hash.copy(): Return a duplicate (“clone”) of the hash object. This can be utilized to effectively figure the overviews of information sharing a typical beginning sub-string.

SHAKE variable-length digests

  • shake.digest(length): Returns the condensation of the information passed to the update() function . This is a bytes object of size which may contain bytes in the entire range from 0 to 255.
  • shake.hexdigest(length): Like overview() aside from the condensation is returned as a string object of twofold length, containing just hexadecimal digits.

SHA3 in Python

A cryptographic hash function is an exceptional class of hash function that has certain properties that make it appropriate for use in cryptography. It is a numerical algorithm that maps information of self-assertive size to a piece line of a fixed size (a hash function) which is intended to likewise be a one-way output function, that is, a function which is infeasible to revert.

Similar Reads

hashlib module

To calculate the cryptographic hash value in Python, “hashlib” Module is used. The hashlib gives the following cryptographic hash functions to discover the hash output of a text as follows:...

SHA – a brief

...

SHA-3 implementation

The Secure Hash Algorithms are a group of cryptographic hash functions proposed by the National Institute of Standards and Technology (NIST) and include:...