- Introduced by
- National Institute of Standards and Technology (NIST)
- Year
- 1993
- Type
- One-way hash algorithm
- Family
- Cryptographic hash function
What does it do?
The Secure Hash Algorithm (SHA) is a family of cryptographic hash functions standardized by NIST starting in 1993.
SHA functions produce fixed-length digests from arbitrary-length input data, serving as a fundamental building block for digital signatures, message authentication, and data integrity verification across security-critical applications.
The intuition behind the method
A hash function condenses an arbitrary input into a fixed-size digest, so that even a small change in the input produces a very different output. SHA functions are designed to be one-way: given a digest, recovering the original input should be computationally infeasible. The family has three groups in common use: SHA-1 (160-bit output), the SHA-2 variants SHA-256, SHA-384 and SHA-512, and SHA-3, which uses a different internal design. SHA-1 and SHA-2 share the iterated Merkle–Damgård structure, which uses bitwise operations, rotations and modular addition in repeated compression rounds. The intended properties are collision resistance (it should be infeasible to find two messages with the same digest) and unpredictability of the digest without knowledge of the input.
History
NIST's Secure Hash Standard, published as FIPS 180, is the source for the SHA family. The revision linked from this page's references is FIPS 180-4. Wang et al. (2005) reported collision-finding methods for SHA-1 that prompted the move away from it for signatures. The SHA-2 family (SHA-256, SHA-384, SHA-512) was later standardized as a stronger successor. NIST subsequently selected the Keccak algorithm through an open competition and standardized it as SHA-3, which uses a sponge construction rather than Merkle–Damgård chaining.
When to use it
Choose SHA-256 or SHA-512 (SHA-2 family) for new designs that need a standard hash for digital signatures, certificates or integrity checks. Do not use SHA-1 for new signatures or certificates, since its collision resistance has been broken; existing SHA-1 uses should be migrated. SHA-3 is an option when a design deliberately avoids the Merkle–Damgård structure of SHA-2, for example to hedge against a weakness in that structure. Do not use a plain hash to store passwords; use a salted key derivation function instead. Do not use a bare hash as proof of authorship, because anyone can compute the hash of a given message, so authentication requires a keyed construction such as a MAC or a signature.
Limitations and considerations
- SHA-1 has been cryptographically broken: Wang et al. (2005) showed collision-finding methods cheaper than brute force, and the SHAttered attack later produced a practical full collision
- For Merkle–Damgård hashes such as SHA-1 and SHA-2, length extension applies to constructions of the form H(secret || message): an attacker who knows the digest and the lengths can compute a valid digest for an extended message without knowing the secret. Keyed hashing of this kind must use HMAC or SHA-3. A plain digest of a message with no secret component is not affected in the same way, but it still provides no authentication (see below)
- A digest alone provides no authentication; a keyed MAC or a signature is needed to tie the digest to a sender
- Fast by design, so unsuitable for password storage without a deliberately slow key derivation function such as bcrypt, scrypt or Argon2
Source conditions do not replace independent scientific review or validation by an analysis engine.
Strengths
- Fixed-length output for any input length (160, 256 or 512 bits depending on the variant), so storage and signature formats can be sized in advance
- Merkle–Damgård chaining means each block's output feeds the next, so a change anywhere in the message changes the final digest
- The SHA-2 variants have been studied extensively without a practical attack on their full design, which is why they are the standard choice for new systems
- Several variants share one interface, so a system can move from SHA-256 to SHA-512 by changing a parameter, without changing the surrounding protocol
Applications
- Digital signatures: Signing documents, certificates, and software using RSA or ECDSA
- Blockchain: Computing block hashes and Merkle trees in cryptocurrency systems
- Version control: Git uses SHA-1 (being migrated to SHA-256) for commit and object identification
- File integrity: Verifying downloads and detecting tampering in stored or transmitted files
- Password authentication: Deriving keys from passwords in conjunction with salt and stretching functions
How it works
How it works
Pad the message so that its length is a multiple of the block size (512 bits for SHA-1 and SHA-256, 1024 bits for SHA-512), and append the original message length as a binary field so that messages of different lengths are padded differently.
Load the starting hash state. In SHA-256 these constants are derived from fractional parts of square roots of small primes; SHA-1 uses a different set of constants, so this description should not be read as applying to every variant. Initialise the working variables used by the compression function.
Process each padded block through a fixed number of rounds (64 to 80, depending on the variant). Each round combines logical functions, bitwise rotations and modular addition to update the working registers, which together form the intermediate hash state carried into the next block.
Combine the result of the last block with the running state and concatenate the final register values to form the digest: 160 bits for SHA-1, 256 bits for SHA-256 and 512 bits for SHA-512.
Common pitfalls
- Accepting SHA-1 signatures or certificates in new or continuing systems; migrate to SHA-256 or another SHA-2 variant
- Hashing passwords with SHA directly or with an unsalted hash; use a salted key derivation function
- Treating a plain SHA digest of a message as proof of who wrote it; anyone can recompute it, so a keyed MAC or signature is required
- Truncating SHA digests to save space without accounting for the reduced collision and preimage resistance of the shorter output
Frequently asked questions
Why was SHA-1 deprecated if collisions were only found recently?
SHA-1 collisions became practical in 2017 (the 'SHAttered' attack). Well before that, Wang et al. (2005) showed that collisions in the full SHA-1 could be found by methods cheaper than brute force, which motivated moving away from SHA-1 for signatures. Discontinuing an algorithm before it is fully broken follows the principle of defence in depth.
Is SHA-256 safe for the foreseeable future?
No practical attack on SHA-256 is known. Cryptography evolves, however. Against preimage search, a quantum computer running Grover's algorithm would reduce the effective security of a hash function, which is why long-lived signatures may warrant SHA-3 or post-quantum alternatives. This is a statement about preimage search; collision resistance is analysed separately.
Can hash functions be used directly to hash passwords?
No. Hash functions are fast by design, which makes them vulnerable to brute-force password guessing. Use a password-specific key derivation function (KDF) such as bcrypt, scrypt or Argon2, which is deliberately slow, takes a salt, and derives a key at a controlled computational cost.
What is the difference between SHA-2 and SHA-3?
SHA-2 uses Merkle–Damgård chaining with an iterated compression function. SHA-3 uses the Keccak sponge construction, which is structurally different, so a weakness of the chaining structure, such as length extension, does not carry over directly. Both are considered secure; SHA-3 is a reasonable choice where a design wants a second, structurally different hash.
Related methods
Sources
National Institute of Standards and Technology (1993). Secure Hash Standard (SHS). Federal Information Processing Standards (FIPS) Publication 180.
Open publication referenceWang, X., Yin, Y. L., & Yu, H. (2005). Finding collisions in the full SHA-1. Proceedings of CRYPTO 2005, Lecture Notes in Computer Science, 3621, 17–36.
Open DOI referenceStevens, M., Bursztein, E., Karpman, P., Albertini, A., & Markov, Y. (2013). The first collision for full SHA-1. Advances in Cryptology – CRYPTO 2017, 570–596.
Open publication reference