Practical 7

Secure Hash Functions

SHA-1 & SHA-256 Implementation and Comparison

🎯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

📚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

Original Message
GECBHARUCH
Modified Message
GECBHARUCh

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

📝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: