Cybersecurity

Hash vs Encryption vs Signature: Three Different Jobs

Encryption Cryptography Authentication OAuth Git Passwords Hashing Certificates TLS HTTPS
792 words Includes Code

Hashes verify data hasn't changed. Encryption keeps data secret. Digital signatures prove WHO sent it — and that they can't deny it.

Key Takeaway: A digital signature combines a hash with a private key to provide three guarantees: authentication (who signed), integrity (no tampering), and non-repudiation (can't deny signing). Every developer working with APIs, certificates, or secure communications needs to understand how signatures work.

Hash vs Encryption vs Signature: Three Different Jobs

Developers often confuse these three cryptographic primitives. Here's the clear distinction:

PrimitivePurposeKey RequirementProves
HashIntegrityNone (one-way function)Data hasn't changed
EncryptionConfidentialitySecret keyData is hidden from others
SignatureAuthentication + IntegrityPrivate keyWHO sent it AND it's unchanged

The critical difference: anyone can compute a hash, but only the private key holder can create a valid signature.

import hashlib, hmac, secrets

message = b"Transfer $500 to Alice"

# HASH — anyone can compute it
digest = hashlib.sha256(message).digest()
print(f"Hash: {digest.hex()[:32]}...")

# ENCRYPTION — hides the content
key = secrets.token_bytes(32)
ciphertext = bytes(a ^ b for a, b in zip(message, key * len(message)))
print(f"Encrypted: {ciphertext.hex()[:32]}...")

# SIGNATURE — proves WHO sent it
private_key = secrets.token_bytes(32)
signature = hmac.new(private_key, message, hashlib.sha256).digest()
print(f"Signature: {signature.hex()[:32]}...")

The Three Properties of Digital Signatures

PropertyWhat It MeansWhy It Matters
AuthenticationProves WHO signed the messageOnly the private key holder could have created this signature
IntegrityProves the message hasn't been modifiedAny change to the message invalidates the signature
Non-RepudiationSender CANNOT deny having signedThe signature is legally binding proof

Without all three, you don't have a proper digital signature. A hash provides integrity but not authentication. Encryption provides confidentiality but not non-repudiation. Only signatures provide all three.

How Signing Works: Step by Step

The signing process has two phases: sign (sender) and verify (receiver).

# Step 1: Sender creates document
document = b"Purchase Order #12345: 100 widgets at $10 each"

# Step 2: Sender signs with PRIVATE key
private_key = secrets.token_bytes(32)
signature = hmac.new(private_key, document, hashlib.sha256).digest()
# signature = Sign(document, private_key)

# Step 3: Send document + signature
# (anyone can intercept — they can't forge without private key)

# Step 4: Receiver verifies with PUBLIC key
recomputed = hmac.new(private_key, document, hashlib.sha256).digest()
valid = recomputed == signature
print(f"Signature valid: {valid}")  # True

The key insight: verification uses the same private key as signing (in this simplified demo). In real systems, the public key can verify without knowing the private key.

Tamper Detection in Action

One of the most powerful properties of signatures is tamper detection. Even a single character change invalidates the signature:

ModificationSignature Valid?
Original: "Pay Alice $100"✓ VALID
"Pay Alice $1000" (changed amount)✗ INVALID
"Pay Bob $100" (changed recipient)✗ INVALID
"Pay Alice $100 " (added space)✗ INVALID
"PAY ALICE $100" (changed case)✗ INVALID
💡 Key Insight: Digital signatures detect ANY modification — even a single trailing space. This is because the signature is computed over the entire message.

Hash Algorithms for Signatures

Digital signatures don't sign the raw data — they sign a hash of the data. The choice of hash algorithm matters:

AlgorithmBitsStatusUse In Signatures
MD5128BROKENNever use
SHA-1160DEPRECATEDAvoid
SHA-256256Current standardRecommended
SHA-384384Better for long-termBetter
SHA-512524Best currentBest

Most modern signatures use SHA-256 or better. MD5 and SHA-1 have known collision attacks and should never be used for signatures.

Signature Algorithms Compared

AlgorithmKey SizePerformanceQuantum StatusUsed In
RSA-PSS2048-4096 bitMost commonVulnerableTLS, software signing
ECDSA256-521 bitSmaller keysVulnerableBitcoin, TLS
EdDSA (Ed25519)256 bitFast, modernVulnerableSSH, Signal, Git
ML-DSA (Dilithium)NIST PQCPost-quantumSafe ✓Future standard
SLH-DSA (SPHINCS+)NIST PQCHash-basedSafe ✓Conservative choice

The NIST post-quantum standards (ML-DSA and SLH-DSA) finalize in 2024. For new systems, consider quantum-resistant algorithms.

Where You Already Use Digital Signatures

You encounter digital signatures daily, even if you don't realize it:

ApplicationHow Signatures Are UsedWhy It Matters
TLS/HTTPSCA signs server certificateVerifies website identity
Email (DKIM)Email server signs messagesPrevents email spoofing
Software UpdatesDeveloper signs release binariesVerifies download authenticity
Git CommitsGPG-signed commitsVerifies author identity
BlockchainTransaction signingAuthorizes transfers
PDF SigningDigital document signaturesLegal non-repudiation

Sign-Then-Encrypt vs Encrypt-Then-Sign

When you need both confidentiality AND authentication, the order matters:

ApproachOrderSecurityRecommendation
Sign-Then-EncryptSign → EncryptSignature inside encryptionRecommended ✓
Encrypt-Then-SignEncrypt → SignSignature on ciphertextLess common

Most protocols (TLS, S/MIME, PGP) use sign-then-encrypt or its equivalent.

Try It Yourself

Start with the hash comparison tool to understand different algorithms, then explore the HMAC generator to see how keys create signatures.

Related BestWordz Tools:

Further Reading

All cryptographic examples use HMAC for educational simplicity. Production systems use RSA-PSS, ECDSA, or EdDSA from peer-reviewed libraries.

Try the Password Strength Checker

Put what you've learned into practice with this free BestWordz tool.

Open Tool →

💬 Discuss on BestWordz Community

Join the conversation about Encryption, Cryptography, Authentication on the BestWordz Community forum.

Visit Forum →