Skip to content

Instantly share code, notes, and snippets.

@morkev
Created October 22, 2024 21:43
Show Gist options
  • Select an option

  • Save morkev/45786107f65838bebaddaff864a5b7c5 to your computer and use it in GitHub Desktop.

Select an option

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.
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