Online Privacy

Hashing vs. Encryption vs. Encoding

Tell the three apart with worked examples, know which one protects secrets, and learn how to pick the right tool for each job.

Key takeaways

  • Encoding needs no key and anyone can reverse it, so it provides no secrecy.
  • Encryption is reversible only with the correct key, which makes it the tool for keeping data confidential.
  • Hashing is one-way and produces the same fingerprint for the same input, which suits integrity checks.
  • Passwords should be stored with a slow, salted password hashing function instead of a fast general-purpose hash.
  • A keyed hash or a digital signature is needed to show that data came from a particular source.

Encoding rewrites data into another format so systems can carry it. Encryption hides data from anyone without a key. Hashing boils data down to a fixed-length fingerprint that isn't meant to be turned back. Only encryption is designed to keep a readable message secret and let the right person recover it.

Mixing them up causes real problems: passwords "protected" with Base64, or files "encrypted" with a hash that nobody can open. This guide shows how each behaves and how to choose.

One word, three treatments

Take the word hello.

Encoded with Base64, it becomes aGVsbG8=. Anyone can decode that back to hello instantly.

Hashed with SHA-256, it becomes a 64-character hexadecimal string that begins 2cf24dba. Every time you hash exactly those five bytes you get the same result. Change one letter to a capital, Hello, and the output begins 185f8db3 and shares no useful resemblance.

Encrypted with a modern cipher and a key, it becomes bytes that look random. With a fresh random value each time, as good practice requires, encrypting hello twice gives different output. With the right key you recover the word; without it you can't.

Three ways to transform data
Encoding Purpose: safe transport and compatibility No key Reversible by anyone Examples: Base64, URL encoding, UTF-8 Encryption Purpose: confidentiality Needs a key Reversible by key holders Examples: AES, ChaCha20, RSA Hashing Purpose: fingerprints and integrity No key in a plain hash Not designed to be reversed Examples: SHA-256, SHA-3 Encoding Purpose: safe transport and compatibility No key Reversible by anyone Examples: Base64, URL encoding, UTF-8 Encryption Purpose: confidentiality Needs a key Reversible by key holders Examples: AES, ChaCha20, RSA Hashing Purpose: fingerprints and integrity No key in a plain hash Not designed to be reversed Examples: SHA-256, SHA-3

Encoding: a change of clothes

Encoding solves compatibility problems. Some channels accept only certain characters, so data has to be represented within those limits.

  • Base64 (RFC 4648) represents binary data using 64 printable characters. It makes output about a third larger. Email attachments and data URLs use it.
  • URL encoding, or percent-encoding, turns a space into %20 so addresses stay valid.
  • HTML entities write < so a browser shows a less-than sign instead of reading a tag.
  • UTF-8 encodes text characters as bytes.

None of these hides anything. A Base64 string may look cryptic to a person, but software decodes it without any secret. Tokens such as JSON Web Tokens in their common signed form use Base64url for their contents, so anyone holding the token can read the claims. Try a Base64 converter or a URL encoder and inspector to see how easily it reverses.

On Linux or macOS:

printf 'hello' | base64
printf 'aGVsbG8=' | base64 --decode

Encryption: reversible with a key

Encryption transforms plaintext into ciphertext using an algorithm and a key. Decryption reverses it. Without the key, a sound cipher gives an attacker no practical way back. Output length usually tracks input length, plus a little for random values and an integrity tag.

There are two main families, one shared key or a public and private pair, compared in symmetric vs. public-key encryption. For the broader picture see encryption explained.

Use encryption whenever you'll need the original again: files, backups, messages, stored customer details, traffic on a network. The hard part is managing keys. If the key sits right beside the encrypted data, an intruder who takes one takes both.

Hashing: a fixed-size fingerprint

A cryptographic hash function takes input of any size and returns a digest of fixed size. SHA-256, from FIPS 180-4, always yields 256 bits whether the input is a word or a large video. Good hash functions aim for these properties:

  1. Deterministic: identical input gives identical output.
  2. Preimage resistance: given a digest, it is infeasible to find an input that produces it.
  3. Second preimage resistance: given one input, it is infeasible to find another with the same digest.
  4. Collision resistance: it is infeasible to find any two inputs with matching digests.
  5. Avalanche behavior: a tiny change alters the output drastically.

Collisions must exist in theory, since unlimited inputs map to limited outputs. The aim is that no one can find them. Older functions MD5 and SHA-1 have practical collision attacks and shouldn't be used where security matters. The SHA-2 family and SHA-3 (FIPS 202) are the usual choices.

Because digests can't be "decrypted," websites offering to reverse a hash really look it up in a table of previously hashed guesses. Short or common inputs fall quickly that way.

Checking a download

If a publisher lists a SHA-256 value, compare it with your copy:

Get-FileHash .\installer.exe -Algorithm SHA256
sha256sum installer.iso       # Linux
shasum -a 256 installer.dmg   # macOS

A match shows your file is identical to the one that produced the published value. It doesn't show the file is safe, and if an attacker controls the web page, they can change both the file and the listed hash. Signatures address that gap. You can also compute digests with a file hash tool or hash generator.

Verifying a file with a hash
1 Publisher hashes the file The digest is posted, ideally through a trusted channel. 2 You download the file The copy may travel through mirrors you don't control. 3 You hash your copy Use the same function, such as SHA-256. 4 Compare the digests Identical means unchanged. Any difference means stop. Publisher hashes the file The digest is posted, ideally through a trusted channel. 1 You download the file The copy may travel through mirrors you don't control. 2 You hash your copy Use the same function, such as SHA-256. 3 Compare the digests Identical means unchanged. Any difference means stop. 4

Password storage: a special case

A service doesn't need to read your password back. It only needs to check that what you typed matches. So it should store a hash. A fast function like plain SHA-256 is the wrong choice, because fast means an attacker with a stolen database can try enormous numbers of guesses.

Proper password hashing functions are deliberately slow and often memory-hungry: Argon2 (RFC 9106), scrypt (RFC 7914), bcrypt and PBKDF2 (RFC 8018). Each password also gets a unique random salt, stored alongside the hash, so identical passwords produce different records and precomputed tables become useless. Details are in how websites should store passwords.

Developers should use a maintained library for this and its recommended settings instead of inventing a scheme.

A worked scenario: a leaked user table

Imagine a hypothetical forum whose database is copied by an intruder. What happens next depends entirely on how the password column was treated.

If passwords were only Base64 encoded, the intruder decodes the whole column at once. Nothing was protected. If they were encrypted, everything depends on the key. Should the key sit in a configuration file on the same server, assume the intruder has it too, and every password opens together.

If they were hashed with a fast function and no salt, identical passwords share identical digests. The most popular choices stand out by how often they repeat, and large precomputed lists crack many of them quickly. If each record has its own salt and a slow function, the intruder must attack accounts one at a time and pay the full cost for every guess. Weak passwords can still fall, but strong and unique ones are likely to hold, and users gain time to change them.

For you as a user, the lesson is that you can't see which approach a site chose. A unique password for every account limits the damage whichever it was, as explained in credential stuffing and password reuse. If a site can email your existing password back to you, it is storing something reversible, which is a good reason to be careful with it.

Keyed hashes and signatures

A plain hash proves nothing about who produced it. Two constructions add that:

  • HMAC (RFC 2104) mixes a secret key into the hashing process. Someone with the same key can confirm the message is authentic and unaltered. Web services often use it to sign requests and webhook deliveries.
  • Digital signatures hash the data and then apply a private key, so anyone with the public key can verify. See digital signatures explained.

Checksums are a weaker cousin

You'll also meet checksums and cyclic redundancy checks, such as CRC32, in network frames, archive files and storage systems. They are fast and good at catching accidental damage, like a flipped bit on a noisy link or a truncated transfer.

They offer no protection against someone deliberately changing data, because it is easy to construct a different input with the same CRC. Use a checksum to catch accidents and a cryptographic hash when someone might want to cheat. Some systems use both at different layers: a cheap check on every packet and a strong digest for the finished file.

Hashes also do quiet work that has nothing to do with secrecy. Version control tools name content by its digest, backup software uses digests to avoid storing the same block twice, and download systems check each piece of a large file as it arrives. In each case the digest acts as a compact name that changes whenever the content does.

Which one do you need?

Goal Use Don't use
Put binary data in a text field Encoding Hashing
Keep a file secret and open it later Encryption Encoding
Detect accidental corruption Hash or checksum Encryption alone
Store user passwords Password hashing with salt Fast hash or reversible encryption
Prove a message came from a partner HMAC or signature Plain hash
Make an address safe for a URL URL encoding Encryption
Choosing a transformation
Do you only need the data to fit a format or channel? YES Encode it, and treat it as fully readable. 1 NO Must someone read the original later, but not everyone? YES Encrypt it and protect the key separately. 2 NO Is it a password you only need to check? YES Use a slow, salted password hashing function. 3 NO Fingerprint it Hash for integrity; add a key or signature to show origin. Do you only need the data to fit a format or channel? 1 IF YES Encode it, and treat it as fully readable. NO Must someone read the original later, but not everyone? 2 IF YES Encrypt it and protect the key separately. NO Is it a password you only need to check? 3 IF YES Use a slow, salted password hashing function. NO Fingerprint it Hash for integrity; add a key or signature to show origin.

How to recognize what you're looking at

When you meet a mystery string, a few clues help, though none are proof. Trailing = signs and a mix of upper case, lower case, digits, + and / suggest Base64. A string of exactly 64 hexadecimal characters is the size of a SHA-256 digest, and 32 hexadecimal characters is the size of MD5. Password records often start with a labeled prefix naming the algorithm. Encrypted data usually has no readable structure unless a file format adds a header.

If decoding yields readable text, it was encoded. Never paste real tokens, keys or password records into an online tool you don't trust.

How they work together

Real systems rarely use just one. A signed and encrypted download might be compressed, hashed, signed, encrypted and then Base64 encoded to pass through a text-only channel. Each step has one job, and removing the encoding at the end reveals only ciphertext.

The order of reasoning matters more than the order of steps. Ask what you need protected: format, secrecy, integrity or origin. Then check that some specific step provides each one. A design that lists Base64 under secrecy, or a bare hash under origin, has a hole in it.

Common mistakes

  • Calling Base64 "encryption" in documentation, which leads others to trust it.
  • Hashing sensitive values with a small set of possibilities, such as phone numbers, and calling them anonymous. Someone can hash every possibility and match.
  • Reusing one salt for every password.
  • Comparing only the first few characters of a digest.
  • Compressing and assuming the result is protected. Compression is another reversible format change.

Written for 4Internet with AI writing tools and not reviewed line by line by a person. Check anything you rely on with the maker, provider or standard concerned. Report an error.

Keep reading

All 45 articles in Online Privacy →

Latest news on this topic