Skip to content

Instantly share code, notes, and snippets.

@RGGH
Created March 2, 2024 13:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RGGH/e7db6cf0f8898ab3afb56ae05b821913 to your computer and use it in GitHub Desktop.
Save RGGH/e7db6cf0f8898ab3afb56ae05b821913 to your computer and use it in GitHub Desktop.
proof-of-work-checker
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