Created
March 2, 2024 13:05
proof-of-work-checker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import hashlib | |
DIFFICULTY = 3 # Number of leading zeros required | |
def meets_difficulty(hash_string): | |
"""Checks if the hash string starts with the required number of zeros.""" | |
return hash_string.startswith("0" * DIFFICULTY) | |
def main(): | |
"""Hashes "Hello, World!" with varying nonces to find a valid hash.""" | |
data = "Hello, World!" | |
nonce = 0 | |
while True: | |
# Combine data and nonce into bytes for hashing | |
data_and_nonce = data.encode() + nonce.to_bytes(8, byteorder="big") | |
# Perform SHA-256 hashing | |
hash_digest = hashlib.sha256(data_and_nonce).hexdigest() | |
# Check if the hash meets the difficulty target | |
if meets_difficulty(hash_digest): | |
print(f"Found valid hash with nonce: {nonce}") | |
print(f"Hash: {hash_digest}") | |
break # Exit the loop once a valid hash is found | |
# Increment the nonce for the next iteration | |
nonce += 1 | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment