# Inventory of 7 encryption algorithms of recommendations of Python encryption and decryption module hashlib

- 2021-10-27 08:00:51
- OfStack

## Preface

In the program, we can often see that there are many encryption algorithms, such as MD5 sha1, etc. Today, we will learn about this encryption algorithm. Before understanding, we need to know that one module is hashlib, which is the current Python1 module that provides character encryption. Its encrypted character type is binary coding, so direct encryption string will report errors.

```
import hashlib
string=' Willful 90 Posterior boy'
# Use encode Conversion
sha1 = hashlib.sha1()
sha1.update(string.encode('utf-8'))
res = sha1.hexdigest()
print("sha1 Adopt encode Convert encryption results :",res)
# Use byte Convert to 2 Binary system
sha1 = hashlib.sha1()
sha1.update(bytes(string,encoding='utf-8'))
res = sha1.hexdigest()
print("sha1 Adopt byte The result of the transformation :",res)
```

You can use either of the following two methods to obtain all the hash algorithm sets in hashlib:

```
import hashlib
a=hashlib.algorithms_available
b=hashlib.algorithms_guaranteed
print(a)
print(b)
```

Let's choose the commonly used centralized algorithm to explain.

## 1. MD5

MD5 is Message-Digest Algorithm 5 (information-summary algorithm 5), which is used to ensure the integrity of information transmission. It is one of the widely used hash algorithms in computers, and the mainstream programming language has been generally implemented by MD5. Operating data (such as Chinese characters) into another fixed-length value is the basic principle of hashing algorithm. The predecessors of MD5 are MD2, MD3 and MD4.

The MD5 algorithm has the following characteristics:

1. Compressibility: For data of any length, the calculated length of MD5 value is fixed.

2. Easy to calculate: It is easy to calculate MD5 value from the original data.

3. Modification resistance: Any change to the original data, even if only one byte is modified, the obtained MD5 value is quite different.

4. Strong anti-collision: Given the original data and its MD5 value, it is very difficult to find a data with the same MD5 value (i.e., forged data).

The function of MD5 is to "compress" large-capacity information into a secure format (that is, to transform a byte string of arbitrary length into a 106-digit string of fixed length) before signing the private key with digital signature software. MD5 is the most common summarization algorithm, which is very fast and produces a fixed 128 bit bytes, usually represented by a 32-bit hexadecimal string.

```
import hashlib
string=' Willful 90 Posterior boy'
md5 = hashlib.md5()
md5.update(string.encode('utf-8'))# Transcoding, update The in must be byte type
res = md5.hexdigest() # Returns character summary information
print(md5.digest())# Returns summary information in byte type
print("md5 Encryption result :",res)
```

## 2. sha1

Secure hash algorithm, the result of SHA1 is 160 bit bytes, usually represented by a 40-bit hexadecimal string

```
import hashlib
string=' Willful 90 Posterior boy'
sha1 = hashlib.sha1()
sha1.update(string.encode('utf-8'))
res = sha1.hexdigest()
print("sha1 Encryption result :",res)
```

## 3. sha224

Secure hash algorithm

```
import hashlib
string=' Willful 90 Posterior boy'
sha224 = hashlib.sha224()
sha224.update(string.encode('utf-8'))
res = sha224.hexdigest()
print("sha224 Encryption result :",res)
```

## 4. sha256

Secure hash algorithm

```
import hashlib
string=' Willful 90 Posterior boy'
sha256 = hashlib.sha256()
sha256.update(string.encode('utf-8'))
res = sha256.hexdigest()
print("sha256 Encryption result :",res)
```

## 5. sha384

Secure hash algorithm

```
import hashlib
string=' Willful 90 Posterior boy'
sha384 = hashlib.sha384()
sha384.update(string.encode('utf-8'))
res = sha384.hexdigest()
print("sha384 Encryption result :",res)
```

## 6. sha512

Secure hash algorithm

```
import hashlib
string=' Willful 90 Posterior boy'
sha512= hashlib.sha512()
sha512.update(string.encode('utf-8'))
res = sha512.hexdigest()
print("sha512 Encryption result :",res)
```

## 7. Advanced Encryption

Although the above encryption algorithm is still very powerful, it sometimes has defects, that is, it can be solved inversely by colliding with the library. Therefore, it is necessary to add custom key to the encryption algorithm before doing encryption.

```
md5 = hashlib.md5()
md5.update('md5'.encode('utf-8'))
res = md5.hexdigest()
print(" Ordinary encryption :",res)
md51 = hashlib.md5(b'md512')
md51.update('md51'.encode('utf-8'))
res = md51.hexdigest()
print(" Adopt key Encryption :",res)
```

## Summarize

Ok, I'll tell you so much today. I mainly introduce the centralized algorithms such as md5, sha1, sha224, sha256, sha384, sha512 and advanced encryption, and briefly introduce the characteristics and usage of each algorithm. Please look forward to the wonderful follow-up!