Hashing vs encryption: what is the difference?
Key takeaways
- Encryption lets authorized users recover the original data with a key, while hashing produces a one-way digest for verification such as file comparisons.
- Store login passwords with a salted, purpose-built scheme such as Argon2id rather than fast hashes like SHA-256, and benchmark its memory and time settings.
- A plain hash cannot authenticate a message if an attacker can change both message and digest, so use HMAC or digital signatures instead.
- Base64 is encoding, not encryption, and plain hashes of predictable values like email addresses can be matched by guessing, so neither gives confidentiality or anonymity.
Hashing creates a one-way digest for checking data, while encryption protects information so authorized users can recover it with the right key. They solve different data security problems: hashing supports verification, and encryption provides confidentiality. Understanding where each belongs helps you protect passwords, files, messages, and customer records without relying on the wrong safeguard. (csrc.nist.gov)
What is the difference between hashing and encryption?
The main difference is reversibility. Encryption transforms readable information, called plaintext, into ciphertext that can be decrypted using the appropriate key. Cryptographic hashing transforms an input into a digest without providing a decryption operation. You use encryption when you need the original information back and hashing when you need a value for comparison or verification. (csrc.nist.gov)
Think about a customer account. A business may need to retrieve a delivery address, so encryption can protect that address while preserving authorized access. It usually needs only to verify a login password, not retrieve it, so a purpose-built password-hashing function is the better fit. Neither technique replaces the other. (cheatsheetseries.owasp.org)
| Feature | Cryptographic hashing | Encryption |
|---|---|---|
| Main purpose | Support verification and integrity checks | Protect confidentiality |
| Recover the original input | No decryption operation | Yes, with the appropriate key |
| Secret key | Ordinary hashes need none; keyed constructions exist | Required within the encryption scheme |
| Output | Digest; fixed length for algorithms such as SHA-256 | Ciphertext, typically accompanied by supporting metadata |
| Common uses | File comparisons, password verification, signature workflows | Protected records, backups, and communications |
These differences explain why hashing and encryption are complementary tools rather than competing levels of protection. The right choice follows the operation your application must perform. (csrc.nist.gov)
A useful starting point is to ask whether authorized users must read the original value later. If yes, investigate encryption. If not, identify exactly what must be verified before selecting a hashing approach. Password verification, file comparison, and message authentication have different requirements, even though all can involve hashes. (cheatsheetseries.owasp.org)
How cryptographic hashing works
Cryptographic hash functions accept data and produce a digest, sometimes described as a digital fingerprint. For a given function, identical input produces identical output. With SHA-256, for example, the digest is 256 bits long whether the input is a short message or a large document. (csrc.nist.gov)
That digest represents the input for verification; it is not a compressed copy waiting to be unpacked. A system can hash a file now, hash it again later, and compare the results. Different digests establish that the underlying bytes differ, which makes hashing useful for detecting changes. (csrc.nist.gov)
The security properties that matter
A cryptographic hash must do more than produce an unfamiliar-looking string. Its security depends on making several kinds of attack computationally impractical:
- Preimage resistance: Given a digest, finding an input that produces it should be infeasible.
- Second-preimage resistance: Given one input, finding a different input with the same digest should be infeasible.
- Collision resistance: Finding any two different inputs that produce the same digest should be infeasible. (csrc.nist.gov)
A collision is therefore not the same as reversing a hash. It involves two inputs sharing an output, rather than necessarily recovering a particular original input. Because fixed-length digests have a finite number of possible values, collisions must exist; the security goal is to make useful collisions impractical to find. (csrc.nist.gov)
This also explains why a digest should not be called a guaranteed unique identifier. It can be an excellent practical comparison value when the algorithm and application are appropriate. That is different from a mathematical promise that no other input could ever match it. (csrc.nist.gov)
One-way does not mean impossible to guess
An attacker does not need to reverse a hash if the original input is predictable. They can guess a value, hash it, and compare the result with the target digest. A match identifies a candidate input without breaking the hash function's underlying mathematics. (cheatsheetseries.owasp.org)
For example, imagine hashing a field containing only “approved” or “rejected.” Anyone who knows those possibilities can calculate both digests. The hash has no decryption operation, but it does little to conceal such a small set of possible inputs. Preimage resistance does not make predictable information unpredictable. (csrc.nist.gov)
How encryption protects readable information
Encryption protects information by making it unintelligible to people who lack the necessary key. Decryption restores the plaintext for authorized use. Unlike hashing, this recoverability is intentional: the purpose is to preserve access while controlling who can exercise it. (csrc.nist.gov)
Consider an encrypted project archive. Team members still need to open the documents, revise them, and restore them from backup. Hashing the documents would not meet that need because the digests could not reconstruct the files. Encryption is suitable precisely because the original content remains recoverable. (csrc.nist.gov)
Symmetric and asymmetric approaches
Symmetric encryption uses a shared secret key for encryption and decryption. AES is a standardized symmetric cipher with 128-, 192-, and 256-bit key options. The algorithm must be used within an appropriate scheme, not treated as a complete storage-security design by itself. (csrc.nist.gov)
Asymmetric cryptography uses a public/private key pair. In public-key encryption, information protected for a recipient is recoverable using the corresponding private key. Practical systems often combine public-key mechanisms for establishing secrets with symmetric encryption for protecting the actual content, rather than encrypting large files directly with a public-key operation. (csrc.nist.gov)
These categories describe how keys work, not which system is automatically safer. A well-known algorithm can still be deployed incorrectly. Prefer maintained libraries and established protocols over assembling cryptographic components yourself. (cheatsheetseries.owasp.org)
Authenticated encryption detects tampering too
Confidentiality alone does not establish that encrypted information has remained unchanged. Authenticated encryption combines confidentiality with integrity protection, allowing the recipient to reject data that fails authentication. AES-GCM is one example; ChaCha20-Poly1305 is another established construction. (csrc.nist.gov)
Supporting inputs matter. A nonce is a value used alongside a key, and constructions such as these require that it not repeat under the same key. Authentication tags must also be verified before decrypted content is accepted. Let a suitable high-level library handle these details wherever possible. (rfc-editor.org)
Authenticated encryption does not, by itself, identify a particular human sender. Its authentication guarantees depend on who controls the relevant keys and how the surrounding system is designed. Keep that distinction in mind when translating technical features into claims about information security. (rfc-editor.org)
Why should passwords be hashed rather than encrypted?
Login passwords should normally be stored using a salted, purpose-built password-hashing scheme because authentication requires verification, not recovery. Reversible encryption creates a decryption capability the application usually does not need. If an attacker obtains both encrypted passwords and the necessary key, they can recover those passwords. (cheatsheetseries.owasp.org)
A password-verification workflow looks like this:
- Create the account: Process the password with a suitable password-hashing function and a fresh random salt.
- Store the verifier: Save the resulting hash, salt, algorithm identifier, and cost settings—not the plaintext password.
- Check a login: Use the library's verification operation with the submitted password and stored verifier.
- Accept or reject: Authenticate only when verification succeeds, alongside the application's other security checks. (pages.nist.gov)
The salt is not a secret encryption key. It makes otherwise identical passwords produce different stored results and prevents attackers from efficiently reusing precomputed lookup tables across accounts. Salts are normally stored with password verifiers because verification needs them. (pages.nist.gov)
Fast file hashes are not password-storage functions
SHA-256 is designed for efficient hashing, which is useful when processing files but undesirable when an attacker is testing password guesses. Password-hashing schemes deliberately impose computational cost. Memory-hard approaches also demand memory, increasing the resources needed for large-scale guessing. (cheatsheetseries.owasp.org)
Argon2id is a purpose-built option for password hashing. Its memory, time, and parallelism settings affect resource use, so configuration matters as much as the algorithm name. Use established guidance and benchmark the settings under realistic login loads rather than copying unexplained parameters. (rfc-editor.org)
The aim is not to make login unbearably slow. It is to make each offline guess expensive enough to impede attackers while keeping authentication practical for legitimate users. Store the settings with the verifier so the system can recognize older configurations and migrate them when appropriate. (pages.nist.gov)
A password manager has a different requirement from a website login database: it must recover saved credentials for its owner. That recovery requirement makes encryption relevant to the vault contents. The apparent exception follows the same rule—choose the mechanism according to what the application must do with the information. (csrc.nist.gov)
Choosing the right tool for everyday scenarios
Start with the required outcome, not the appearance of the stored value. Both ciphertext and digests may look unreadable, but that tells you little about their security guarantees. The following scenarios show how to translate a business need into a technical requirement. (csrc.nist.gov)
Checking a downloaded file
Suppose a software publisher provides a file and an expected SHA-256 digest. You can calculate the downloaded file's digest and compare it with the published value. A mismatch shows that the bytes differ from the version represented by that digest. (csrc.nist.gov)
The reference digest must come from a trustworthy source. If an attacker can replace both the file and the displayed digest, the comparison does not establish authenticity. A plain hash verifies consistency with a reference; it does not independently prove who published that reference. (learn.microsoft.com)
Keeping customer records readable only to authorized users
A support system may need to display protected customer details during an approved workflow. Encryption can preserve that capability while protecting stored content. The design must also specify which applications can request decryption and how access to keys is controlled. (cheatsheetseries.owasp.org)
For this scenario, write down the recovery requirement explicitly: “An authorized support process must retrieve this field.” That statement helps prevent a team from selecting hashing merely because the stored output looks concealed. It also directs attention to the people and services allowed to recover the plaintext. (csrc.nist.gov)
Authenticating messages with HMAC or digital signatures
A plain hash is not enough when an attacker could modify both a message and its digest. HMAC combines a cryptographic hash function with a shared secret key to provide message authentication. It does not encrypt the message or hide its contents. (csrc.nist.gov)
Digital signatures serve a related purpose using public-key cryptography. A signer uses a private key, while others verify using the corresponding public key. Signature workflows commonly involve hashing, but a signature is not simply another name for a hash, and signatures do not provide message confidentiality. (csrc.nist.gov)
The distinction is practical: parties sharing an HMAC key can generate valid authentication codes themselves. Public-key verification lets recipients check signatures without giving them the signing key. Choose according to who needs to verify messages and who should be capable of creating valid proofs. (csrc.nist.gov)
Hashing and encryption often work together
An application does not have to choose one technique for every security task. A login service can use an encrypted network connection to transport a password and then verify it against a stored password hash. Each layer addresses a different exposure point. (cheatsheetseries.owasp.org)
TLS 1.3 illustrates this combination. It uses authenticated encryption to protect application traffic, while hash-based mechanisms contribute to key derivation and handshake verification. Hashing participates in the protocol without replacing the encryption that conceals transmitted content. (rfc-editor.org)
The implication for data protection is straightforward: trace information through its entire journey. Protecting a database does not automatically protect the network connection used to reach it. Likewise, protecting transport does not determine how information is stored after it arrives. (cheatsheetseries.owasp.org)
A useful design exercise is to map one sensitive field from collection through processing, storage, backup, and deletion. At every stage, identify whether the system needs confidentiality, verification, or both. This keeps the discussion focused on actual exposure rather than a vague requirement to “secure data.” (cheatsheetseries.owasp.org)
Common mistakes that weaken protection
Treating encoding as encryption
Base64 and hexadecimal change how bytes are represented. They do not require a secret key to decode and should not be treated as confidentiality controls. A Base64-encoded password remains recoverable by anyone who can decode the representation. (rfc-editor.org)
Encoding can still be useful for carrying binary ciphertext or a digest through a text-only interface. The mistake is confusing the container with the protection inside it. Ask which cryptographic operation occurred before the value was encoded. (rfc-editor.org)
Assuming hashed personal information is anonymous
Replacing an email address with its plain hash does not necessarily prevent identification. An attacker who has a candidate address can hash it and test for a match. The same reasoning applies to other predictable identifiers and small sets of possible values. (csrc.nist.gov)
Treat hashing as a technical transformation, not an automatic anonymity guarantee. Before using hashed identifiers for sharing or analytics, consider what candidates another party could test. A design that supports matching may also enable unwanted matching, so evaluate the intended use and the attacker's likely knowledge. (csrc.nist.gov)
Focusing on encryption algorithms while neglecting keys
Strong encryption cannot compensate for an exposed decryption key. Key storage, access permissions, lifecycle management, and recovery procedures are part of the security design. Keeping keys in a dedicated management system can help separate them from the information they protect. (cheatsheetseries.owasp.org)
Plan for both compromise and loss. A stolen key may expose protected content, while an unavailable key can prevent legitimate recovery. Document who owns each key, what it protects, and how the organization will replace or recover it when necessary. (cheatsheetseries.owasp.org)
Leaving sensitive information in logs
A protected database offers little reassurance if debugging output contains the original secrets. Passwords, access tokens, encryption keys, and sensitive personal information should not be casually recorded in logs. Logging decisions need the same care as primary storage decisions. (cheatsheetseries.owasp.org)
Review error handlers, request logging, and troubleshooting workflows for accidental disclosure. Log security-relevant events without including the secrets involved. This preserves useful operational visibility while reducing the chance that diagnostics become an alternative source of sensitive information. (cheatsheetseries.owasp.org)
A practical checklist before implementation
Use this checklist to turn the comparison into a design review. Record the answers for each sensitive data type rather than assuming one decision covers the entire application. The exercise should make both the required capability and its limitations explicit.
- Define recovery needs. Identify whether the application must retrieve the original value or only verify a candidate. This separates encryption requirements from hashing requirements. (csrc.nist.gov)
- Name the threat. Distinguish a stolen database, an intercepted connection, and unauthorized application access. Select controls for the exposure you actually need to address. (cheatsheetseries.owasp.org)
- Choose the correct construction. Password hashing, plain file hashing, HMAC, signatures, and authenticated encryption are not interchangeable. State which security property each component provides. (learn.microsoft.com)
- Check operational requirements. Document salts and cost settings for password verifiers, and key and nonce handling for encryption. Use supported libraries rather than custom cryptographic designs. (pages.nist.gov)
- Inspect secondary copies. Include logs, exports, and backups in the review. Protecting the main database is insufficient when another workflow exposes the same plaintext. (cheatsheetseries.owasp.org)
- Plan maintenance. Assign responsibility for key lifecycle management and for reviewing cryptographic configurations. Security decisions need an owner after the initial implementation is complete. (cheatsheetseries.owasp.org)
The takeaway: verify with hashes, recover with encryption
Hashing supports verification without a decryption operation; encryption preserves authorized recovery of protected information. Use purpose-built password hashing for login verifiers, authenticated encryption for confidential content, and HMAC or signatures when message authenticity is required. Effective information security comes from matching these tools to specific needs—not from choosing whichever produces the most unreadable-looking output. (csrc.nist.gov)
Related guides
Sources
- Cryptographic hash function - Glossary | CSRC
- csrc.nist.gov: Encryption
- Password Storage - OWASP Cheat Sheet Series
- FIPS 180-4, Secure Hash Standard (SHS) | CSRC
- csrc.nist.gov: Hash functions
- Collision resistance - Glossary | CSRC
- csrc.nist.gov: Preimage resistance
- csrc.nist.gov: Final
- Cryptographic Storage - OWASP Cheat Sheet Series
- SP 800-38D, Recommendation for Block Cipher Modes of Operation: Galois/Counter Mode (GCM) and GMAC | CSRC
- RFC 8439: ChaCha20 and Poly1305 for IETF Protocols | RFC Editor
- NIST Special Publication 800-63B
- RFC 9106: Argon2 Memory-Hard Function for Password Hashing and Proof-of-Work Applications | RFC Editor
- MACs, hashes, and signatures - Windows apps | Microsoft Learn
- HMAC - Glossary | CSRC
- csrc.nist.gov: Digital signature
- Transport Layer Security - OWASP Cheat Sheet Series
- RFC 8446: The Transport Layer Security (TLS) Protocol Version 1.3 | RFC Editor
- RFC 4648: The Base16, Base32, and Base64 Data Encodings | RFC Editor
- Key Management - OWASP Cheat Sheet Series
- Logging - OWASP Cheat Sheet Series