Custom Python Hash Auditor
Custom Python Hash Auditor
In this final guide, we will build a Python script that demonstrates the SHA256 and RIPEMD160 hashing algorithms used in Bitcoin. We will also implement the HASH160 hybrid process used for address generation.
The Hash Auditor
import hashlib
def audit_hashes(input_string):
print(f"--- Cryptographic Hash Audit ---")
print(f"[*] Input Message: '{input_string}'")
# 1. Standard SHA256
data = input_string.encode('utf-8')
sha256_hash = hashlib.sha256(data).hexdigest()
print(f"[*] SHA256: {sha256_hash}")
# 2. Double SHA256 (SHA256d)
# Used for Block Hashes and TXIDs
sha256d_hash = hashlib.sha256(hashlib.sha256(data).digest()).hexdigest()
print(f"[*] SHA256d: {sha256d_hash}")
# 3. RIPEMD160
# Note: RIPEMD160 is in the hashlib library but requires specific handling
ripemd160_hash = hashlib.new('ripemd160', data).hexdigest()
print(f"[*] RIPEMD160: {ripemd160_hash}")
# 4. HASH160 (The Address Generator)
# RIPEMD160(SHA256(data))
hash160_binary = hashlib.new('ripemd160', hashlib.sha256(data).digest()).digest()
print(f"[*] HASH160: {hash160_binary.hex()}")
# --- Simulation ---
# Case 1: Standard Input
audit_hashes("Bitcoin")
print("\n--- Avalanche Effect Demonstration ---")
# Case 2: Tiny change (Capital B to lowercase b)
audit_hashes("bitcoin")
How to Run the Auditor
-
Ensure you have Python 3 installed.
-
Copy the code into a file named
hash_auditor.py. -
Run it using
python3 hash_auditor.py.
Technical Takeaways
-
digest() vs hexdigest(): The
digest()function returns raw binary bytes, whilehexdigest()returns the human-readable string. Bitcoin nodes usually store data as raw bytes for efficiency. -
SHA256d Efficiency: Hashing twice is not significantly slower for a computer, but it provides a massive security "Insurance Policy" against future cryptographic discoveries.
-
The Hybrid Power: Notice how HASH160 produces a 20-byte result. This is exactly why Bitcoin addresses (once Base58 encoded) are roughly the same length as a credit card number, making them human-manageable.
Congratulations! You have completed the Cryptographic Hash Functions module. You now understand the mathematical foundation that keeps the Bitcoin ledger immutable.
TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation: