SoFunction
Updated on 2024-11-19

Detailed summary algorithm for the hashlib module in Python

hashlib module

Python's hashlib provides common digest algorithms such as MD5, SHA1, and so on.

What is a summary algorithm?

The digest algorithm is also known as hash algorithm and hash algorithm. It converts data of arbitrary length into a data string of fixed length (usually represented as a hexadecimal string) through a function.

What makes the digest algorithm able to point out if the data has been tampered with is that the digest function is a one-way function; it is easy to compute f(data), but very difficult to backtrack data through digest.

Moreover, a single bit of modification to the original data can result in a completely different calculated summary.

typical example

Let's take the common digest algorithm MD5 as an example to calculate the MD5 value of a string:

import hashlib
md5 = hashlib.md5()
('my name is leizi'.encode('utf-8'))
print(())

Then the calculated md5 is

d7d8c24cddfb4c15b83db713badda3d3

If you have a large amount of data, you can call update() multiple times in chunks, and the final calculation will be the same:

 
import hashlib
md5 = hashlib.md5()
('my name is leizi'.encode('utf-8'))
(" Welcome to Sad Spice.".encode('utf-8'))
print(())

final result

96f4063331ea2c477fb3d7e980d73404

Try changing one letter and see if the calculation is completely different.

import hashlib
md5 = hashlib.md5()
('my name is leizi1'.encode('utf-8'))
(" Welcome to Sad Spice ".encode('utf-8'))
print(())

Results Printing

57488933f9d74f5404533d111cbbf4e4

MD5 is the most common digest algorithm, is fast, and produces a fixed 128 bit/16 byte result, usually represented as a 32 bit hexadecimal string.

Another common digest algorithm is SHA1, and calling SHA1 is exactly similar to calling MD5:

import hashlib
sha1 = hashlib.sha1()
('my name is leizi '.encode('utf-8'))
('Welcome to the sad chorizo'.encode('utf-8'))
print(())

Results:

abbe3ae3d30736838602e9d119a2dfe016577a07

Commonly used attributes

# List all encryption algorithms
h.digest_size
# Size of hash bytes generated.
h.block_size
#The size of the block inside the hash

Common methods

([arg])
# Create a hash object with the specified encryption mode
(arg)
# Update the hash object with a string argument. If this method is called repeatedly on the same hash object, (a); (b) is equivalent to (a+b)
()
# Returns a summary as a binary data string value.
()
# Returns a summary as a hexadecimal data string value
()
# make a copy of

Features of hashlib

1. Summary algorithms are widely used in many places.

2, it should be noted that the digest algorithm is not a cryptographic algorithm, can not be used for encryption (because it can not be deduced from the digest back to the plaintext), can only be used for tamper-proof.

3, its unidirectional computing characteristics determine the user password can be verified without storing the plaintext password.

A good hash algorithm that will enable:

  • Forward fast: given plaintext and a hash algorithm, the hash value can be computed in a finite amount of time and with finite resources.
  • Reversal Difficulty: given (a number of) hash values, it is difficult (essentially impossible) to reverse the plaintext in a finite amount of time.
  • Input sensitivity: the original input information should all look very different when modified by a little bit of information, resulting in a hash value.
  • Conflict avoidance: It is very difficult to find two plaintexts with different contents that have the same hash value (conflict). That is, for any two different blocks of data, its hash value of the same possibility is very small; for a given block of data, find and its hash value of the same block of data is extremely difficult.

In addition to the above, the ones we listed, the library has these encryption methods:

  • sha224()
  • sha256()
  • sha384()
  • sha512()
  • blake2b()
  • blake2s()
  • sha3_224
  • sha3_256
  • sha3_384
  • sha3_512
  • shake_128
  • shake_256
  • wait a minute!

The main application scenarios, that is, our password encryption and plaintext encryption, in accordance with certain conventions to form our encryption can be, some times in order to avoid too simple encryption, we will agree on a private key to increase the complexity of the encryption.

In automation, I also use this to generate different names for the files, so that it's easier to have a distinction in the automation process.

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