What Is a Hash Function?
A hash function maps data of arbitrary length to a hash value of fixed length.
Virtually any kind of data can be used as input: a single word, a longer text, a password, or even a file several gigabytes in size.
For example,
Hello World
becomes the following hash value when using SHA-256:
a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
SHA-256 always produces a 256-bit hash value, which is typically represented as a 64-character hexadecimal number.
At first glance, the hash value looks like a random string of characters. However, one important property of a hash function is that the same input always produces the same hash value when processed by the same hash function.
Even a tiny change to the input, on the other hand, can produce a completely different hash value.
How Does a Hash Function Work?
At a basic level, you can think of a hash function as a mathematical function:
Input → Hash function → Hash value
The input can vary greatly in size: both a short piece of text and a very large file can be processed.
With a cryptographic hash function such as SHA-256, the resulting hash value always has a length of 256 bits. When represented as a hexadecimal number, this corresponds to 64 characters.
For example:
"Hi"
↓
SHA-256
↓
256 bits
64 hex characters
"Hello World"
↓
SHA-256
↓
256 bits
64 hex characters
a file several gigabytes in size
↓
SHA-256
↓
256 bits
64 hex characters
The size of the input therefore does not determine the length of the hash value.
Hash Value and Digest
The result of a hash function is called a hash value, or simply a hash. In the context of cryptographic hash functions, you will also encounter the terms digest or message digest.
The hash value is a fixed-length representation of the input, with the length determined by the respective algorithm. It is not a compressed version of the original data, nor does it simply contain the original data in a smaller form.
Try It Yourself
You can try out directly how different inputs produce different hash values. Enter any text and calculate its SHA-256 hash.
You can also find the Hash Generator with additional algorithms in the Tools section.
For example, start by entering:
Hello World
and then change just a single character.
Change:
Hello World
to:
hello World
The two inputs differ by only one character: the uppercase H has been replaced by a lowercase h. Nevertheless, the resulting hash values differ in many places.
This behavior is an important property of cryptographic hash functions. I explain why this happens and what lies behind the so-called avalanche effect in more detail below.
The Same Input Produces the Same Hash Value
An important property of a hash function is its determinism.
If exactly the same input is processed by the same hash function, it always produces the same hash value.
For example, Hello World always produces the same hash when using SHA-256.
It does not matter whether you calculate the hash today, tomorrow, or a year from now — with the same input and the same algorithm, the result is identical.
This makes hash values useful, for example, when verifying files.
If you download a file and then calculate its hash value, you can compare it with a hash value published by the provider. If the two values match, this indicates that the downloaded file matches the expected version.
There is an important caveat, however: the reference hash value itself must have been obtained from a trustworthy source. A simple hash comparison does not automatically prove the authenticity or origin of a file.
Why Does a Hash Have a Fixed Length?
Cryptographic hash functions map inputs of arbitrary length to a hash value with a fixed, predefined length.
SHA-256 always produces a 256-bit hash value. When represented as a hexadecimal number, this corresponds to 64 characters.
It makes no difference whether the input consists of three characters or several million characters.
For example:
abc
↓
SHA-256
↓
256 bits
64 hex characters
and:
A very long text ...
↓
SHA-256
↓
256 bits
64 hex characters
This fixed output length is a fundamental property of hash functions.
However, it also leads to an interesting mathematical consequence: there are far more possible inputs than possible hash values.
Therefore, different inputs must exist that produce the same hash value. Such a match is called a hash collision.
This does not mean that you can simply find two arbitrary texts with the same SHA-256 hash. What matters for a cryptographic hash function is how difficult it is to deliberately find a collision.
I explain in detail why such collisions are unavoidable and why they do not automatically represent a security problem in What Is a Hash Collision?
What Happens If You Change Just One Character?
This is where an important property of cryptographic hash functions becomes visible.
Consider two inputs:
Hello World
hello World
We changed just a single character:
H → h
The inputs are therefore almost identical.
Their hash values are not.
With SHA-256, we get:
Hello World
a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e
and:
hello World
db4067cec62c58bf8b2f8982071e77c082da9e00924bf3631f3b024fa54e7d7e
This behavior is intentional. A small change to the input should not cause only a small change to the hash value. Instead, as many bits of the hash value as possible should change.
With a good cryptographic hash function, even a small change to the input typically causes approximately half of the output bits to change.
This property is known as the avalanche effect.
The Avalanche Effect
In simple terms, the avalanche effect means:
A small change to the input causes a large, seemingly random change to the hash value.
With SHA-256, the hash value consists of 256 bits.
If a single bit of the input is changed, an ideal hash function is expected to change approximately half of the output bits.
For this example, we can observe the effect by comparing the two hash values.
We change:
Hello World
to:
hello World
This changes exactly one character of the input.
When the resulting SHA-256 hash values are compared bit by bit, 124 out of 256 bits differ.
That corresponds to:
124 / 256 × 100 ≈ 48.4%
The input therefore changed only slightly, while almost half of the hash bits changed.
This is exactly the behavior illustrated by the avalanche effect.
Importantly, the roughly 50% figure is not a guarantee for every individual change to an input. It describes the expected behavior of an ideal or appropriately designed cryptographic hash function.
You also cannot directly determine from the hash value which character of the input was changed.
The avalanche effect is therefore an important property in the design of cryptographic hash functions.
I explain exactly how the avalanche effect works and why approximately 50% changed bits are often discussed in What Is the Avalanche Effect?
Hashing Is Not Encryption
Hash functions are often confused with encryption. While both techniques operate on data, they serve different purposes.
With encryption, plaintext is transformed into ciphertext. Using the appropriate key, the ciphertext can be decrypted again:
Plaintext
↓
Encryption
+ key
↓
Ciphertext
↓
Decryption
+ key
↓
Plaintext
Hashing, on the other hand, has no comparable decryption step:
Data
↓
Hash function
↓
Hash value
A hash function is therefore not a method for securely encrypting data.
If you hash a piece of text, for example, you cannot simply transform the hash value back into the original text.
This is a fundamental difference between hashing and encryption:
Encryption is designed so that data can be decrypted again. A hash value, by contrast, is the result of a hash function and is not intended to restore the original data.
However, this does not mean that determining the original input corresponding to a hash value is fundamentally impossible. For small or known input spaces, for example, many possible inputs can be tried and their hash values compared. I explain why this is not decryption in Can You Reverse a Hash?
Hashing Is Not Encoding Either
Another common misconception is confusing hashing with encoding.
With encoding, data is transformed into another format according to a defined set of rules. A well-known example is Base64.
Base64 is not encryption. The encoding is reversible: the original data can be recovered from a Base64 representation.
Data
↓
Encoding
↓
Base64
↓
Decoding
↓
Data
Hashing does not work this way:
Data
↓
Hash function
↓
Hash value
A hash value is therefore not simply another representation of the original data.
The three concepts can be distinguished in simplified form as follows:
| Method | Result | Can the original data be recovered? |
|---|---|---|
| Hashing | Hash value | Not through a decryption or decoding step |
| Encryption | Ciphertext | Yes, with the appropriate key |
| Encoding | Encoded data | Yes, through decoding |
Well-Known Hash Functions
Numerous hash algorithms have been developed over time.
Some of the best-known include:
| Algorithm | Hash length | Classification |
|---|---|---|
| MD5 | 128 bits | Not suitable for applications that require collision resistance |
| SHA-1 | 160 bits | No longer suitable for new applications that require collision resistance |
| SHA-256 | 256 bits | Widely used member of the SHA-2 family |
| SHA-512 | 512 bits | Member of the SHA-2 family |
| SHA3-224 | 224 bits | Member of the SHA-3 family |
| SHA3-256 | 256 bits | Member of the SHA-3 family |
| SHA3-384 | 384 bits | Member of the SHA-3 family |
| SHA3-512 | 512 bits | Member of the SHA-3 family |
MD5 is vulnerable to known collision attacks and should therefore not be used for applications that require collision resistance. However, MD5 may still be encountered in certain non-cryptographic use cases.
SHA-1 likewise no longer provides the expected collision resistance and should not be used for new applications where this property is required.
SHA-256 and SHA-512 belong to the SHA-2 family. SHA3-224, SHA3-256, SHA3-384, and SHA3-512 belong to the SHA-3 family.
The differences between these algorithms include their security properties, hash lengths, internal designs, and historical development. I cover these differences in more detail in dedicated articles.
Key Takeaways
A hash function maps data of arbitrary length to a hash value of a defined length.
The most important properties to remember are:
- The same input produces the same hash value when processed by the same hash function.
- The length of the hash value is independent of the size of the input.
- A small change to the input can cause a large change to the hash value.
- In cryptographic hash functions, this behavior is known as the avalanche effect.
- Hashing is not encryption.
- Hashing is not encoding either.
- Different inputs can produce the same hash value. This is called a hash collision.
- Not every hash algorithm is suitable for every use case.
Hash functions are therefore a fundamental building block of modern cryptography and many IT security applications.
Where to Go Next
Once you understand the fundamental properties of hash functions, you can explore individual topics in much greater detail.
Fundamentals
- What Is a Hash Collision?
- The Avalanche Effect in Detail
- Preimage Resistance
- Second-Preimage Resistance
- Collision Resistance
Hash Algorithms
- What Is SHA-256?
- SHA-512 and the SHA-2 Family
- What Is SHA-3?
- Why Is MD5 Insecure?
- Why Is SHA-1 Considered Insecure?
Password Security
- Why Is SHA-256 Not Suitable for Passwords?
- How Does Password Hashing Work?
- bcrypt, scrypt, and Argon2 Compared
- What Is a Salt and Why Is It Used for Password Hashing?
Sources and Standards
The following primary and standards sources form the basis for the technical statements in this article:
-
National Institute of Standards and Technology (NIST), FIPS 180-4 – Secure Hash Standard (SHS)
Specification of the SHA-1 and SHA-2 hash algorithms, including SHA-256 and SHA-512. -
National Institute of Standards and Technology (NIST), FIPS 202 – SHA-3 Standard
Specification of SHA3-224, SHA3-256, SHA3-384, SHA3-512, as well as SHAKE128 and SHAKE256. -
National Institute of Standards and Technology (NIST), Hash Functions – NIST Policy on Hash Functions
Current overview and classification of hash functions supported by NIST. -
National Institute of Standards and Technology (NIST), CSRC Glossary – Hash Function
Definition of a hash function and the properties collision resistance, preimage resistance, and second-preimage resistance. -
NIST SP 800-107 Rev. 1 – Recommendation for Applications Using Approved Hash Algorithms
NIST recommendation concerning applications and security properties of cryptographic hash functions. At the end of 2022, NIST announced its intention to withdraw the publication; relevant content was transferred to the current NIST Hash Functions page. -
IETF, RFC 6151 – Updated Security Considerations for the MD5 Message-Digest and the HMAC-MD5 Algorithms
Security assessment of MD5, particularly its known collision issues. -
IETF, RFC 6194 – Security Considerations for the SHA-0 and SHA-1 Message-Digest Algorithms
Security considerations and known weaknesses of SHA-1.