What a hash function does
A hash function maps arbitrary-length input to a fixed-length digest. It is deterministic: identical input gives identical output, while a well-designed cryptographic hash exhibits a broad change in digest when the input changes slightly.
A digest is derived data, not compressed text that can be expanded back into the message. One-way behavior underpins many integrity, signature and content-identification applications.
Preimage, second preimage and collisions
Security properties are distinct: preimage resistance, second-preimage resistance and collision resistance. A collision occurs when two different inputs share a digest; generic collision search for an n-bit hash follows roughly the 2^(n/2) birthday bound.
Digest size and known cryptanalysis therefore matter. MD5 and SHA-1 are unsuitable where cryptographic collision resistance is required, even though legacy or non-adversarial checksum uses may still exist.
The SHA family and algorithm choice
SHA-256 and SHA-512 are SHA-2 functions and remain common choices for integrity and many protocols. SHA-3 uses a different construction and provides another standardized family with different internal properties.
Choice depends on protocol requirements, interoperability, reliable library support and security goals rather than bit count alone. Avoid custom hashes or non-standard compositions without specific analysis.
Hashes, checksums and HMAC
An unauthenticated hash only detects accidental change when the expected digest arrives through a trusted path. An attacker able to alter both data and digest can simply calculate a new hash.
HMAC combines a hash with a secret key to provide message authenticity and integrity between parties sharing that key. Traditional checksums are mainly designed for accidental corruption rather than active attackers.
Passwords: why fast hashes are wrong
Passwords have limited entropy and can be guessed offline. Fast functions such as SHA-256 enable enormous numbers of attempts and are therefore inappropriate for password storage even when a simple salt is added.
Use adaptive password hashing functions such as Argon2id, scrypt or bcrypt with unique salts and calibrated cost parameters. Salts defeat precomputed tables but do not replace computational cost.
Hashing is not encryption
Encryption is reversible with a key and aims to provide confidentiality. Hashing is not designed to recover the original message and serves different goals such as fingerprints and integrity.
Choose the primitive from the threat model: authenticated encryption for confidential data, HMAC for keyed message authentication, password hashing for credentials and cryptographic hashes for public digests.