Hashing vs encryption
Хеширование или шифрование
Hashing creates a compact fingerprint that does not let you reconstruct the original data. Encryption hides the data while keeping it recoverable with the right key.
Start with what the application needs to do
Does the application need to read the original value later, or does it only need to check a value it already has? Encrypt a backup because you need to restore it. Hash a download if you want to compare it with a known good copy without storing that copy. To verify a password, use a dedicated password-hashing algorithm: the user supplies the password, so the application has no reason to recover it from storage.
What each operation guarantees
A cryptographic hash function turns input into a fixed-length digest. SHA-256 produces 256 bits whether the input is one byte or a large file. The same bytes always produce the same digest. Different inputs can share a digest; that's a collision. A secure hash function makes collisions hard to find and makes it hard to find an input for a given digest. That does not make a predictable input safe: someone can still guess it, hash the guess, and compare the result.
Encryption turns plaintext into ciphertext. Symmetric encryption uses the same secret key to encrypt and decrypt. With public-key encryption, anyone with the recipient's public key can encrypt a message, but decryption requires the corresponding private key. Encryption provides confidentiality; to also detect tampering, use an authenticated encryption scheme such as AES-GCM. A secure scheme also depends on key handling: storing an unprotected key alongside the data it encrypts defeats the point if both are exposed together.
Two practical examples
- Checking a download. Compute the file's SHA-256 digest and compare it with the expected digest obtained from a trusted source. A match is useful only if you trust that reference. If someone can replace both the file and the published digest, they can make the comparison pass. A digital signature can authenticate the file when you trust the signer's public key.
- Verifying a password. Store an Argon2id result along with its parameters and a unique random salt. On login, verify the candidate using those stored parameters and salt. The salt is not secret: it prevents identical passwords from producing identical stored results across accounts and makes precomputed hashes impractical to reuse. Argon2id makes each guess costly through its time and memory requirements. Adding a salt to plain SHA-256 does not make SHA-256 slow enough for password storage.
How it comes up in interviews
A common question is: “Why can a service reset your password but not send you the old one?” With password hashing, the service can verify a candidate but has no stored plaintext to retrieve or ciphertext to decrypt. Another common distinction is encoding versus encryption: Base64 changes how bytes are represented, not who can read them. Anyone can decode it without a key.