Skip to content

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 CaseStatusRecommendation
Git object namesGit names content with SHA-1 and picked SHA-256 as its successor in 2018Follow your Git object format
HMAC-SHA1NIST: acceptable for HMAC generation with keys of at least 112 bitsPrefer HMAC-SHA256
TLS signaturesTLS 1.3 lists the SHA-1 signature schemes as legacy algorithms that endpoints SHOULD NOT negotiateUse SHA-256 or stronger
Digital signature generationNIST: disallowed except where NIST protocol-specific guidance allows itUse SHA-256 or stronger
Digital signature verificationNIST: legacy useMigrate
Applications that do not need collision resistanceNIST: acceptablePrefer 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

AttackYearComplexityImpact
First collision attack on the full 80 rounds (Wang, Yin and Yu)20052^69 operations; too high to run at the timeNot implemented until 2017
First collision for full SHA-120172^63.1 compression-function callsPractical
Identical-prefix collision (improved)20202^61.2Practical
Chosen-prefix collision20202^63.4Practical

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

PropertySHA-1SHA-256BLAKE3
Collision strengthBelow 80 bits; attacks at 2^61.2128 bits128 bits
Accelerated paths in this librarySHA extensions, ARMv8, SSE2 and ARM assemblySHA extensions, ARMv8, Power8, SSE2 and ARM assemblyAVX-512, AVX2, SSE4.1
RecommendationLegacy onlyGeneral usePerformance

When to Use SHA-1

Only use SHA-1 for:

  1. Legacy Compatibility - Interfacing with old systems
  2. HMAC-SHA1 - When required by protocol (but prefer HMAC-SHA256)
  3. Git Compatibility - Repositories that use SHA-1 object names
  4. Non-Security Checksums - Where collision resistance is not needed

Never use SHA-1 for:

  1. Digital Signatures - NIST disallows SHA-1 for signature generation outside protocol-specific exceptions
  2. Certificates - Chosen-prefix collisions allow colliding certificates
  3. Password Hashing - We recommend Argon2id
  4. New Protocols - NIST recommends migrating to SHA-2 or SHA-3
  5. 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

  1. Stevens, Bursztein, Karpman, Albertini and Markov: The first collision for full SHA-1 (IACR ePrint 2017/190)
  2. 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)
  3. NIST SP 800-131A Revision 2: Transitioning the Use of Cryptographic Algorithms and Key Lengths
  4. Git documentation: hash-function-transition
  5. NIST: NIST Retires SHA-1 Cryptographic Algorithm (15 December 2022)
  6. FIPS 180-4: Secure Hash Standard (SHS)
  7. NIST: Hash Functions
  8. RFC 8446: The Transport Layer Security (TLS) Protocol Version 1.3
  9. NIST: SHA-1 example with intermediate values
  10. RFC 6194: Security Considerations for the SHA-0 and SHA-1 Message-Digest Algorithms
  11. NIST: Research Results on SHA-1 Collisions (2017)