Hash algorithm encryption and decryption
1. Introduction to Hash Encryption and Decryption
Hash, English called hash.
A hash function can calculate data (byte strings) of any length into a fixedlength result data.
We are accustomed to call the data to be calculated as source data, and the resulting data after calculation as hash value or digests.
There are several hash functions, corresponding to different algorithms, the common ones are MD5, SHA1, SHA224, SHA256, SHA384, SHA512
The characteristics of hash calculation are:
The same source data, using the same hash algorithm, the calculated hash value must be the same
No matter how big the source data is, with the same hash algorithm, the length of the calculated hash value is the same.
Algorithm Calculated Result Length
MD5 16 bytes
SHA1 20 bytes
SHA224 28 bytes
SHA256 32 bytes
SHA384 48 bytes
SHA512 64 bytes
The algorithm is irreversible.
That is, the source data cannot be calculated inversely through the hash value. So hashing is different from encryption and decryption we often say.
Different source data using the same hash algorithm may produce the same hash value, which is called the collision rate (collision rate)
For various hash algorithms, the longer the length of the calculation result, the lower the collision rate, and the longer the calculation time is usually spent.
Even with the MD5 algorithm, the collision rate is very small, almost negligible. It's about 1.47*10 to the negative 29th power
2. Encryption method
'md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512',
'blake2b', 'blake2s',
'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
'shake_128', 'shake_256'
md5 encryption
# coding=utf8
"""
@Project : pachongmaster
@File : hash_test.py
@Author: gaojs
"""
import hashlib
# use md5 algorithm
h = hashlib.md5()
# sha 256 algorithm
# m = hashlib.sha256()
# The metadata to be calculated must be in string format
# String objects need to be encoded into byte string objects
h.update("Hi, my fate is up to me".encode())
# Generate the bytes object corresponding to the hash value
resultBytes = h.digest()
resultHex = h.hexdigest()
print(resultHex)
sha 256 encryption
# coding=utf8
"""
@Project : pachongmaster
@File : hash_test.py
@Author: gaojs
"""
import hashlib
# use md5 algorithm
# h = hashlib.md5()
# md5 encryption intervention is as follows: 8b365af9b1089f502e7bc60ac9c81ed2
# sha 256 algorithm
h = hashlib.sha256()
# The metadata to be calculated must be in string format
# String objects need to be encoded into byte string objects
h.update("Hi, my fate is up to me".encode())
# Generate the bytes object corresponding to the hash value
resultBytes = h.digest()
resultHex = h.hexdigest()
# sha 256 encryption result is as follows: 8a3e8bf351d6bc5c311a6790f7aeeea46d955db7a4357653b21254380651623f
print(resultHex)
sha 512 encryption
# coding=utf8
"""
@Project : pachongmaster
@File : hash_test.py
@Author: gaojs
"""
import hashlib
# use md5 algorithm
# h = hashlib.md5()
# md5 encryption intervention is as follows: 8b365af9b1089f502e7bc60ac9c81ed2
# sha 512 algorithm
h = hashlib.sha512()
# The metadata to be calculated must be in string format
# String objects need to be encoded into byte string objects
h.update("Hi, my fate is up to me".encode())
# Generate the bytes object corresponding to the hash value
resultBytes = h.digest()
resultHex = h.hexdigest()
# sha 512 encryption results are as follows:
#c945f94c933078a77812afa75cd693ee17e581b9920696063cf50983fcb1a56d577565c3bcda05badc6c4ffdf64f1abbdd6dd0897c3e1620950b7ec31c96fe6c
print(resultHex)
3. How does python decrypt the hash algorithm
The encryption and decryption algorithm is to operate on the source data to generate encrypted data, and reverse the process to inversely calculate the source data from the encrypted data.
The differences between encryption and decryption algorithms and hash algorithms are:
Encryption and decryption algorithms are reversible, and hash algorithms are irreversible.
The hash algorithm can generate a relatively small hash value for large data, while the source data of the encryption algorithm is large, and the encrypted data will also be large.
Encryption and decryption algorithms can be divided into symmetric encryption and asymmetric encryption
Symmetric encryption means that the same key is used for encryption and decryption.
Asymmetric encryption means that encryption and decryption use different keys, usually a pair of keys, called the public key (used to encrypt) and the private key (used to decrypt).
The more common symmetric encryption algorithms are: AES, RC4, DES, 3DES, IDEA, etc.
Among them, the security level is higher than AES.
The most wellknown asymmetric encryption system is RSA (Rivest–Shamir–Adleman).
hash_jiemi.py
# coding=utf8
"""
@Project : pachongmaster
@File : hash_jiemi.py
@Author: gaojs
"""
# Currently wellknown Python encryption and decryption libraries are cryptography and PyNaCl
from cryptography.fernet import Fernet
def jiami():
"""
Encryption and decryption process
:return:
"""
# The following is an example of using this library for AES encryption and decryption
# generate key
key = Fernet.generate_key()
fin = Fernet(key)
strings = 'gaojs, my fate is up to me!'
# The original message must be a string
stringsBytes = strings.encode()
# generate encrypted bytes
token_result = fin.encrypt(stringsBytes)
print(token_result)
# decrypt, the return value is a byte string object
result = fin.decrypt(token_result)
print(result.decode())
if __name__ == '__main__':
jiami()
Hash, English called hash.
A hash function can calculate data (byte strings) of any length into a fixedlength result data.
We are accustomed to call the data to be calculated as source data, and the resulting data after calculation as hash value or digests.
There are several hash functions, corresponding to different algorithms, the common ones are MD5, SHA1, SHA224, SHA256, SHA384, SHA512
The characteristics of hash calculation are:
The same source data, using the same hash algorithm, the calculated hash value must be the same
No matter how big the source data is, with the same hash algorithm, the length of the calculated hash value is the same.
Algorithm Calculated Result Length
MD5 16 bytes
SHA1 20 bytes
SHA224 28 bytes
SHA256 32 bytes
SHA384 48 bytes
SHA512 64 bytes
The algorithm is irreversible.
That is, the source data cannot be calculated inversely through the hash value. So hashing is different from encryption and decryption we often say.
Different source data using the same hash algorithm may produce the same hash value, which is called the collision rate (collision rate)
For various hash algorithms, the longer the length of the calculation result, the lower the collision rate, and the longer the calculation time is usually spent.
Even with the MD5 algorithm, the collision rate is very small, almost negligible. It's about 1.47*10 to the negative 29th power
2. Encryption method
'md5', 'sha1', 'sha224', 'sha256', 'sha384', 'sha512',
'blake2b', 'blake2s',
'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512',
'shake_128', 'shake_256'
md5 encryption
# coding=utf8
"""
@Project : pachongmaster
@File : hash_test.py
@Author: gaojs
"""
import hashlib
# use md5 algorithm
h = hashlib.md5()
# sha 256 algorithm
# m = hashlib.sha256()
# The metadata to be calculated must be in string format
# String objects need to be encoded into byte string objects
h.update("Hi, my fate is up to me".encode())
# Generate the bytes object corresponding to the hash value
resultBytes = h.digest()
resultHex = h.hexdigest()
print(resultHex)
sha 256 encryption
# coding=utf8
"""
@Project : pachongmaster
@File : hash_test.py
@Author: gaojs
"""
import hashlib
# use md5 algorithm
# h = hashlib.md5()
# md5 encryption intervention is as follows: 8b365af9b1089f502e7bc60ac9c81ed2
# sha 256 algorithm
h = hashlib.sha256()
# The metadata to be calculated must be in string format
# String objects need to be encoded into byte string objects
h.update("Hi, my fate is up to me".encode())
# Generate the bytes object corresponding to the hash value
resultBytes = h.digest()
resultHex = h.hexdigest()
# sha 256 encryption result is as follows: 8a3e8bf351d6bc5c311a6790f7aeeea46d955db7a4357653b21254380651623f
print(resultHex)
sha 512 encryption
# coding=utf8
"""
@Project : pachongmaster
@File : hash_test.py
@Author: gaojs
"""
import hashlib
# use md5 algorithm
# h = hashlib.md5()
# md5 encryption intervention is as follows: 8b365af9b1089f502e7bc60ac9c81ed2
# sha 512 algorithm
h = hashlib.sha512()
# The metadata to be calculated must be in string format
# String objects need to be encoded into byte string objects
h.update("Hi, my fate is up to me".encode())
# Generate the bytes object corresponding to the hash value
resultBytes = h.digest()
resultHex = h.hexdigest()
# sha 512 encryption results are as follows:
#c945f94c933078a77812afa75cd693ee17e581b9920696063cf50983fcb1a56d577565c3bcda05badc6c4ffdf64f1abbdd6dd0897c3e1620950b7ec31c96fe6c
print(resultHex)
3. How does python decrypt the hash algorithm
The encryption and decryption algorithm is to operate on the source data to generate encrypted data, and reverse the process to inversely calculate the source data from the encrypted data.
The differences between encryption and decryption algorithms and hash algorithms are:
Encryption and decryption algorithms are reversible, and hash algorithms are irreversible.
The hash algorithm can generate a relatively small hash value for large data, while the source data of the encryption algorithm is large, and the encrypted data will also be large.
Encryption and decryption algorithms can be divided into symmetric encryption and asymmetric encryption
Symmetric encryption means that the same key is used for encryption and decryption.
Asymmetric encryption means that encryption and decryption use different keys, usually a pair of keys, called the public key (used to encrypt) and the private key (used to decrypt).
The more common symmetric encryption algorithms are: AES, RC4, DES, 3DES, IDEA, etc.
Among them, the security level is higher than AES.
The most wellknown asymmetric encryption system is RSA (Rivest–Shamir–Adleman).
hash_jiemi.py
# coding=utf8
"""
@Project : pachongmaster
@File : hash_jiemi.py
@Author: gaojs
"""
# Currently wellknown Python encryption and decryption libraries are cryptography and PyNaCl
from cryptography.fernet import Fernet
def jiami():
"""
Encryption and decryption process
:return:
"""
# The following is an example of using this library for AES encryption and decryption
# generate key
key = Fernet.generate_key()
fin = Fernet(key)
strings = 'gaojs, my fate is up to me!'
# The original message must be a string
stringsBytes = strings.encode()
# generate encrypted bytes
token_result = fin.encrypt(stringsBytes)
print(token_result)
# decrypt, the return value is a byte string object
result = fin.decrypt(token_result)
print(result.decode())
if __name__ == '__main__':
jiami()
Related Articles

A detailed explanation of Hadoop core architecture HDFS
Knowledge Base Team

What Does IOT Mean
Knowledge Base Team

6 Optional Technologies for Data Storage
Knowledge Base Team

What Is Blockchain Technology
Knowledge Base Team
Explore More Special Offers

Short Message Service(SMS) & Mail Service
50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00