sha256verify python
时间: 2023-10-13 10:26:40 浏览: 97
Python库 | verify_email-1.0.0.tar.gz
SHA-256 is a cryptographic hash function that takes input and produces a fixed-size output, typically represented as a string of characters. It is commonly used in digital signatures, message authentication codes (MACs), and other applications where data integrity is important.
To verify a SHA-256 hash in Python, you can use the hashlib module, which provides a hashlib.sha256() function that takes a byte string as input and returns a SHA-256 hash object. You can then compare the resulting hash with the expected hash to verify the integrity of the data.
Here's an example:
```python
import hashlib
# Data to hash
data = b"Hello, world!"
# Expected SHA-256 hash
expected_hash = "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9"
# Compute SHA-256 hash
hash_object = hashlib.sha256(data)
computed_hash = hash_object.hexdigest()
# Verify hash
if computed_hash == expected_hash:
print("Hashes match! Data is intact.")
else:
print("Hashes do not match! Data has been tampered with.")
```
This code computes the SHA-256 hash of the byte string `b"Hello, world!"` using the `hashlib.sha256()` function, and then compares it with the expected hash value. If the two hashes match, it prints a message indicating that the data is intact; otherwise, it prints a message indicating that the data has been tampered with.
阅读全文