A cryptographic hash function takes an arbitrary amount of input data and deterministically maps it to a fixed-size bit string, known as the hash value or digest. This means no matter how large the input file or message, the resulting hash will always have the exact same length. For example, hashing a single character or a 10 GB video file with SHA-256 will always produce a 256-bit (32-byte) output.
Hashing is entirely a one-way operation by mathematical design; you cannot reverse the hash digest back into the original input. This property is crucial for applications like password storage, where the system only stores the hash of the password. When a user logs in, the system hashes the provided password and compares it to the stored hash, ensuring the actual password is never kept in plain text.
The fall of MD5 and the rise of SHA algorithms
MD5 (Message-Digest algorithm 5) produces a 128-bit hash value, typically rendered as a 32-character hexadecimal number. While it was widely used for checksums and password hashing in the past, it is now considered cryptographically broken. Researchers have demonstrated practical collision attacks where two different inputs produce the exact same MD5 hash, rendering it unsafe for cryptographic security, though it is still sometimes used for non-security checksums.
The Secure Hash Algorithm (SHA) family was developed by the NSA and published by NIST to provide stronger security. SHA-256 and SHA-512 belong to the SHA-2 family, producing 256-bit and 512-bit digests, respectively. The increased bit length exponentially increases the difficulty of brute-force and collision attacks. Finding a collision in SHA-256 would require testing on the order of 2^128 operations, a number so vast that it remains computationally infeasible with current technology.
Choosing the right hashing algorithm
When selecting a hashing algorithm, the primary considerations are security requirements and computational overhead. For digital signatures, TLS certificates, and blockchain technologies like Bitcoin, SHA-256 has become the industry standard due to its robust security profile and efficient hardware-level implementations on modern processors.
SHA-512 is often used when higher security margins are desired or on 64-bit architectures where it can actually perform faster than SHA-256 in software implementations. However, for password hashing, simply using SHA-256 or SHA-512 is insufficient because they are designed to be fast, making brute-force attacks easier. For passwords, specialized key derivation functions like bcrypt, scrypt, or Argon2—which intentionally introduce computational delay and require memory—are the correct choice.