1import hashlib
2def find_hashcash_challenge(prefix, difficulty=6):
3 """
4 Find a string X where SHA256(prefix + X) has `difficulty` leading zeros.
5 Args:
6 prefix: The string to prepend to X
7 difficulty: Number of leading zeros required (default: 6)
8 Returns:
9 tuple: (found_string, hash_result, attempts)
10 """
11 target = '0' * difficulty
12 nonce = 0
13 while True:
14 candidate = prefix + str(nonce)
15 hash_result = hashlib.sha256(candidate.encode()).hexdigest()
16 if hash_result[:difficulty] == target:
17 return candidate, hash_result, nonce
18 nonce += 1
19# Solve the challenge
20prefix = 'nE3ysYM4HN'
21candidate, hash_result, attempts = find_hashcash_challenge(prefix, difficulty=6)
22print(f"Prefix: {prefix}")
23print(f"Found: {candidate}")
24print(f"SHA256: {hash_result}")
25print(f"Attempts: {attempts}")