What Is a Hash Function? SHA-256, Use Cases, and Why It's Not Encryption

A hash function is a function that, no matter how long the data you feed it, returns a fixed-length value (a hash value). With SHA-256, whether the input is a single character or a one-gigabyte file, the result is always a 256-bit value (64 hexadecimal characters). What matters is that the same input always produces the same value (deterministic), while you cannot get the original input back from the hash value (one-way). Using SHA-256 as an example, this article lays out how hash functions work, their properties, the difference from encryption, and the correct way to use them for password storage and tamper detection.

The bottom line first: a hash cannot be decrypted. Use encryption for data you want to keep secret and read back later; use hashing when you need a fingerprint of data for matching or tamper detection. For new uses, choose SHA-256/SHA-512 (or SHA-3). MD5 and SHA-1 have known collisions and are deprecated. For password storage, do not use raw SHA-256 — use a salt plus bcrypt / Argon2.

1. What a hash function is — any length to fixed length, one-way and deterministic

A hash function takes input (a message) of any length and converts it into a fixed-length bit string (a hash value, or digest). The leading example, SHA-256, always produces a 256-bit output no matter what the input is. It has three basic properties.

Let us confirm that the same input always yields the same 64 characters with a concrete example.

SHA-256("hello") = 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

A hash value is often likened to a fingerprint of data. Just as you cannot reconstruct a person from a fingerprint, you cannot recreate the original data from a hash value. But you can reliably compare "whether this fingerprint matches that fingerprint." That is the essence of hashing.

2. Properties of hash functions — collision resistance, preimage resistance, the avalanche effect

A cryptographically secure hash function does more than return a fixed length; it is designed to satisfy the following properties.

Another important property is the avalanche effect. Changing the input by just one bit flips about half of the output bits, so the result looks completely different. Compare the example below (only a single trailing . was added).

InputSHA-256 (leading part)
The quick brown foxa value such as 5cac4f98…
The quick brown fox.changes to something entirely different like 7d38b56b…

Because of this, a hash value gives no hint at all about which parts of the original data are similar. Even a one-character tamper changes the hash dramatically, which makes hashing well suited to tamper detection.

3. The difference from encryption — a hash cannot be decrypted

Hashing and encryption are often confused, but their purpose and reversibility are entirely different. The biggest difference is whether you can get the original back.

AspectHash (SHA-256, etc.)Encryption (AES, etc.)
PurposeFingerprint / matching / tamper detectionKeeping data secret (making it unreadable)
ReversibilityIrreversible (cannot be decrypted)Reversible (decryptable with the key)
KeyNot requiredRequired (encryption/decryption key)
Output lengthFixed lengthRoughly proportional to the input
Typical examplePassword matching / checksumsEncrypting data in transit or at rest

Encryption is a mechanism that assumes a holder of the correct key will later restore the plaintext. A hash, by contrast, has no "restore" operation defined at all. The phrase "decrypt a hashed password" is incorrect; all you can actually do is hash the entered password again and compare it with the stored value.

The common claim of "cracking a hash" does not mean it was decrypted — it means a guessed input happened to hash to a matching value (brute-force or dictionary attack). That is why measures that make guessing the input harder (salting and stretching, described later) are effective.

4. Representative algorithms — MD5/SHA-1 deprecated, SHA-256/512 recommended

There are many kinds of hash functions, some confirmed secure and some not.

AlgorithmOutput lengthRecommendation / notes
MD5128 bitsDeprecated. Collisions are easy to construct. Not for cryptographic use
SHA-1160 bitsDeprecated. A practical collision was published in 2017 (SHAttered)
SHA-256256 bitsRecommended. SHA-2 family. The current standard choice
SHA-512512 bitsRecommended. SHA-2 family. Often faster on 64-bit platforms
SHA-3VariableRecommended. A newer generation with a different internal structure from SHA-2
MD5 and SHA-1 have known collisions. MD5 was broken long ago, and in 2017 a concrete example of "two different inputs producing the same hash value" was published for SHA-1. Never use them where collision resistance is required, such as signatures, certificates, or tamper detection. For new implementations, choose SHA-256 / SHA-512 (or SHA-3).

For non-cryptographic purposes with no adversary, such as simple deduplication or cache keys, MD5 is not immediately problematic. Still, to avoid confusion and misuse, it is safest to avoid it in new implementations.

5. Use cases — password storage, tamper detection, data identification

Hash functions are widely used close at hand. Here are the representative use cases, along with how to use them correctly.

6. Cautions — do not store passwords with raw SHA, use salt and stretching

Finally, let us cover the easiest mistake. Do not hash passwords with raw SHA-256 for storage. The reason is that SHA-256 is built to be fast to compute. Speed is usually an advantage, but for an attacker it means "an enormous number of candidates can be tried per second," which works in their favor.

Salt

A salt is a unique random value added to the password per user before hashing. It defends against the following attacks.

Key stretching

Key stretching deliberately makes a single verification heavy by repeating the hash computation thousands to hundreds of thousands of times, or by forcing it to use a large amount of memory. A slowdown imperceptible to legitimate users becomes a major barrier to an attacker doing brute force. The functions that implement this safely are dedicated password functions such as bcrypt, scrypt, and Argon2.

In short: the correct answer for password storage is "a salt plus a dedicated function with a tunable cost (bcrypt / scrypt / Argon2)." For new work, Argon2 is recommended. General-purpose SHA-256 is ideal for file checksums and data identification, but it is not the function to use directly for passwords.
Free Tool Compute one for real with the Hash Generator Compute hash values such as SHA-256 for text right in your browser and copy them. You can also see the "avalanche effect" on the spot — change a single character and watch the hash transform completely.

Frequently Asked Questions (FAQ)

What is the difference between hashing and encryption?

The biggest difference is whether you can get the original back. Encryption uses a key to turn plaintext into ciphertext, and with the correct key you can decrypt it back to the original plaintext (reversible). A hash function, by contrast, only computes a fixed-length value from the input, and there is no procedure to recover the original input from the hash value (one-way and irreversible). So while you can "store a password as a hash," you cannot "decrypt hashed data" in principle. Use encryption for data you want to keep secret and read back later, and use hashing as a fingerprint for tamper detection or matching.

Are SHA-1 and MD5 still safe to use?

They must not be used where collision resistance is required, such as signatures, certificates, or tamper detection. MD5 was broken long ago, and practical collisions for SHA-1 (two different inputs producing the same hash value) were published in 2017; both are deprecated. For new uses, use SHA-256 or SHA-512 (the SHA-2 family) or SHA-3. For non-cryptographic purposes with no adversary, such as simple deduplication or cache keys, continuing to use MD5 is not immediately dangerous, but it is safest to avoid it in new implementations to prevent confusion.

Is SHA-256 alone enough for storing passwords?

No. SHA-256 is designed to be fast to compute, and that speed works in the attacker's favor, so hashing passwords with raw SHA-256 for storage is inappropriate. Attackers can try an enormous number of candidates per second on a GPU, and without a salt they can also use rainbow tables or spot users who share the same password. For passwords, add a unique salt per user and use a dedicated password-hashing function (key derivation function) with a tunable cost, such as bcrypt, scrypt, or Argon2. These are intentionally slow and make brute-force attacks hard.

← Back to the Tech Blog list