![]()
What is Hashing?
Hashing is a one-way function that converts input data into a fixed-length string (hash). Hash functions are used for password storage, data integrity, and digital signatures.
Common Hashing Algorithms:
- MD5 (Not Secure)
 - SHA-1 (Weak Security)
 - SHA-256, SHA-512 (Recommended)
 
Hashing in Python (SHA-256 Example)
import hashlib
def hash_text(text):
    hash_obj = hashlib.sha256(text.encode())
    return hash_obj.hexdigest()
# Example usage
print(hash_text("password123"))
SHA-256 generates a unique hash that cannot be reversed.
What is Encryption?
Encryption converts plaintext into ciphertext using a key. Unlike hashing, encryption is reversible using a decryption key.
Common Encryption Algorithms:
- Symmetric Encryption (AES, DES) – Same key for encryption & decryption.
 - Asymmetric Encryption (RSA, ECC) – Public key encrypts, private key decrypts.
 
AES Encryption with Python (Symmetric Encryption)
from Crypto.Cipher import AES
import base64
key = b'1234567890abcdef'  # 16-byte key
cipher = AES.new(key, AES.MODE_EAX)
def encrypt_text(plain_text):
    nonce = cipher.nonce
    ciphertext, tag = cipher.encrypt_and_digest(plain_text.encode())
    return base64.b64encode(nonce + ciphertext).decode()
# Example usage
encrypted = encrypt_text("Hello, Secure World!")
print("Encrypted:", encrypted)
AES encryption ensures secure data transmission.
RSA Encryption (Asymmetric Encryption)
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import base64
# Generate RSA Keys
key = RSA.generate(2048)
public_key = key.publickey().export_key()
private_key = key.export_key()
def rsa_encrypt(plain_text, pub_key):
    recipient_key = RSA.import_key(pub_key)
    cipher_rsa = PKCS1_OAEP.new(recipient_key)
    encrypted = cipher_rsa.encrypt(plain_text.encode())
    return base64.b64encode(encrypted).decode()
# Example usage
encrypted_message = rsa_encrypt("Secure Data!", public_key)
print("Encrypted:", encrypted_message)
RSA allows secure communication between parties.
When to Use Hashing vs Encryption?
| Use Case | Hashing | Encryption | 
|---|---|---|
| Password Storage | Yes | No | 
| Data Integrity (Checksums) | Yes | No | 
| Secure Data Transfer | No | Yes | 
| Digital Signatures | Yes | Yes | 
