🎯Objective
To implement and compare secure hash functions — specifically SHA-1 and SHA-256 — in order to understand how hashing is used to ensure data integrity, non-reversibility, and message uniqueness in cryptographic systems.
📋Purpose of the Practical
- To compute cryptographic hash values using SHA-1 and SHA-256 algorithms
- To understand how small changes in input produce completely different hashes (avalanche effect)
- To compare hash length, strength, and resistance between SHA-1 and SHA-256
- To learn the role of hashing in digital signatures, authentication, file verification, and more
📚Theory: What are Hash Functions?
A hash function is a mathematical algorithm that converts an input (or 'message') into a fixed-size string of bytes, typically a digest that appears random. Hash functions are widely used in data integrity, password storage, digital signatures, and blockchain technology.
Secure Hash Algorithms (SHA) Comparison
| Hash Algorithm |
Output Length |
Security Level (as of now) |
| SHA-1 |
160 bits (20 bytes) |
Broken (collisions possible) |
| SHA-256 |
256 bits (32 bytes) |
Secure (widely used) |
💻Implementation Code
from Crypto.Hash import SHA1, SHA256
message1 = "GECBHARUCH"
message2 = "GECBHARUCh" # Just one letter different
# SHA-1 using Crypto.Hash
sha1_hash1 = SHA1.new(message1.encode()).hexdigest()
sha1_hash2 = SHA1.new(message2.encode()).hexdigest()
# SHA-256 using Crypto.Hash
sha256_hash1 = SHA256.new(message1.encode()).hexdigest()
sha256_hash2 = SHA256.new(message2.encode()).hexdigest()
print("SHA-1 of GECBHARUCH:", sha1_hash1)
print("SHA-1 of GECBHARUCh:", sha1_hash2)
print("\nSHA-256 of GECBHARUCH:", sha256_hash1)
print("SHA-256 of GECBHARUCh:", sha256_hash2)
SHA-1 of GECBHARUCH: c5a4daec925a35123ec96c14e73eea16e12db40f
SHA-1 of GECBHARUCh: 5722e90661ffd8c4ae9e5d7bb00250c159f64e1b
SHA-256 of GECBHARUCH: 8ceb210ad7c7eb630802fe3466ec318435ed43ec5e1faa9e5ff721abe866baa6
SHA-256 of GECBHARUCh: 705d70d891f148d374e159a51e1d2fab1d08cb71523fd868026f6ff75d5c447b
⚡Avalanche Effect Demonstration
Small Input Change → Completely Different Hash
Result: Even changing just one letter case (H → h) produces completely different hash outputs!
🔐Hash Output Comparison
SHA-1 Output
160 bits / 40 hex characters
c5a4daec925a35123ec96c14e73eea16e12db40f
SHA-256 Output
256 bits / 64 hex characters
8ceb210ad7c7eb630802fe3466ec318435ed43ec5e1faa9e5ff721abe866baa6
📊Important Concepts
| Concept |
Description |
| Deterministic |
Same input → same output always |
| One-way |
Cannot reverse-engineer input from output |
| Avalanche effect |
A small change in input changes the entire hash |
| SHA-1 is broken |
Collision attacks found; do not use for secure systems |
| SHA-256 is secure |
Stronger and recommended for modern systems |
| Fixed Output Length |
Input can be any length, but output is always fixed |
🔒Security Aspects
SHA-1
• 160-bit (40 hex chars) hash
• Previously secure, now weak
• Collision vulnerabilities found
• Deprecated in modern standards
SHA-256
• Part of SHA-2 family
• 256-bit (64 hex chars) hash
• Highly secure and widely used
• Used in Bitcoin, SSL/TLS certificates
🌟Applications of Hash Functions
Password Hashing
File Integrity Verification
Digital Signatures
Blockchain Technology
SSL/TLS Certificates
Data Authentication
🔬Key Observations
- A single character difference in input strings results in completely different hash outputs
- SHA-1 produces 40 hexadecimal characters (160 bits)
- SHA-256 produces 64 hexadecimal characters (256 bits)
- Demonstrates the avalanche effect - a desired property of cryptographic hash functions
- SHA-256 is stronger and more future-proof than SHA-1
📝Summary
This practical demonstrates the use of secure hash functions (SHA-1 and SHA-256) in data integrity and authentication. It highlights the significance of hash uniqueness, the avalanche effect, and non-reversibility, which make these algorithms essential for security in applications like digital signatures, password protection, blockchains, and file integrity verification. With growing cryptographic needs, SHA-256 is recommended over SHA-1 for all modern security implementations.
🎓Learning Outcomes
By the end of this practical, students will:
-
Understand how cryptographic hash functions work
- Learn the difference between SHA-1 and SHA-256
- Observe how even a small change in input produces a completely different hash (avalanche effect)
- Appreciate the importance of using secure hash algorithms in modern cryptographic systems
- Recognize real-world applications of hash functions in cybersecurity