StringMash.com

Hashing vs encryption

One can be undone with the right key. The other can't be undone at all.

The short answer

Encryption scrambles data so that only someone with the key can turn it back: it's reversible by design. Hashing turns data into a fixed-length fingerprint that can't be turned back into the data, with or without a key. Use encryption to keep something secret and get it back later; use hashing to check something without keeping it, such as a password or a file's integrity.

See both on your own text

Type in the box. The hash changes completely with every letter, and stays the same length. The encrypted text can only be turned back with the password, and decrypting it gives your text exactly.

SHA-256 hashOne-way
caa2b0d00d8f66c9e841d4d136932ccbd1e04bac894ae6403670c4a3f30ba22564 characters, 64 bytes
Encrypted (AES-GCM demo)Reversible with key
oMxBXkmE+r+MO2LRxf4Ix+mFnH0Zxdb+LYjRV3et/fLQV0BCXhUklyiE50y1LOgmBNnwP9xEGP5mSzt9pXwscuo/udlvsr6TwmA=100 characters, 100 bytes
Decrypted
Meet me at the station at noon30 characters, 30 bytes
Open the SHA-256 generator →

Side by side

EncryptionHashing
PurposeKeep data secret, then read it againCheck or identify data without storing it
Reversible?Yes, with the keyNo
KeyNeeded to encrypt and to decryptNone (a salt or HMAC key is a different thing)
Output lengthGrows with the inputAlways the same, whatever the input
Same input twiceUsually different output (random IV)Always the same output
Typical usesHTTPS, disk encryption, messagingPassword storage, checksums, Git, signatures
ExamplesAES, ChaCha20, RSASHA-256, SHA-3, BLAKE2; bcrypt and Argon2 for passwords

The avalanche effect

Change one letter, Hello to Jello, and about half the bits of a good hash flip. That's why a hash can't be nudged towards a target: nothing about the output tells you how close the input was.

For comparison, SHA-256 of Hello is `185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969` and of Jello is `2c5cccf620a95c8f5d20dceb7ec4ab6b6225319b215e9c00f697caeb9ae79a1b`. One letter apart, and nothing in common.

SHA-256 of your text
185f8db32271fe25f561a6fc938b2e264306ec304eda518007d176482638196964 characters, 64 bytes

Why passwords are hashed, not encrypted

A site never needs to know your password, only that what you typed matches. So it stores a hash, and on login hashes what you type and compares the two. If the database leaks, there's no key that turns the hashes back into passwords. An encrypted password store is weaker: whoever gets the key gets every password.

Plain SHA-256 or MD5 isn't enough, though. They're built to be fast, which lets an attacker try billions of guesses a second against a leaked hash. Password storage uses deliberately slow functions with a salt, a random value stored with each hash so identical passwords get different hashes. OWASP's current advice is Argon2id, with bcrypt or scrypt as alternatives and PBKDF2 where FIPS-140 compliance is needed.

About the encryption demo

The demo uses AES-256-GCM through your browser's Web Crypto API, with the key made from the password by PBKDF2 (600,000 rounds) and a random salt and IV. It's there to show the difference, not to protect anything: for real secrets, use an established tool. Nothing you type, password included, leaves the page.

Questions

Is hashing more secure than encryption?

Neither is more secure; they do different jobs. Encryption is for data you need back. Hashing is for data you only need to check.

Can a hash be decrypted?

No. There's no key. The only attack is guessing inputs and comparing hashes, which is why weak passwords and fast hashes are a risk.

Is SHA-256 encryption?

No. SHA-256 is a hash function: one-way, fixed-length, with no key.

What's a salt?

A random value stored next to each password hash and mixed into it, so two people with the same password get different hashes and precomputed tables don't work.

Sources

Added . What's new