Created
October 22, 2024 21:43
-
-
Save morkev/45786107f65838bebaddaff864a5b7c5 to your computer and use it in GitHub Desktop.
Brute-force search for a hash with a specific three-byte prefix using SHA256.
This file contains hidden or 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 | |
| import os | |
| def find_specific_hash_collision(target_prefix_hex): | |
| target_prefix = bytes.fromhex(target_prefix_hex) | |
| attempts = 0 | |
| while True: | |
| data = os.urandom(16) | |
| hash_object = hashlib.sha256(data) | |
| hash_digest = hash_object.digest() | |
| if hash_digest[:3] == target_prefix: | |
| print(f"Collision found after {attempts} attempts") | |
| print(f"Data (hex): {data.hex()}") | |
| print(f"SHA256 Hash: {hash_digest.hex()}") | |
| return data, hash_digest | |
| attempts += 1 | |
| target_prefix_hex = '8b7183' | |
| find_specific_hash_collision(target_prefix_hex) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment