Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the use of hashlib module in python

2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

In this issue, Xiaobian will bring you about the use of hashlib module in python. The article is rich in content and analyzed and described from a professional perspective. After reading this article, I hope you can gain something.

Definition of cryptographic hash function

Cryptographic hash functions convert arbitrary length data into a fixed-length binary data (usually represented by a hexadecimal string) through hash algorithms (also known as digest algorithms, hash algorithms).

Common cryptographic hash functions

Common cryptographic hash functions are: md5 is 128 bits (32 characters), sha1 is 160 bits (40 characters), sha224 (224 bits 56 characters), sha384 (384 bits 66 characters), sha3_224 (224 bits 56 characters), sha3_256(256 bits 64 characters), sha3_384(384 bits 96 characters), sha3_512(512 bits 128 characters), sha512 (512 bits 128 characters), shake_128(128 bits 32 characters), shake_256(256 bits 64 characters), etc.

Characteristics and functions of cryptographic hash function

Features of cryptographic hash function: A fixed-length digest is computed for arbitrary length data by hash algorithm. The same abstracts are calculated for the same data; different abstracts are calculated for different data (there is a very, very low probability that different data will produce the same abstracts, referred to as collisions); data cannot be recovered from abstracts. So you can check against the digest to see if the data has been tampered with.

Cryptographic hashing functions are: electronic signature, tamper-proof, storage password and so on.

In addition: MD5 and SHA1 have been confirmed to have hidden dangers, and sha2 and sha3 are recommended.

Cryptographic hash function use cases

The following are sha2 and sha3 use cases (note the salting usage, which adds noise values to strings before hashing them). It can improve safety and reduce the possibility of collision):

import hashlibsha2 = hashlib.sha224() #Standard sha2 Usage sha2.update ("Test sha2".encode("utf8"))print (sha2.hexdigest())sha2 = hashlib.sha224 ('eqwro '.encode ('utf8')) #salt sha2 usage sha2.update ("Test sha2".encode("utf8"))print (sha2.hexdigest())sha3 = hashlib.sha3_224() #Standard sha3 Usage sha3.update ("Test sha2".encode("utf8"))print (sha3.hexdigest())sha3 = hashlib.sha3_224 ('eqwro '.encode ('utf8')) #salt sha3 usage sha3.update ("Test sha2".encode("utf8"))print (sha3.hexdigest())out: 844e8c2c97312b6344377e49a412e2545e75a7b551f9551038a5f451a2cb1365509f141da28fe3935f7f6c8c6ef7b3fb347004f98d8b283cf92459c0c2ccc081e26d607b8fd39f804f6a57641446f6be01d459 6fc2320f0da1a04385b1518fdeb69ddcdf34e5b25996447d2cfbb25f9f

Python's hashlib module does not have a direct way to calculate the hash value of a file. The following method can be used to calculate the hash value of a file.

def sha3_file(file_path, salt ='', buffers=1024): sha3 = hashlib.sha3_224(salt.encode ('utf8 '))with open(file_path, ' rb') as f:while data := f.read (buffers):sha3.update(data)return sha3.hexdigest()print(sha3_file ('all1.jpg '))out:c88d28d070e6ec978b6d51814ca7e7c27b8a2bf26b58819826815d2c The above is a small series for everyone to share p. What is the use of the hashlib module in ython? If you happen to have similar doubts, you may wish to refer to the above analysis for understanding. If you want to know more about it, please pay attention to the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report