Updated On : Feb-19,2021 Tags hashlib, secure-hash, message-digest
hashlib - Compute Secure Hashes (Message Digests) in Python

hashlib - Compute Secure Hashes (Message Digests) in Python

Computing hash is the process of applying a deterministic mathematical function to a message of any size and transform it to a bit array of fixed size. The function used to compute a hash is a one-way function hence we can not revert the transformed message to get the original message. The only way to get the original message is by trying brute force search on messages to generate a hash that matches with the hash at hand. The hash functions commonly used in cryptography are referred to as secure hash functions. Some people refer to hash as message digest as well. The secure hash has many applications like indexing data in the hashtable, digital signatures, detect duplicates, uniquely identify files, use as checksums to check for corruption, etc.

Python provides us with a module named hashlib which provides an implementation of the majority of commonly used hash functions. As a part of this tutorial, we'll explain how we can use the API of hashlib to generate hashes of messages with simple and easy-to-understand examples.

The hashlib provides a list of algorithms which will be available on all platform and some of the algorithms availability will be depended on OpenSSL library that Python uses. The hashlib provides a constructor for algorithms that are available on all platforms as well. The algorithms which have a dependency on the OpenSSL version are available through a method named new().

The hashlib provides two important attributes which let us know the list of secure hashing algorithms available on a particular system.

  • algorithms_available - It returns a list of all possible algorithms available on the platform. It'll include available through the OpenSSL library installed on the system.
  • algorithms_guaranteed - It returns a list of algorithm which are implemented in this library and are guaranteed to work. The hashlib provides constructors for each of these algorithms.

Below we have printed a list of algorithms available on the system on which this tutorial was run.

In [1]:
import hashlib

print("List of Available Algorithms to Construct Secure Hash/Message Digest : {}".format(hashlib.algorithms_available))
print("\nList of Algorithms Guaranteed to Work : {}".format(hashlib.algorithms_guaranteed))
print("\nList of Algorithms that May Work : {}".format(hashlib.algorithms_available.difference(hashlib.algorithms_guaranteed)))
List of Available Algorithms to Construct Secure Hash/Message Digest : {'sha512', 'blake2b', 'shake256', 'sha384', 'sha1', 'md5-sha1', 'sha3_384', 'sha3_256', 'sm3', 'sha224', 'whirlpool', 'sha3-512', 'shake128', 'sha3-256', 'md4', 'blake2b512', 'sha3_224', 'shake_128', 'blake2s', 'sha3_512', 'md5', 'sha512-224', 'sha512-256', 'blake2s256', 'ripemd160', 'sha256', 'sha3-224', 'mdc2', 'shake_256', 'sha3-384'}

List of Algorithms Guaranteed to Work : {'sha3_256', 'sha512', 'sha256', 'sha3_512', 'blake2b', 'sha3_224', 'sha224', 'shake_128', 'sha384', 'blake2s', 'md5', 'shake_256', 'sha1', 'sha3_384'}

List of Algorithms that May Work : {'blake2s256', 'ripemd160', 'sha512-224', 'sha3-224', 'mdc2', 'sha3-384', 'shake256', 'blake2b512', 'sm3', 'whirlpool', 'sha3-512', 'shake128', 'sha3-256', 'md4', 'md5-sha1', 'sha512-256'}

Example 1: SHA1

As a part of our first example, we'll explain how we can compute the secure hash of a given string using SHA1 hashing algorithm. The SHA1 algorithm generates 20 bytes hash of the given input message by working on a block of 64 bytes of data at a time.


  • sha1(message_bytes=None) - This constructor creates an instance of sha1 with the initial message given as bytes. It can be then used to generate a message digest. We can only create an instance of sha1 as well without any initial message. We can add messages using a call to update() method.

Important Methods of sha1 Instance

  • update(message_bytes) - This method adds messages given as input to already an existing message. We can call this method more than once and it'll keep on adding up messages.

  • digest() - It returns message digest of the data in bytes format. The size of the output is 20 bytes.

  • hexdigest() - It returns message digest of data as hexadecimal digits. The hexadecimal digest will be twice the size of bytes digest because one byte can represent two hexadecimal digits. The size of the output is 40 hexadecimal digits.

Important Attributes of sha1 Instance

  • digest_size - It returns an integer representing the number of bytes of secure hash that the algorithm will generate.
  • block_size - It returns an integer value representing the block size of the algorithm. The secure hash algorithms divide data into blocks where each block contains a number of bytes of data on which algorithm will work.

Our code for this example starts by creating an instance of sha1 with the input string defined earlier. It gives input in bytes format by calling encode() method on the input string. It then generates messages digests and prints them. The second part of the code again creates an instance of sha1 but without any initial message. It uses update() method to update the message of the algorithm. It then prints the message digest. At last, we print the digest size and block size of the algorithm.

Please make a NOTE that we'll be using secure hash and message digests terms interchangeably but both mean the same thing.

In [1]:
import hashlib

message = "Welcome to CoderzColumn."

########## 1 ##################
sha_1 = hashlib.sha1(message.encode())
message_digest1 = sha_1.digest()

print("Message Digest 1 : {}".format(message_digest1))

########## 2 ##################
sha_2 = hashlib.sha1()
sha_2.update(bytes(message, encoding="utf-8"))
message_digest2 = sha_2.digest()

print("Message Digest 2 : {}".format(message_digest2))


print("\nMessage Digest Size for 1 : {} and 2 : {}".format(sha_1.digest_size, sha_2.digest_size,))
print("Message Block  Size for 1 : {} and 2 : {}".format(sha_1.block_size, sha_2.block_size,))
Message Digest 1 : b'\xa7A\x99zW`an\x87\xf2\xf9\x1f~\xf1I6\xabix\xb3'
Message Digest 2 : b'\xa7A\x99zW`an\x87\xf2\xf9\x1f~\xf1I6\xabix\xb3'

Message Digest Size for 1 : 20 and 2 : 20
Message Block  Size for 1 : 64 and 2 : 64

Example 2: SHA1 Continued ...

As a part of our second example, we are continuing with an explanation of using SHA1 algorithm. We are using our algorithm for this part to explain how we can call update() method more than one time to give a message in parts and it'll give the same results if data is given in one go.

Our code first two parts of the code are the same as our previous example. Our third part of the code first creates an instance of sha1 and then calls update() method two times giving half of the original message each time. We then print message digest.

When we run the below code, we can notice that all message digests are equal.

Please make a NOTE that all secure hash algorithms available through hashlib require input in bytes format and will return output message digest also in bytes format.

In [66]:
import hashlib

message = "Welcome to CoderzColumn."

########## 1 ##################
sha_1 = hashlib.sha1(message.encode())
message_digest1 = sha_1.digest()

print("Message Digest 1 : {}".format(message_digest1))

########## 2 ##################
sha_2 = hashlib.sha1()
sha_2.update(bytes(message, encoding="utf-8"))
message_digest2 = sha_2.digest()

print("Message Digest 2 : {}".format(message_digest2))

########## 3 ##################
sha_3 = hashlib.sha1()
sha_3.update(bytes("Welcome to ", encoding="utf-8"))
sha_3.update(bytes("CoderzColumn.", encoding="utf-8"))
message_digest3 = sha_3.digest()

print("Message Digest 3 : {}".format(message_digest3))


print("\nMessage Digest Size for 1 : {}, 2 : {} and 3 : {}".format(sha_1.digest_size, sha_2.digest_size,sha_3.digest_size,))
print("Message Block  Size for 1 : {}, 2 : {} and 3 : {}".format(sha_1.block_size, sha_2.block_size,sha_3.block_size,))
Message Digest 1 : b'\xa7A\x99zW`an\x87\xf2\xf9\x1f~\xf1I6\xabix\xb3'
Message Digest 2 : b'\xa7A\x99zW`an\x87\xf2\xf9\x1f~\xf1I6\xabix\xb3'
Message Digest 3 : b'\xa7A\x99zW`an\x87\xf2\xf9\x1f~\xf1I6\xabix\xb3'

Message Digest Size for 1 : 20, 2 : 20 and 3 : 20
Message Block  Size for 1 : 64, 2 : 64 and 3 : 64

Example 3: SHA1 - Hex Message Digest

As a part of our third example, we are explaining how we can generate hex message digest using SHA1 algorithm.

Our code for this example is exactly the same as our previous example with the only change that we are using hexdigest() method to calculate digest instead of digest() method.

In [2]:
import hashlib

message = "Welcome to CoderzColumn."

########## 1 ##################
sha_1 = hashlib.sha1(message.encode())
hex_message_digest1 = sha_1.hexdigest()

print("Hex Message Digest 1 : {}".format(hex_message_digest1))

########## 2 ##################
sha_2 = hashlib.sha1()
sha_2.update(bytes(message, encoding="utf-8"))
hex_message_digest2 = sha_2.hexdigest()

print("Hex Message Digest 2 : {}".format(hex_message_digest2))

########## 3 ##################
sha_3 = hashlib.sha1()
sha_3.update(bytes("Welcome to ", encoding="utf-8"))
sha_3.update(bytes("CoderzColumn.", encoding="utf-8"))
hex_message_digest3 = sha_3.hexdigest()

print("Hex Message Digest 3 : {}".format(hex_message_digest3))

print("\nHex Message Digest Size for 1 : {}, 2 : {} and 3 : {}".format(sha_1.digest_size, sha_2.digest_size,sha_3.digest_size,))
print("Hex Message Block  Size for 1 : {}, 2 : {} and 3 : {}".format(sha_1.block_size, sha_2.block_size,sha_3.block_size,))
Hex Message Digest 1 : a741997a5760616e87f2f91f7ef14936ab6978b3
Hex Message Digest 2 : a741997a5760616e87f2f91f7ef14936ab6978b3
Hex Message Digest 3 : a741997a5760616e87f2f91f7ef14936ab6978b3

Hex Message Digest Size for 1 : 20, 2 : 20 and 3 : 20
Hex Message Block  Size for 1 : 64, 2 : 64 and 3 : 64

Example 4: Use of new() Method to Create Hash Algorithm Instance

As a part of our fourth example, we are demonstrating how we can use new() method to create an instance of a hashing algorithm using new() method.


  • new(name,[data]) - This constructor creates an instance of algorithm given as input by the name parameter. It provides access to algorithms for which there is no direct constructor available in hashlib and is dependent on the OpenSSL library.

Our code for this example, generates an instance of sha1 and md5 algorithms using new() method. It then generates message digests and prints them. We also print digest size and block size at the end for each algorithm. The MD5 algorithm generates a digest of size 16 bytes and works on blocks of data of size 64 bytes.

In [33]:
import hashlib

message = "Welcome to CoderzColumn."

########## 1 ##################
sha_1 = hashlib.sha1(message.encode())
sha_1_new = hashlib.new(sha_1.name, data=message.encode())
message_digest1 = sha_1_new.digest()

print("Message Digest 1 : {}".format(message_digest1))

########## 2 ##################
sha_2 = hashlib.new("sha1")
sha_2.update(bytes(message, encoding="utf-8"))
message_digest2 = sha_2.digest()

print("Message Digest 2 : {}".format(message_digest2))

########## 2 ##################
md5 = hashlib.new("md5")
md5.update(bytes(message, encoding="utf-8"))
message_digest3 = md5.digest()

print("Message Digest 3 : {}".format(message_digest3))


print("\nMessage Digest Size for 1 : {}, 2 : {}, 3 : {}".format(sha_1.digest_size, sha_2.digest_size, md5.digest_size))
print("Message Block  Size for 1 : {}, 2 : {}, 3 : {}".format(sha_1.block_size, sha_2.block_size, md5.block_size))
Message Digest 1 : b'\xa7A\x99zW`an\x87\xf2\xf9\x1f~\xf1I6\xabix\xb3'
Message Digest 2 : b'\xa7A\x99zW`an\x87\xf2\xf9\x1f~\xf1I6\xabix\xb3'
Message Digest 3 : b'\xfe\xc3v\xf7\x84\xc7\x88s\xf1\x96:w_L\xd7\xfb'

Message Digest Size for 1 : 20, 2 : 20, 3 : 16
Message Block  Size for 1 : 64, 2 : 64, 3 : 64

Example 5: SHAKE128

As a part of our fifth example, we are demonstrating the usage of SHAKE128 secure hashing algorithm. It let us compute the message digest of variable length providing security of 128 bits. The SHAKE_128 works on 128 bytes block of data at a time.


  • shake_128(message_bytes=None) - It creates and instance of shake_128 which can be used to compute digest.

Important Methods of shake_128 Instance

  • digest(digest_size_in_bytes) - It returns an message digest of given byte size.
  • hexdigest(digest_size_in_bytes) - It returns an hex message digest of given byte size. The length of the hex digest will be double the size normal digest because one byte can represent two hexadecimal digits. The number of bytes used will be the same as a normal digest.

Our code for this example creates and instances of shake_128 and generates normal and hex message digests of size 15 bytes.

In [4]:
import hashlib

message = "Welcome to CoderzColumn."

########## 1 ##################
shake_128_1 = hashlib.shake_128(message.encode())

message_digest1 = shake_128_1.digest(15)
print("Message Digest     1 : {}".format(message_digest1))

hex_message_digest1 = shake_128_1.hexdigest(15)
print("Hex Message Digest 1 : {}".format(hex_message_digest1))

########## 2 ##################
shake_128_2 = hashlib.new("shake_128")
shake_128_2.update(bytes(message, encoding="utf-8"))

message_digest2 = shake_128_2.digest(15)
print("\nMessage Digest     2 : {}".format(message_digest2))

hex_message_digest2 = shake_128_2.hexdigest(15)
print("Hex Message Digest 2 : {}".format(hex_message_digest2))


print("\nMessage Digest Size for 1 : {}, 2 : {}".format(shake_128_1.digest_size, shake_128_2.digest_size))
print("Message Block  Size for 1 : {}, 2 : {}".format(shake_128_1.block_size, shake_128_2.block_size))
Message Digest     1 : b'%I\xf4\x11\xfe\xbfs\x95\xef@5M5\xfc\x83'
30
Hex Message Digest 1 : 2549f411febf7395ef40354d35fc83

Message Digest     2 : b'%I\xf4\x11\xfe\xbfs\x95\xef@5M5\xfc\x83'
Hex Message Digest 2 : 2549f411febf7395ef40354d35fc83

Message Digest Size for 1 : 0, 2 : 0
Message Block  Size for 1 : 168, 2 : 168

Example 6: SHAKE256

As a part of our sixth example, we have explained how we can use SHAKE256 algorithm to generate secure hashes. It works like SHAKE128 but provides the security of 256 bits. All the methods of the shake_256 instance are the same as shake_128 instance.

Our code for this example is exactly the same as our previous example with the only change that we are using shake_256 algorithm for this example.

In [119]:
import hashlib

message = "Welcome to CoderzColumn."

########## 1 ##################
shake_256_1 = hashlib.shake_256(message.encode())

message_digest1 = shake_256_1.digest(15)
print("Message Digest     1 : {}".format(message_digest1))

hex_message_digest1 = shake_256_1.hexdigest(15)
print("Hex Message Digest 1 : {}".format(hex_message_digest1))

########## 2 ##################
shake_256_2 = hashlib.new("shake_256")
shake_256_2.update(bytes(message, encoding="utf-8"))

message_digest2 = shake_256_2.digest(15)
print("\nMessage Digest     2 : {}".format(message_digest2))

hex_message_digest2 = shake_256_2.hexdigest(15)
print("Hex Message Digest 2 : {}".format(hex_message_digest2))


print("\nMessage Digest Size for 1 : {}, 2 : {}".format(shake_128_1.digest_size, shake_128_2.digest_size))
print("Message Block  Size for 1 : {}, 2 : {}".format(shake_128_1.block_size, shake_128_2.block_size))
Message Digest     1 : b'3\x91\x8c\xeb\xe9\x1a?\x15\x11\x1c\xa2L\xa4\x88\x17'
Hex Message Digest 1 : 33918cebe91a3f15111ca24ca48817

Message Digest     2 : b'3\x91\x8c\xeb\xe9\x1a?\x15\x11\x1c\xa2L\xa4\x88\x17'
Hex Message Digest 2 : 33918cebe91a3f15111ca24ca48817

Message Digest Size for 1 : 0, 2 : 0
Message Block  Size for 1 : 168, 2 : 168

Example 7: Password Hashing using pbkdf2_hmac()

As a part of our seventh example, we'll demonstrate how we can use key derivation/key stretching algorithm PKCS#5 for generating a secure hash of passwords.

The reason behind using different functions based on salt input bytes for hashing passwords is to protect them against brute force attacks.


  • pbkdf2_hmac(hash_algo, password, salt, iterations,dklen=None) - It takes as input hashing algorithm name, byte sequence to use as a password, byte sequence to use like salt, number of iterations, and returns a hash of input password. If dklen parameter is provided with some number then a digest of that many bytes will be generated else digest of size will be based on hashing algorithm given as input.

Our code for this example generates secure hash of input string using pbkdf2_hmac() function with different parameter settings. All inputs are given as bytes sequence.

In [29]:
import hashlib

password = "CoderzColumn"
salt = "CC"
########## 1 ##################
hashed_pass = hashlib.pbkdf2_hmac("sha256", password=password.encode(),
                                            salt = salt.encode(),
                                            iterations=100000)

print("Password Hash ({}) : {}".format(len(hashed_pass), hashed_pass))

########## 2 ##################
hashed_pass = hashlib.pbkdf2_hmac("md4", password=password.encode(),
                                         salt = salt.encode(),
                                         iterations=100000)

print("\nPassword Hash ({}) : {}".format(len(hashed_pass), hashed_pass))

########## 3 ##################
hashed_pass = hashlib.pbkdf2_hmac("sha256", password=password.encode(),
                                            salt = salt.encode(),
                                            iterations=1000000,
                                            dklen=64)

print("\nPassword Hash ({}) : {}".format(len(hashed_pass), hashed_pass))
Password Hash (32) : b"_\xcf8y\xa1w\xa8\xfc\x19\x82\xe85p\xa2%\x96'`\x07*\xef\xf6]\x9b\x01\xc8\x81\xd5\xb8\x0c\xdc\x07"

Password Hash (16) : b'\xe6\x01\x8f\xe8\x97\x8a\x9e\x12\nk\x1a\xaa\xc7\xbb\xcd\x9e'

Password Hash (64) : b'\xc6T\xd6\xf1s\x1b!\x9b\xbe\xf4\xc9\xcc\x83\x12\xf9\xe4\xd7\x87\x12\x83\xfdOYL\xfax\xaf\x14\xd8\xaf\xc8\xce0\xb6(\x02\x9b\xb7\xe1\xee\xa2\x8a\xbc\x92\xe3\xe6\xe4\xaat\x8b\x81p=J\r"\n=\'Fo\x01\xcf\x13'

Example 8: Password Hashing using scrypt()

As a part of our eighth example, we'll explain how we can generate secure has for passwords using key derivation/key stretching algorithms. We are using scrypt() method of hashlib this time.


  • scrypt(password,salt,n,r,p,maxmem=0,dklen=64) - It takes as input password and other parameter values and generates hash of password.
    • The parameter n is a value representing the CPU/Memory cost factor.
    • The parameter r represents the block size of the algorithm.
    • The parameter p represents the parallelization factor.
    • The parameter maxmem accepts a number that represents an upper bound on memory usage by the algorithm. The default value is 0 hence no limitation.
    • The parameter dklen represents the required digest size in bytes. The default is 64 bytes.

Our code for this example generates a secure hash of the password using scrypt() method. It tries different parameter combinations to generate the hash.

In [27]:
import hashlib

password = "CoderzColumn"
salt = "CC"
########## 1 ##################
hashed_pass = hashlib.scrypt(password=password.encode(),
                             salt = salt.encode(),
                             n=2,
                             r=16,
                             p=1
                             )

print("Password Hash ({}) : {}".format(len(hashed_pass), hashed_pass))

########## 2 ##################
hashed_pass = hashlib.scrypt(password=password.encode(),
                                  salt = salt.encode(),
                                  n=4,
                                  r=8,
                                  p=1
                                  )

print("\nPassword Hash ({}) : {}".format(len(hashed_pass), hashed_pass))

########## 3 ##################
hashed_pass = hashlib.scrypt(password=password.encode(),
                             salt = salt.encode(),
                             n=2,
                             r=16,
                             p=1,
                             dklen=32)

print("\nPassword Hash ({}) : {}".format(len(hashed_pass), hashed_pass))
Password Hash (64) : b'\x85\x81a\x80-0\x12\x0e{T\x1c\xf8\x01D.}\x85\xc5\xbb\xb2\x1c\xb3\x1d\xe1\x1a\xcf\x9b\xfc\xd9\xaa\x0bUPM`\x90\x13\x81h\x1cp\xdbv+\xe3I\xe3\xf1\x0eY\x0e\xc63m\x97f5\xe4P\xc4\xfa\xdd\x8c\xe8'

Password Hash (64) : b'\x8b|\x87m\xf0D\xb2\xcf\x89\xaa\xe4\xfb\xa2kQh\x91~:\xa7M%kGA\x17\x1f\x080N\xaa\x98\xa3`\xdeN\xc2\xefC^\x92\x9d8\xcd=\xe1H\xd6<\xfb:(\x8fH\x92\xe4\x07\x18\xed;\xfdh)P'

Password Hash (32) : b'\x85\x81a\x80-0\x12\x0e{T\x1c\xf8\x01D.}\x85\xc5\xbb\xb2\x1c\xb3\x1d\xe1\x1a\xcf\x9b\xfc\xd9\xaa\x0bU'

Example 9: Blake2B

As a part of our ninth example, we are demonstrating how we can generate a secure hash of the given message using Blake2B algorithm.


  • blake2b(data=b'',digest_size=64,key=b'',salt=b'',person=b'',fanout=1,depth=1,leaf_size=0,node_offset=0,node_depth=0,inner_size=0,last_node=False,usedforsecurity=False) - This constructor creates an instance of blake2b instance which can be used to generate secure hash.
    • The data parameter accepts data for which we want to generate a secure hash in bytes format.
    • The digest_size accepts number specifying message digest size in bytes.
    • The key accepts key in bytes format for keyed hashing.
    • The salt accepts a salt string in bytes format for randomizing hashing.
    • The person parameter accepts bytes for personalization.

Our code for this example has three parts.

The first part creates an instance of blake2b with just a message as input and all other parameters are left with their default values. We are then generating message digest and hex message digests as well.

Our second part of the code is generating an instance of blake2b with the message, digest size, key, and salt parameters set. We are then generating message digest and hex message digest using this instance.

Our third part of the code is generating an instance of blake2b using new() method described earlier.

At last, we are printing digest size and block size for each instance.

In [136]:
import hashlib

message = "Welcome to CoderzColumn."

########## 1 ##################
blake2b_1 = hashlib.blake2b(message.encode())

message_digest1 = blake2b_1.digest()
print("Message Digest     1 : {}".format(message_digest1))

hex_message_digest1 = blake2b_1.hexdigest()
print("Hex Message Digest 1 : {}".format(hex_message_digest1))

########## 2 ##################
key = bytes("python", encoding="utf-8")
salt = bytes("ml", encoding="utf-8")
print("\nKey Size : {}, Salt Size : {}".format(len(key), len(salt)))
blake2b_2 = hashlib.blake2b(message.encode(), digest_size=32, key=key, salt=salt)

message_digest2 = blake2b_2.digest()
print("\nMessage Digest     2 : {}".format(message_digest2))

hex_message_digest2 = blake2b_2.hexdigest()
print("Hex Message Digest 2 : {}".format(hex_message_digest2))

########## 3 ##################
blake2b_3 = hashlib.new("blake2b", digest_size=32, key=key, salt=salt)
blake2b_3.update(bytes(message, encoding="utf-8"))

message_digest3 = blake2b_3.digest()
print("\nMessage Digest     2 : {}".format(message_digest3))

hex_message_digest2 = blake2b_3.hexdigest()
print("Hex Message Digest 2 : {}".format(message_digest3))


print("\nMessage Digest Size for 1 : {}, 2 : {}, 3 : {}".format(blake2b_1.digest_size, blake2b_2.digest_size, blake2b_3.digest_size))
print("Message Block  Size for 1 : {}, 2 : {}, 3 : {}".format(blake2b_1.block_size, blake2b_2.block_size, blake2b_3.block_size))
Message Digest     1 : b'\x1e\xd1D\'\xea\xf9\x96o\xfc\x0f\x86\x8f\xe2\x1f\xa8\xbe\xcf-\x1fF<A\x89o\x93\'\x19\xe47\xb7\xc4=\xcc\x86%A\x83F\x04r\xba\t(E\xd5\xae\xcfPQ\xe1r7\xd5\xab\x04w\xb6d\xeeF\x82_"b'
Hex Message Digest 1 : 1ed14427eaf9966ffc0f868fe21fa8becf2d1f463c41896f932719e437b7c43dcc86254183460472ba092845d5aecf5051e17237d5ab0477b664ee46825f2262

Key Size : 6, Salt Size : 2

Message Digest     2 : b'\x11\x86\xb1\x0f\x9b\xac\xcb\xcc\x19/zX\xa0?~{oDk\x97\x07\x05\x04\xf1\xa1@\x89\xb9\x92\xe6\x8b\xb8'
Hex Message Digest 2 : 1186b10f9baccbcc192f7a58a03f7e7b6f446b97070504f1a14089b992e68bb8

Message Digest     2 : b'\x11\x86\xb1\x0f\x9b\xac\xcb\xcc\x19/zX\xa0?~{oDk\x97\x07\x05\x04\xf1\xa1@\x89\xb9\x92\xe6\x8b\xb8'
Hex Message Digest 2 : b'\x11\x86\xb1\x0f\x9b\xac\xcb\xcc\x19/zX\xa0?~{oDk\x97\x07\x05\x04\xf1\xa1@\x89\xb9\x92\xe6\x8b\xb8'

Message Digest Size for 1 : 64, 2 : 32, 3 : 32
Message Block  Size for 1 : 128, 2 : 128, 3 : 128

Example 10: Blake2S

As a part of our tenth example, we are demonstrating how we can generate message digest using Blake2S algorithm.


  • blake2s(data=b'',digest_size=32,key=b'',salt=b'',person=b'',fanout=1,depth=1,leaf_size=0,node_offset=0,node_depth=0,inner_size=0,last_node=False,usedforsecurity=False) - This constructor creates an instance of blake2s instance which can be used to generate secure hash.

The blake2s algorithm has the same signature as blake2b with only a change in default digest size of 32 instead of 64.


Our code for this example is exactly the same as our previous example with the only minor change that we are using blake2s instance for generating message digest in this example instead.

In [137]:
import hashlib

message = "Welcome to CoderzColumn."

########## 1 ##################
blake2s_1 = hashlib.blake2s(message.encode())

message_digest1 = blake2s_1.digest()
print("Message Digest     1 : {}".format(message_digest1))

hex_message_digest1 = blake2s_1.hexdigest()
print("Hex Message Digest 1 : {}".format(hex_message_digest1))

########## 2 ##################
key = bytes("python", encoding="utf-8")
salt = bytes("ml", encoding="utf-8")
print("\nKey Size : {}, Salt Size : {}".format(len(key), len(salt)))
blake2s_2 = hashlib.blake2s(message.encode(), digest_size=32, key=key, salt=salt)

message_digest2 = blake2s_2.digest()
print("\nMessage Digest     2 : {}".format(message_digest2))

hex_message_digest2 = blake2s_2.hexdigest()
print("Hex Message Digest 2 : {}".format(hex_message_digest2))

########## 3 ##################
blake2s_3 = hashlib.new("blake2s", digest_size=32, key=key, salt=salt)
blake2s_3.update(bytes(message, encoding="utf-8"))

message_digest3 = blake2s_3.digest()
print("\nMessage Digest     2 : {}".format(message_digest3))

hex_message_digest2 = blake2s_3.hexdigest()
print("Hex Message Digest 2 : {}".format(message_digest3))


print("\nMessage Digest Size for 1 : {}, 2 : {}, 3 : {}".format(blake2s_1.digest_size, blake2s_2.digest_size, blake2s_3.digest_size))
print("Message Block  Size for 1 : {}, 2 : {}, 3 : {}".format(blake2s_1.block_size, blake2s_2.block_size, blake2s_3.block_size))
Message Digest     1 : b'(\xac\xd2\x98\x18\xab\x03c\xbdb\xef\xd3\xa1\xc4RX\x08\x9f.u61\xefS\xe6S\x98:\x1f\xa7\xaa\x10'
Hex Message Digest 1 : 28acd29818ab0363bd62efd3a1c45258089f2e753631ef53e653983a1fa7aa10

Key Size : 6, Salt Size : 2

Message Digest     2 : b'\x9f\x00M\xb3\x13%\xc1\xdd}\x0e\xe4\xd81y\x10K\xf1\x18\xe7k$V\xec\xc5k|\x1a*\xdf*F\x1a'
Hex Message Digest 2 : 9f004db31325c1dd7d0ee4d83179104bf118e76b2456ecc56b7c1a2adf2a461a

Message Digest     2 : b'\x9f\x00M\xb3\x13%\xc1\xdd}\x0e\xe4\xd81y\x10K\xf1\x18\xe7k$V\xec\xc5k|\x1a*\xdf*F\x1a'
Hex Message Digest 2 : b'\x9f\x00M\xb3\x13%\xc1\xdd}\x0e\xe4\xd81y\x10K\xf1\x18\xe7k$V\xec\xc5k|\x1a*\xdf*F\x1a'

Message Digest Size for 1 : 32, 2 : 32, 3 : 32
Message Block  Size for 1 : 64, 2 : 64, 3 : 64

Example 11

As a part of our eleventh example, we are just looping through all guaranteed available algorithms, create instances of them, and generate normal and hex message digest for the input string. We are printing message digests generated by all algorithms along with their digest size and name as well.

In [124]:
message = "Welcome to CoderzColumn."

for algorithm in hashlib.algorithms_guaranteed:
    hashing_algo = hashlib.new(algorithm, message.encode())

    print("Algorithm                             : {}".format(algorithm))
    if algorithm in ["shake_128", "shake_256"]:
        message_digest1 = hashing_algo.digest(10)
    else:
        message_digest1 = hashing_algo.digest()
    print("Message Digest (Digest Size : {})     : {}".format(len(message_digest1), message_digest1))

    if algorithm in ["shake_128", "shake_256"]:
        hex_message_digest1 = hashing_algo.hexdigest(10)
    else:
        hex_message_digest1 = hashing_algo.hexdigest()
    print("Hex Message Digest (Digest Size : {}) : {}".format(len(hex_message_digest1),hex_message_digest1))
    print("="*80, "\n")
Algorithm                             : sha1
Message Digest (Digest Size : 20)     : b'\xa7A\x99zW`an\x87\xf2\xf9\x1f~\xf1I6\xabix\xb3'
Hex Message Digest (Digest Size : 40) : a741997a5760616e87f2f91f7ef14936ab6978b3
================================================================================

Algorithm                             : sha3_256
Message Digest (Digest Size : 32)     : b"\xeb\x17\x95=\x99$4r\x0f\xfb\\\x0e'7\x1a\x84\x05\xf5$\xdaI\xd7\x8f\xeb\x1c\xad\x06\xc1>\xc76\x01"
Hex Message Digest (Digest Size : 64) : eb17953d992434720ffb5c0e27371a8405f524da49d78feb1cad06c13ec73601
================================================================================

Algorithm                             : sha3_384
Message Digest (Digest Size : 48)     : b'\xdb\xb1\xb1\xaf\x9b\xddIb\x99\xb5p4\x1ax\x00\xcf\xfe\xc4\x153\xa7\xf1\xe2\xdc\xd6\xb1\r\xc1\xa3Dn8\xd7\x8b\xd99W"\xee%k\x7f\x91\xabF\xa6O\xbb'
Hex Message Digest (Digest Size : 96) : dbb1b1af9bdd496299b570341a7800cffec41533a7f1e2dcd6b10dc1a3446e38d78bd9395722ee256b7f91ab46a64fbb
================================================================================

Algorithm                             : sha3_224
Message Digest (Digest Size : 28)     : b'\x00\xa8d\x976O\x17}\x17/\x87\xf6|\x19l\x90r1b\xad8\xf2Eu\x05\x127\xeb'
Hex Message Digest (Digest Size : 56) : 00a86497364f177d172f87f67c196c90723162ad38f24575051237eb
================================================================================

Algorithm                             : shake_256
Message Digest (Digest Size : 10)     : b'3\x91\x8c\xeb\xe9\x1a?\x15\x11\x1c'
Hex Message Digest (Digest Size : 20) : 33918cebe91a3f15111c
================================================================================

Algorithm                             : sha512
Message Digest (Digest Size : 64)     : b'xFY\xb0\x9dIu\x022\x1b\xddp1\xa1\xd6\xef:c\x84F8\x11+#\x0e@\xedf\x809\xf2\xd3E\xd6L\xbcI\xad\xcd\x0c9]\xd9\xc7\xe1\x9f\x15h\xdc\xf9\xe1\xc4%-:\xed\xaa\xa8\xb2b\xc4\xaa!\x8b'
Hex Message Digest (Digest Size : 128) : 784659b09d497502321bdd7031a1d6ef3a63844638112b230e40ed668039f2d345d64cbc49adcd0c395dd9c7e19f1568dcf9e1c4252d3aedaaa8b262c4aa218b
================================================================================

Algorithm                             : md5
Message Digest (Digest Size : 16)     : b'\xfe\xc3v\xf7\x84\xc7\x88s\xf1\x96:w_L\xd7\xfb'
Hex Message Digest (Digest Size : 32) : fec376f784c78873f1963a775f4cd7fb
================================================================================

Algorithm                             : sha3_512
Message Digest (Digest Size : 64)     : b'\x9d\xc4\xfd\xbb\x0c O+\xa4\xdb.\x05\xba\xd7\x94\xae\x86\x83\xd8u\x18\x0c\x14149I\x14enO>\xf8zJy1\xf6\x0b\x0b\xcam\x97\x82\xd3\x91\xcf=\x9cl+\x8b/W\x18\x19\xa1\xa6Y\x10j(Y\xf4'
Hex Message Digest (Digest Size : 128) : 9dc4fdbb0c204f2ba4db2e05bad794ae8683d875180c143134394914656e4f3ef87a4a7931f60b0bca6d9782d391cf3d9c6c2b8b2f571819a1a659106a2859f4
================================================================================

Algorithm                             : sha224
Message Digest (Digest Size : 28)     : b'9\x83\xab\x8d\x7f\x03\xc7$\xaf\xa0\x98Z\rZs\xf7\xfc\x16/\xcf\x8dG\xde\x11\xab\xaccG'
Hex Message Digest (Digest Size : 56) : 3983ab8d7f03c724afa0985a0d5a73f7fc162fcf8d47de11abac6347
================================================================================

Algorithm                             : blake2s
Message Digest (Digest Size : 32)     : b'(\xac\xd2\x98\x18\xab\x03c\xbdb\xef\xd3\xa1\xc4RX\x08\x9f.u61\xefS\xe6S\x98:\x1f\xa7\xaa\x10'
Hex Message Digest (Digest Size : 64) : 28acd29818ab0363bd62efd3a1c45258089f2e753631ef53e653983a1fa7aa10
================================================================================

Algorithm                             : blake2b
Message Digest (Digest Size : 64)     : b'\x1e\xd1D\'\xea\xf9\x96o\xfc\x0f\x86\x8f\xe2\x1f\xa8\xbe\xcf-\x1fF<A\x89o\x93\'\x19\xe47\xb7\xc4=\xcc\x86%A\x83F\x04r\xba\t(E\xd5\xae\xcfPQ\xe1r7\xd5\xab\x04w\xb6d\xeeF\x82_"b'
Hex Message Digest (Digest Size : 128) : 1ed14427eaf9966ffc0f868fe21fa8becf2d1f463c41896f932719e437b7c43dcc86254183460472ba092845d5aecf5051e17237d5ab0477b664ee46825f2262
================================================================================

Algorithm                             : sha256
Message Digest (Digest Size : 32)     : b'w\xa4\x18\\\xda\tP\x82\xdd\x9a\xc1\xf2\x86\xdd\xa6nX\x81\x99\x97\xe4"\xe0\x0b\xa81\xbat\xa3;\tj'
Hex Message Digest (Digest Size : 64) : 77a4185cda095082dd9ac1f286dda66e58819997e422e00ba831ba74a33b096a
================================================================================

Algorithm                             : shake_128
Message Digest (Digest Size : 10)     : b'%I\xf4\x11\xfe\xbfs\x95\xef@'
Hex Message Digest (Digest Size : 20) : 2549f411febf7395ef40
================================================================================

Algorithm                             : sha384
Message Digest (Digest Size : 48)     : b'^O\xff\x9b8\xfb \x95\xe9\x9e^\xf3\xc4\xe7\x11\xc3\x1a\xb0\xcf\xe9\x00\xd2\x1b\xefn\xa2_a\xb1\xb2S\xf2y\xeb`\x0e`\x81\xaa\xa3\x91\xd8G\xdd\x7f\x89^*'
Hex Message Digest (Digest Size : 96) : 5e4fff9b38fb2095e99e5ef3c4e711c31ab0cfe900d21bef6ea25f61b1b253f279eb600e6081aaa391d847dd7f895e2a
================================================================================

Example 12

As a part of our eleventh example, we are just looping through all algorithms which are available through the OpenSSL library, create an instance of them and generate a normal and hex message digest for the input string. We are printing message digests generated by all algorithms along with their digest size and name as well. We have found out a list of OpenSSL algorithms by doing a set difference between algorithms_available and algorithms_guaranteed attributes of hashlib.

In [108]:
message = "Welcome to CoderzColumn."
may_be_algorithms = hashlib.algorithms_available.difference(hashlib.algorithms_guaranteed)

for algorithm in may_be_algorithms:
    hashing_algo = hashlib.new(algorithm, message.encode())

    print("Algorithm                             : {}".format(algorithm))
    if algorithm in ["shake_128", "shake_256"]:
        message_digest1 = hashing_algo.digest(10)
    else:
        message_digest1 = hashing_algo.digest()
    print("Message Digest (Digest Size : {})     : {}".format(len(message_digest1), message_digest1))

    if algorithm in ["shake_128", "shake_256"]:
        hex_message_digest1 = hashing_algo.hexdigest(10)
    else:
        hex_message_digest1 = hashing_algo.hexdigest()
    print("Hex Message Digest (Digest Size : {}) : {}".format(len(hex_message_digest1),hex_message_digest1))
    print("="*80, "\n")
Algorithm                             : shake128
Message Digest (Digest Size : 16)     : b'%I\xf4\x11\xfe\xbfs\x95\xef@5M5\xfc\x83\x8e'
Hex Message Digest (Digest Size : 32) : 2549f411febf7395ef40354d35fc838e
================================================================================

Algorithm                             : mdc2
Message Digest (Digest Size : 16)     : b'P\x0f\xe0\xea~\xa8\x9c\x10\xab$\x82\xc3\x1b\x83X\x85'
Hex Message Digest (Digest Size : 32) : 500fe0ea7ea89c10ab2482c31b835885
================================================================================

Algorithm                             : blake2b512
Message Digest (Digest Size : 64)     : b'\x1e\xd1D\'\xea\xf9\x96o\xfc\x0f\x86\x8f\xe2\x1f\xa8\xbe\xcf-\x1fF<A\x89o\x93\'\x19\xe47\xb7\xc4=\xcc\x86%A\x83F\x04r\xba\t(E\xd5\xae\xcfPQ\xe1r7\xd5\xab\x04w\xb6d\xeeF\x82_"b'
Hex Message Digest (Digest Size : 128) : 1ed14427eaf9966ffc0f868fe21fa8becf2d1f463c41896f932719e437b7c43dcc86254183460472ba092845d5aecf5051e17237d5ab0477b664ee46825f2262
================================================================================

Algorithm                             : sha512-256
Message Digest (Digest Size : 32)     : b"\xbb\xe0I\xab\xb7%\xf4'\xbf\x94]\xc58\x89\xc7\x8b\xb0\xf8\xf6\x04\xc0C\xac\xee\xea\x8f\xcd\xa9\xdc\xa0\x92N"
Hex Message Digest (Digest Size : 64) : bbe049abb725f427bf945dc53889c78bb0f8f604c043aceeea8fcda9dca0924e
================================================================================

