What's the coolest program you've made in Python?

Password hashing is a crucial security technique for protecting user credentials. This Python program demonstrates how to hash passwords using different algorithms like MD5, SHA-256, and BLAKE2b for secure storage.

What is Password Hashing?

Password hashing converts plain text passwords into encrypted strings using mathematical algorithms. Instead of storing actual passwords, systems store these hashed values. When a user logs in, their input is hashed and compared to the stored hash, ensuring passwords remain secure even if data is compromised.

Implementation Approach

  • Create functions for different hashing algorithms

  • Get user input for the password string

  • Allow user to select preferred hashing method

  • Generate and display the hashed result

Creating Hash Functions

First, we define functions for each hashing algorithm. Each function takes a password string and returns its hash digest −

import hashlib

def hash_with_MD5(password):
    encoded = password.encode()
    print("Hashed with MD5:", hashlib.md5(encoded).hexdigest())

def hash_with_SHA(password):
    encoded = password.encode()
    print("Hashed with SHA-256:", hashlib.sha256(encoded).hexdigest())

def hash_with_blake(password):
    encoded = password.encode()
    print("Hashed with BLAKE2b:", hashlib.blake2b(encoded).hexdigest())

# Test the functions
test_password = 'tutorialspoint'
hash_with_MD5(test_password)
hash_with_SHA(test_password)
hash_with_blake(test_password)
Hashed with MD5: 6c60b3cfe5124f982eb629e00a98f01f
Hashed with SHA-256: 15e6e9ddbe43d9fe5745a1348bf1535b0456956d18473f5a3d14d6ab06737770
Hashed with BLAKE2b: 109f6f017d7a77bcf57e4b48e9c744280ae7f836477c16464b27a3fe62e1353c70ec4c7f938080ee7c311094eede0235a43151c3d2b7401a3cb5a8f8ab3fbb

Complete Password Hasher Program

Here's the full interactive password hashing program that lets users choose their preferred algorithm −

import hashlib

def hash_with_MD5(password):
    encoded = password.encode()
    print("Hashed with MD5:", hashlib.md5(encoded).hexdigest())

def hash_with_SHA(password):
    encoded = password.encode()
    print("Hashed with SHA-256:", hashlib.sha256(encoded).hexdigest())

def hash_with_blake(password):
    encoded = password.encode()
    print("Hashed with BLAKE2b:", hashlib.blake2b(encoded).hexdigest())

print("Password Hashing Tool")
print("1. MD5")
print("2. SHA-256") 
print("3. BLAKE2b")

while True:
    choice = input("\nEnter choice (1/2/3): ")
    
    if choice in ('1', '2', '3'):
        password = input('Enter password to hash: ')
        
        if choice == '1':
            hash_with_MD5(password)
        elif choice == '2':
            hash_with_SHA(password)
        elif choice == '3':
            hash_with_blake(password)
        
        continue_choice = input("\nHash another password? (yes/no): ")
        if continue_choice.lower() == "no":
            break
    else:
        print("Invalid choice. Please enter 1, 2, or 3.")
Password Hashing Tool
1. MD5
2. SHA-256
3. BLAKE2b

Enter choice (1/2/3): 2
Enter password to hash: Python
Hashed with SHA-256: 18885f27b5af9012df19e496460f9294d5ab76128824c6f993787004f6d9a7db

Hash another password? (yes/no): yes

Enter choice (1/2/3): 1
Enter password to hash: Tutorialspoint
Hashed with MD5: da653faa9f00528be9a57f3474f0e437

Hash another password? (yes/no): no

Security Comparison

Algorithm Output Length Security Level Speed
MD5 32 characters Weak (deprecated) Fast
SHA-256 64 characters Strong Medium
BLAKE2b 128 characters Very Strong Fast

Key Points

  • Always use .encode() to convert strings to bytes before hashing

  • MD5 is fast but cryptographically broken − avoid for production

  • SHA-256 provides good security for most applications

  • BLAKE2b offers excellent security with high performance

  • Use hexdigest() to get human-readable hash output

Conclusion

This password hasher demonstrates three popular hashing algorithms with an interactive interface. For production systems, prefer SHA-256 or BLAKE2b over MD5, and consider adding salt for enhanced security against rainbow table attacks.

---
Updated on: 2026-03-27T00:40:12+05:30

211 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements