SoFunction
Updated on 2024-11-21

Explanation of the hashlib module in Python

Module Introduction

1. This module implements a common interface for many different secure hash and message digest algorithms.

2. What is hash (Hash)? Hash, is any length of the input through the hash algorithm into a fixed length of the output, simply put, through the function, the plaintext data into ciphertext data to achieve the role of encryption.

3. Each type of hash has a constructor method that returns a hash object with the same simple interface. For example, use sha256() to create a SHA-256 hash object.

4. Then use the update() method to enter 'byte-like objects (usually bytes)' into this object. At any time you can use the digest() or hexdigest() method to get a digest of the stitched data entered into this object so far.

hashilib constructor

Common hash algorithm constructors in this module

sha1(), sha224(), sha256(), sha384(), sha512(), blake2b() and blake2s(). md5()

Also available on most platforms are

sha3_224(), sha3_256(), sha3_384(), sha3_512(), shake_128(), shake_256() and so on.

If we need to get the constructor object, we can just introduce the module hashilib and generate it in a pointwise manner, as in the following example.

code example

        import hashlib
        h = hashlib.md5()
        print(h)

Print results

<md5 _hashlib.HASH object @ 0x00000233D317EAD0>

For example, take md5 as an example, find the algorithm constructor name after the module name with a dot to generate the algorithm constructor object.

() - creates the object

A generic constructor that accepts the string name of the desired algorithm as its first formal parameter.

Entering the name string of the hash constructor algorithm constructor in the new() brackets is equivalent to calling the constructor to create an object. This method has the same result as generating a constructor object with a dot as written above.

grammatical format

(name, [data, ]*, usedforsecurity=True)

code example

import hashlib		
		h = ('md5')
		print(h)

Print results

<md5 _hashlib.HASH object @ 0x0000025FC95EEAD0>

hash.digest_size

The size of the resulting hash object in bytes.

code example

import hashlib
		h = hashlib.md5()
		res1 = h.digest_size
		print(res1)

Print results

16

Returns the binary size of this constructor, md5 in hexadecimal.

hash.block_size

The internal block size of the hash algorithm expressed in bytes.

code example

import hashlib
		h = hashlib.md5()
		res1 = h.block_size
		print(res1)

Print results

64

() - pass in the parameters

Use the byte type to update the hash object.

Code Sample I

import hashlib
		h = hashlib.md5()
		(b'abcdefg')  # Pass byte-type objects into hash objects
		res = ()
		print(res)

Print results

7ac66c0f148de9519b8bd264312c4d64

1. call md5() to generate a hash object, use the method update input need to be encrypted object, need to pay attention to must be 'byte-type data'.

2. How to get a byte-type object? You can use the built-in method encode() to convert a string to byte. Or, when the string is English or purely numeric, it can be converted to byte by adding 'b' outside the quotes of the string.

Repeated calls are equivalent to a single call with all arguments passed in: (a); (b) is equivalent to (a+b)

Code Sample Two

import hashlib
		h = hashlib.md5()
		(b'abcd')  # Pass b'abcd' once and b'efg' again and it's the same as passing b'abcdefg' once.
		(b'efg')
		res = ()
		print(res)

Print results

7ac66c0f148de9519b8bd264312c4d64

The result is the same as the value in Example One.

() - returns the byte string object

Returns a digest of the data currently passed to the update() method. This is a byte string object of size digest_size.

This means that the encrypted byte string object is returned.

code example

import hashlib
		h = hashlib.md5()
		(b'abcdefg')
		res = ()
		print(res)

Print results

b'z\xc6l\x0f\x14\x8d\xe9Q\x9b\x8b\xd2d1,Md'

() - returns a string object

Similar to digest() but the digest is returned as a twice-length string object containing only hexadecimal digits. This can be used to securely exchange data values in email or other non-binary environments.

code example

    import hashlib
		h = hashlib.md5()
		(b'abcdefg')
		res = ()
		print(res)

Print results

7ac66c0f148de9519b8bd264312c4d64

cryptographic complement

The data is encrypted and cannot be decrypted directly, the decryption on the website is usually a brute force decryption, constantly guessing and trying to possibly get a result.

salting

We can also make it more difficult to crack the data by adding a salt operation, which is actually adding other data to the data that is actually going to be encrypted.

code example

import hashlib
		h = hashlib.md5()
		('You're so handsome'.encode('utf8'))  # Assumed to be real data
		(b'asgxfajchdvb')  # Randomly typed data
		res = ()
		print(res)

Print results

ef77c21d860c3ccf4aed6e52720c8e42

Because multiple calls to update give the same result as a single call, we can separate the true from the false data.

If the fake data is removed, the result is: 195eaff9c88bceae9f094f5ef322e5da So an upgrade to the level of encryption is realized.

Dynamic salt addition

Instead of writing fake data to death, we can implement dynamic salting through variables, the variable values can be time, part of the username...

code example

import hashlib
		x = 'asgxfajchdvb'
		h = hashlib.md5()
		('You're so handsome'.encode('utf8'))
		(('utf8'))
		res = ()
		print(res)

Print results

ef77c21d860c3ccf4aed6e52720c8e42

Same result as writing it to death.

Cryptographic Application Scenarios

1. How password encryption is compared

The user input is plaintext but after the program will use the same encryption algorithm to become ciphertext, and then take the ciphertext and with the database inside the ciphertext comparison if it is the same is the correct password is not the same is the error

2. Document content consistency checking

As a software provider, the content of the security software will be encrypted at the same time to get a unique ciphertext of the security software, and the user will do the same encryption of the content after downloading the software and compare the ciphertexts of the two times to see if they are the same; if they are the same, it means that the content has not been modified; if they are not, it means that the content has been modified in the middle of the process, and viruses may be present.

3. Optimization strategies for consistency checking of large files

If a file is very large, then if all read and encrypted speed is too slow, this time you can consider the contents of the file sliced read and encrypted operations

to this article on Python hashlib module analysis of the article is introduced to this, more related Python hashlib module content, please search my previous posts or continue to browse the following related articles I hope you will support me in the future!