Hashing transforms data of any length into a fixed-size string (a hash). Hash functions are used to check file integrity, store passwords, create digital signatures, and power blockchains. This article explains how MD5, SHA-256, and SHA-512 work, when to use them, and why some algorithms are no longer suitable for protecting data.
To calculate a hash quickly, use the online hash generator on reChecker.
What a hash and hash function are
A hash function takes input data of any length and returns an output of fixed length. The same input always produces the same hash. Changing even one bit in the input produces a completely different result — this property is called the avalanche effect.
A hash is irreversible: you cannot recover the original data from a hash. This is what distinguishes hashing from encryption, where data can be decrypted with a key.
The avalanche effect
The avalanche effect is a key property of cryptographic hash functions. Changing one bit in the input data should change about half of the output bits. For example, the hashes of the strings Hello, World! and Hello, World? (which differ in the last character) are completely different. This makes it harder to find collisions and predict a hash from a similar input.
Key properties of good hash functions
| Property | Description |
|---|---|
| Determinism | The same input always produces the same hash |
| Fixed length | The output size does not depend on the input size |
| Avalanche effect | A small change to the input significantly changes the hash |
| Collision resistance | It is difficult to find two different inputs with the same hash |
| One-way property | It is not feasible to calculate the input from the hash in a reasonable amount of time |
MD5: history and use
MD5 (Message-Digest Algorithm 5) was developed by Ronald Rivest in 1991. It produces a hash 128 bits long (32 hexadecimal characters). The algorithm processes data in 512-bit blocks and uses bitwise operations, modular addition, and shifts.
Why MD5 is no longer secure
Cryptanalysis has revealed vulnerabilities in MD5:
- Collisions — two different inputs with the same hash can be found in a reasonable amount of time
- Precomputed attacks — rainbow tables make it possible to quickly guess passwords from their hashes
- Lack of cryptographic security — NIST and other organizations do not recommend MD5 for security
Where MD5 is still suitable
MD5 remains useful for non-critical tasks:
- Checking file integrity when downloading (alongside SHA-256)
- Cache keys and HTTP ETags
- Quickly checking for duplicates in non-sensitive data
- Identifiers (for example, Git commits) — collisions do not provide direct access in these cases
Do not use MD5 for passwords, signatures, or data protection.
How MD5 works internally
MD5 processes input in 512-bit blocks (64 bytes). Each block passes through four rounds of 16 operations. The algorithm uses bitwise functions (AND, OR, XOR, NOT), addition modulo 2³², and circular shifts. The final hash is the concatenation of four 32-bit registers. The algorithm's simple structure and short output are what made successful attacks possible.
The SHA family
SHA (Secure Hash Algorithm) is a family of cryptographic hash functions developed by NIST.
SHA-1: an obsolete standard
SHA-1 produces 160 bits (40 characters). It has been considered obsolete since 2017: Google demonstrated a practical collision. Do not use SHA-1 for new projects. Browsers are stopping their trust in SHA-1 in certificates.
SHA-256
SHA-256 is part of SHA-2 and produces 256 bits (64 characters). It is widely used in TLS, certificates, digital signatures, and blockchain (Bitcoin). It is considered cryptographically secure for the foreseeable future.
SHA-512
SHA-512 is a longer version of SHA-2, with 512 bits (128 characters). On 64-bit systems, it is often faster than SHA-256. It is used when greater security or compatibility with older standards is needed.
Differences between SHA-256 and SHA-512
| Parameter | SHA-256 | SHA-512 |
|---|---|---|
| Output length | 256 bits | 512 bits |
| Block size | 512 bits | 1024 bits |
| Speed on 64-bit systems | Slower | Faster |
| Use | TLS, signatures, blockchain | Longer hashes, compatibility |
The choice between SHA-256 and SHA-512 is most often determined by compatibility. SHA-256 is shorter and supported everywhere. SHA-512 provides a greater security margin and can run faster on modern servers. Both are sufficient for most tasks.
Use cases
Password storage
Passwords should never be stored in plain text. Hash them with a salt (a unique random string for each user):
хеш = hash(соль + пароль)A salt prevents the use of rainbow tables: even if different users have the same password, they will have different hashes. Recommended algorithms include bcrypt, scrypt, and Argon2 — they are deliberately slow and resistant to brute-force attacks. Plain SHA-256 without iterations is a weak choice for passwords: an attacker can try millions of hashes per second on a GPU. Specialized functions (bcrypt, Argon2) slow down brute-force attacks thousands of times.
Checking file integrity
When downloading a distribution, check its hash:
sha256sum linux.iso
# Сравнить с хешем на сайте разработчикаMatching hashes confirm that the file was not changed during transfer. Many websites publish hashes next to download links. If the hashes do not match, the file may have been corrupted during transfer or replaced. Always check the hashes of critical files: installers, OS images, and cryptographic keys.
Digital signatures
A signature is created by hashing a document and encrypting the hash with a private key. The recipient verifies the signature using a public key. SHA-256 and SHA-512 are standard choices for signatures.
Blockchain
In Bitcoin and many cryptocurrencies, hashes link blocks into a chain. Each block contains the hash of the previous block. Changing any block changes its hash and breaks the entire subsequent chain — this ensures the history cannot be changed. Bitcoin uses double SHA-256 (SHA-256 of the result of SHA-256) for additional security. Mining is the search for an input whose hash meets the difficulty requirement (starts with a specified number of zero bits).
Checksums
MD5 and SHA are used as checksums for backups, synchronization, and deduplication.
Why MD5 and SHA-1 must not be used for security
| Algorithm | Problem |
|---|---|
| MD5 | Collisions can be found in minutes on an ordinary PC |
| SHA-1 | Practical collisions have been demonstrated |
For passwords, signatures, certificates, and any cryptographic tasks, choose SHA-256 or SHA-512. MD5 and SHA-1 are acceptable only for non-critical integrity checks.
Practical examples
JavaScript: Web Crypto API
async function sha256(text) {
const encoder = new TextEncoder();
const data = encoder.encode(text);
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
const hashArray = Array.from(new Uint8Array(hashBuffer));
return hashArray.map(b => b.toString(16).padStart(2, '0')).join('');
}
sha256('Hello, World!').then(console.log);
// dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986fThe Web Crypto API supports SHA-1, SHA-256, SHA-384, and SHA-512. It does not include MD5 — use third-party libraries only for non-critical tasks.
Python: hashlib
import hashlib
# SHA-256
data = b'Hello, World!'
print(hashlib.sha256(data).hexdigest())
# SHA-512
print(hashlib.sha512(data).hexdigest())
# MD5 (только для некритичных задач)
print(hashlib.md5(data).hexdigest())Command line
# SHA-256 файла
sha256sum document.pdf
# SHA-256 строки
echo -n "Hello, World!" | sha256sum
# SHA-512
echo -n "Hello, World!" | sha512sumOn macOS, the sha256sum command may be unavailable — use shasum -a 256:
echo -n "Hello, World!" | shasum -a 256The -n flag in echo matters: without it, a newline is added and the hash will be different.
Output format
Hashes are usually represented in hexadecimal (hex): each byte is two characters, 0–9, a–f. SHA-256 produces 32 bytes — 64 hex characters. Base64 is also used (for example, in some APIs), but hex is common for integrity checks and debugging.
Algorithm comparison
| Algorithm | Output length | Speed | Security | Recommendation |
|---|---|---|---|---|
| MD5 | 128 bits | Very fast | Broken | Checksums only |
| SHA-1 | 160 bits | Fast | Obsolete | Do not use |
| SHA-256 | 256 bits | Medium | Secure | Recommended |
| SHA-512 | 512 bits | Fast on 64-bit systems | Secure | Recommended |
Speed depends on the implementation and platform. On 64-bit processors, SHA-512 often outperforms SHA-256 thanks to optimizations for long words.
Common mistakes
Hashing without a salt
Hashing passwords with plain SHA-256 without a salt lets an attacker use rainbow tables. A single hash may match thousands of passwords in dictionaries. Always add a unique salt for each user.
Comparing hashes in constant time
When checking a password or signature, compare hashes using a constant-time comparison function. Ordinary byte-by-byte comparison with an early exit is vulnerable to timing attacks: an attacker can guess characters by measuring response times.
Using MD5 for signatures and certificates
Using MD5 in digital signatures or certificates makes it possible to create two documents with the same signature. An attacker can replace a document while preserving a valid signature. Always use SHA-256 or SHA-512.
Recommendations
- Security — use SHA-256 or SHA-512. Use MD5 and SHA-1 only for non-critical tasks.
- Passwords — use bcrypt, scrypt, or Argon2 with a salt, not plain SHA.
- Files — check integrity using SHA-256; MD5 can be added for compatibility.
- Tools — to check hashes quickly, use the hash generator on reChecker: MD5, SHA-1, SHA-256, and SHA-512 in one interface.
Hashing is a fundamental tool in cryptography and web development. The choice of algorithm depends on the task: for security, use only SHA-2; for quick checks and compatibility, MD5 is acceptable if you understand its limitations. Use the online hash generator on reChecker to check MD5, SHA-1, SHA-256, and SHA-512 without installing additional software.