Algorithm                             : shake256
Message Digest (Digest Size : 32)     : b'3\x91\x8c\xeb\xe9\x1a?\x15\x11\x1c\xa2L\xa4\x88\x17\xcf\xa0\xb7\xae\xfa\x07\xf8\x04\xe99\x9e\x14\xa2\xf3\x10I\xea'
Hex Message Digest (Digest Size : 64) : 33918cebe91a3f15111ca24ca48817cfa0b7aefa07f804e9399e14a2f31049ea
================================================================================

Algorithm                             : md4
Message Digest (Digest Size : 16)     : b'\xaa\x8b\x01\x0f\xef\x8a}YtZ\xc3\xa0\xe5\xacu\x86'
Hex Message Digest (Digest Size : 32) : aa8b010fef8a7d59745ac3a0e5ac7586
================================================================================

Algorithm                             : sha512-224
Message Digest (Digest Size : 28)     : b')\xf6\xceZ\xcekd\xd9G\xbf\x97~\x05\x0e\x88\xa6\xc3m\x1a<\x1f\x1f\xb0Hm@\x07\x12'
Hex Message Digest (Digest Size : 56) : 29f6ce5ace6b64d947bf977e050e88a6c36d1a3c1f1fb0486d400712
================================================================================

Algorithm                             : sha3-256
Message Digest (Digest Size : 32)     : b"\xeb\x17\x95=\x99$4r\x0f\xfb\\\x0e'7\x1a\x84\x05\xf5$\xdaI\xd7\x8f\xeb\x1c\xad\x06\xc1>\xc76\x01"
Hex Message Digest (Digest Size : 64) : eb17953d992434720ffb5c0e27371a8405f524da49d78feb1cad06c13ec73601
================================================================================

Algorithm                             : blake2s256
Message Digest (Digest Size : 32)     : b'(\xac\xd2\x98\x18\xab\x03c\xbdb\xef\xd3\xa1\xc4RX\x08\x9f.u61\xefS\xe6S\x98:\x1f\xa7\xaa\x10'
Hex Message Digest (Digest Size : 64) : 28acd29818ab0363bd62efd3a1c45258089f2e753631ef53e653983a1fa7aa10
================================================================================

Algorithm                             : sha3-512
Message Digest (Digest Size : 64)     : b'\x9d\xc4\xfd\xbb\x0c O+\xa4\xdb.\x05\xba\xd7\x94\xae\x86\x83\xd8u\x18\x0c\x14149I\x14enO>\xf8zJy1\xf6\x0b\x0b\xcam\x97\x82\xd3\x91\xcf=\x9cl+\x8b/W\x18\x19\xa1\xa6Y\x10j(Y\xf4'
Hex Message Digest (Digest Size : 128) : 9dc4fdbb0c204f2ba4db2e05bad794ae8683d875180c143134394914656e4f3ef87a4a7931f60b0bca6d9782d391cf3d9c6c2b8b2f571819a1a659106a2859f4
================================================================================

Algorithm                             : sm3
Message Digest (Digest Size : 32)     : b'\x11\x98\xd5\x0fEqO\x84\xc7\x82\xd1!\xfdgK\xb3y\xc8\x00H6D\x8c\xdaZA\xff\x01A\xdb\xe2D'
Hex Message Digest (Digest Size : 64) : 1198d50f45714f84c782d121fd674bb379c8004836448cda5a41ff0141dbe244
================================================================================

Algorithm                             : sha3-384
Message Digest (Digest Size : 48)     : b'\xdb\xb1\xb1\xaf\x9b\xddIb\x99\xb5p4\x1ax\x00\xcf\xfe\xc4\x153\xa7\xf1\xe2\xdc\xd6\xb1\r\xc1\xa3Dn8\xd7\x8b\xd99W"\xee%k\x7f\x91\xabF\xa6O\xbb'
Hex Message Digest (Digest Size : 96) : dbb1b1af9bdd496299b570341a7800cffec41533a7f1e2dcd6b10dc1a3446e38d78bd9395722ee256b7f91ab46a64fbb
================================================================================

Algorithm                             : sha3-224
Message Digest (Digest Size : 28)     : b'\x00\xa8d\x976O\x17}\x17/\x87\xf6|\x19l\x90r1b\xad8\xf2Eu\x05\x127\xeb'
Hex Message Digest (Digest Size : 56) : 00a86497364f177d172f87f67c196c90723162ad38f24575051237eb
================================================================================

Algorithm                             : md5-sha1
Message Digest (Digest Size : 36)     : b'\xfe\xc3v\xf7\x84\xc7\x88s\xf1\x96:w_L\xd7\xfb\xa7A\x99zW`an\x87\xf2\xf9\x1f~\xf1I6\xabix\xb3'
Hex Message Digest (Digest Size : 72) : fec376f784c78873f1963a775f4cd7fba741997a5760616e87f2f91f7ef14936ab6978b3
================================================================================

Algorithm                             : ripemd160
Message Digest (Digest Size : 20)     : b'\xadhzf\xdel\xe1O\xda\x06W\xeeQ\xd2\x10C\x0c\x0e\x0e\xb3'
Hex Message Digest (Digest Size : 40) : ad687a66de6ce14fda0657ee51d210430c0e0eb3
================================================================================

Algorithm                             : whirlpool
Message Digest (Digest Size : 64)     : b'\ta\x89~,1\x1c\xc0X\x8e`\xedJ\x98\xea\x00\xa3\xea\xbf\x12\x8d"B\xf4Dh\xe5\x99X\x81;2\xd86=\xe5\xa2\x1e\xe9\x15[\xb3\xcc\xdf\xe4"\xc6V\x90\x99\xdc\xe2\xe2-\xe8\xb2R\xc2H\x1a*\xdc\xdb\xbb'
Hex Message Digest (Digest Size : 128) : 0961897e2c311cc0588e60ed4a98ea00a3eabf128d2242f44468e59958813b32d8363de5a21ee9155bb3ccdfe422c6569099dce2e22de8b252c2481a2adcdbbb
================================================================================

This ends our small tutorial explaining the API of hashlib module to generate the secure hash. Please feel free to let us know your views in the comments section.

References



Sunny Solanki  Sunny Solanki