SHA-1
Header: #include <cryptopp/sha.h> | Namespace: CryptoPP
Since: Crypto++ 1.0
Thread Safety: Not thread-safe per instance; use separate instances per thread
SHA-1 is cryptographically broken and should not be used for security purposes.
SHA-1 collisions have been computed in practice since 2017, and chosen-prefix collisions since 2020. Use it only for:
- Legacy system compatibility
- Applications that do not need collision resistance
- Interoperating with Git repositories that name objects with SHA-1
For new applications, use SHA-256, SHA-3, or BLAKE3. NIST says SHA-1 should be phased out by 31 December 2030 in favour of SHA-2 and SHA-3.
SHA-1 is a hash function with a 160-bit digest, specified in FIPS 180-4. NIST rates its collision strength below 80 bits.
Quick Example
#include <cryptopp/sha.h>
#include <cryptopp/hex.h>
#include <cryptopp/filters.h>
#include <iostream>
int main() {
using namespace CryptoPP;
// WARNING: SHA-1 is deprecated for security use
SHA1 hash;
std::string message = "Hello, World!";
std::string digest, hexOutput;
StringSource(message, true,
new HashFilter(hash,
new StringSink(digest)
)
);
StringSource(digest, true,
new HexEncoder(new StringSink(hexOutput))
);
std::cout << "SHA-1: " << hexOutput << std::endl;
// Output: 0A0A9F2A6772942557AB5355D76AF442F8F65E01
return 0;
}When SHA-1 Is Still Used
Despite being broken, SHA-1 remains in use for:
| Use Case | Status | Recommendation |
|---|---|---|
| Git object names | Git names content with SHA-1 and picked SHA-256 as its successor in 2018 | Follow your Git object format |
| HMAC-SHA1 | NIST: acceptable for HMAC generation with keys of at least 112 bits | Prefer HMAC-SHA256 |
| TLS signatures | TLS 1.3 lists the SHA-1 signature schemes as legacy algorithms that endpoints SHOULD NOT negotiate | Use SHA-256 or stronger |
| Digital signature generation | NIST: disallowed except where NIST protocol-specific guidance allows it | Use SHA-256 or stronger |
| Digital signature verification | NIST: legacy use | Migrate |
| Applications that do not need collision resistance | NIST: acceptable | Prefer SHA-256 for new designs |
Class: SHA1
Constants
static const int DIGESTSIZE = 20; // 160 bits (20 bytes)
static const int BLOCKSIZE = 64; // 512 bits (64 bytes)
Methods
Update()
void Update(const byte* input, size_t length);Add data to hash computation.
Final()
void Final(byte* digest);Finalize and get 20-byte digest.
Restart()
void Restart();Reset to initial state. Final() also restarts the object.
CalculateDigest()
void CalculateDigest(byte* digest,
const byte* input,
size_t length);One-shot hashing: Update() followed by Final().
VerifyDigest()
bool VerifyDigest(const byte* digest,
const byte* input,
size_t length);Verify hash. The comparison uses VerifyBufsEqual (constant time).
Complete Example: Legacy Compatibility
#include <cryptopp/sha.h>
#include <cryptopp/hex.h>
#include <cryptopp/filters.h>
#include <iostream>
using namespace CryptoPP;
// Only use for legacy system compatibility!
std::string sha1Hash(const std::string& input) {
SHA1 hash;
std::string digest, hexDigest;
StringSource(input, true,
new HashFilter(hash,
new StringSink(digest)
)
);
StringSource(digest, true,
new HexEncoder(new StringSink(hexDigest))
);
return hexDigest;
}
int main() {
// Legacy compatibility example
std::string hash = sha1Hash("test data");
std::cout << "SHA-1 (legacy): " << hash << std::endl;
// WARNING: Do not use for:
// - Digital signatures
// - Certificate fingerprints
// - Password hashing
// - Any security-critical application
return 0;
}Security
Known Attacks
| Attack | Year | Complexity | Impact |
|---|---|---|---|
| First collision attack on the full 80 rounds (Wang, Yin and Yu) | 2005 | 2^69 operations; too high to run at the time | Not implemented until 2017 |
| First collision for full SHA-1 | 2017 | 2^63.1 compression-function calls | Practical |
| Identical-prefix collision (improved) | 2020 | 2^61.2 | Practical |
| Chosen-prefix collision | 2020 | 2^63.4 | Practical |
First Practical Collision (2017)
Stevens and co-authors produced the first SHA-1 collision: two distinct PDF documents with the same SHA-1 hash that display different contents. The computation was equivalent to 2^63.1 calls to the compression function and took about 6,500 CPU years and 100 GPU years.
In 2020 Leurent and Peyrin estimated the cost of a collision at about 11,000 US dollars, and of a chosen-prefix collision at about 45,000 US dollars, using rented GPUs.
What’s Still Safe
- HMAC-SHA1: NIST lists HMAC generation with any approved hash, including SHA-1, as acceptable for keys of at least 112 bits. We recommend HMAC-SHA256 for new designs.
- Applications that do not need collision resistance: NIST lists these SHA-1 uses as acceptable.
What’s Broken
- Digital signatures: Chosen-prefix collisions can break signature schemes; NIST disallows SHA-1 for signature generation except where protocol-specific guidance allows it
- Certificates: Leurent and Peyrin built two PGP keys with different identities and colliding SHA-1 certificates
- Git: Git v2.13.0 and later use a hardened SHA-1 that is not vulnerable to the 2017 attack, but the Git project still considers SHA-1 weak
- File integrity: An attacker who can prepare both files can give them the same SHA-1 hash
Migration Guide
Replace SHA-1 with SHA-256
// OLD (insecure)
#include <cryptopp/sha.h>
SHA1 hash;
// NEW (secure)
#include <cryptopp/sha.h>
SHA256 hash;
// Both implement HashTransformation; only the class name and digest size change
Replace HMAC-SHA1 with HMAC-SHA256
// OLD
#include <cryptopp/hmac.h>
#include <cryptopp/sha.h>
HMAC<SHA1> hmac(key, keyLen);
// NEW
#include <cryptopp/hmac.h>
#include <cryptopp/sha.h>
HMAC<SHA256> hmac(key, keyLen);SHA-1 vs Modern Alternatives
| Property | SHA-1 | SHA-256 | BLAKE3 |
|---|---|---|---|
| Collision strength | Below 80 bits; attacks at 2^61.2 | 128 bits | 128 bits |
| Accelerated paths in this library | SHA extensions, ARMv8, SSE2 and ARM assembly | SHA extensions, ARMv8, Power8, SSE2 and ARM assembly | AVX-512, AVX2, SSE4.1 |
| Recommendation | Legacy only | General use | Performance |
When to Use SHA-1
Only use SHA-1 for:
- Legacy Compatibility - Interfacing with old systems
- HMAC-SHA1 - When required by protocol (but prefer HMAC-SHA256)
- Git Compatibility - Repositories that use SHA-1 object names
- Non-Security Checksums - Where collision resistance is not needed
Never use SHA-1 for:
- Digital Signatures - NIST disallows SHA-1 for signature generation outside protocol-specific exceptions
- Certificates - Chosen-prefix collisions allow colliding certificates
- Password Hashing - We recommend Argon2id
- New Protocols - NIST recommends migrating to SHA-2 or SHA-3
- File Integrity - Where an attacker may prepare the files
Test Vector
The “abc” digest is the one-block example from NIST’s SHA-1 example document.
// SHA-1("abc") = a9993e364706816aba3e25717850c26c9cd0d89d
std::string message = "abc";
SHA1 hash;
byte digest[SHA1::DIGESTSIZE];
hash.Update((const byte*)message.data(), message.size());
hash.Final(digest);
byte expected[] = {
0xa9, 0x99, 0x3e, 0x36, 0x47, 0x06, 0x81, 0x6a,
0xba, 0x3e, 0x25, 0x71, 0x78, 0x50, 0xc2, 0x6c,
0x9c, 0xd0, 0xd8, 0x9d
};
assert(std::memcmp(digest, expected, 20) == 0);Exceptions
TruncatedFinal(), TruncatedVerify() and VerifyTruncatedDigest() throw InvalidArgument when the requested size is larger than 20 bytes.
See Also
- SHA-256 - Recommended replacement
- SHA-512 - Higher security margin
- BLAKE3 - Fastest secure hash
- HMAC - Message authentication
References
- Stevens, Bursztein, Karpman, Albertini and Markov: The first collision for full SHA-1 (IACR ePrint 2017/190)
- Leurent and Peyrin: SHA-1 is a Shambles: First Chosen-Prefix Collision on SHA-1 and Application to the PGP Web of Trust (IACR ePrint 2020/014)
- NIST SP 800-131A Revision 2: Transitioning the Use of Cryptographic Algorithms and Key Lengths
- Git documentation: hash-function-transition
- NIST: NIST Retires SHA-1 Cryptographic Algorithm (15 December 2022)
- FIPS 180-4: Secure Hash Standard (SHS)
- NIST: Hash Functions
- RFC 8446: The Transport Layer Security (TLS) Protocol Version 1.3
- NIST: SHA-1 example with intermediate values
- RFC 6194: Security Considerations for the SHA-0 and SHA-1 Message-Digest Algorithms
- NIST: Research Results on SHA-1 Collisions (2017)