SHA-3
Header: #include <cryptopp/sha3.h> | Namespace: CryptoPP
Since: Crypto++ 5.6.2 as Keccak; FIPS 202 padding since Crypto++ 5.6.4
Thread Safety: Not thread-safe per instance; use separate instances per thread
SHA-3 is a hash function family standardised by NIST in FIPS 202, built from the KECCAK sponge construction rather than the SHA-2 design. FIPS 202 describes the SHA-3 hash functions as alternatives to the SHA-2 functions.
Quick Example
#include <cryptopp/sha3.h>
#include <cryptopp/hex.h>
#include <cryptopp/filters.h>
#include <iostream>
int main() {
using namespace CryptoPP;
SHA3_256 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 << "SHA3-256: " << hexOutput << std::endl;
return 0;
}Usage Guidelines
Do:
- Use SHA-3 for algorithmic diversity alongside SHA-2
- Use when a standard or protocol specifically requires SHA-3
- Use the extendable-output functions SHAKE128 and SHAKE256 (
#include <cryptopp/shake.h>) when you need variable-length output
Avoid:
- Mixing up SHA-3 with SHA-2 (different constructions)
- Using Keccak (pre-FIPS padding) when you need SHA-3 (FIPS 202)
Available Variants
The constants below come from SHA3_Final, where BLOCKSIZE is the rate, 200 - 2 x DIGESTSIZE bytes.
SHA3_256
SHA3_256 hash;
static const int DIGESTSIZE = 32; // 256 bits
static const int BLOCKSIZE = 136; // Rate: 1088 bits
SHA3_512
SHA3_512 hash;
static const int DIGESTSIZE = 64; // 512 bits
static const int BLOCKSIZE = 72; // Rate: 576 bits
SHA3_224
SHA3_224 hash;
static const int DIGESTSIZE = 28; // 224 bits
static const int BLOCKSIZE = 144; // Rate: 1152 bits
SHA3_384
SHA3_384 hash;
static const int DIGESTSIZE = 48; // 384 bits
static const int BLOCKSIZE = 104; // Rate: 832 bits
Methods
Update()
void Update(const byte* input, size_t length);Add data to the hash computation.
Final()
void Final(byte* digest);Finalize and get digest.
TruncatedFinal()
void TruncatedFinal(byte* digest, size_t size);Get truncated hash. A size larger than the digest size throws InvalidArgument.
Restart()
void Restart();Reset to initial state. Final() and TruncatedFinal() also restart 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: File Integrity with SHA3-256
#include <cryptopp/sha3.h>
#include <cryptopp/files.h>
#include <cryptopp/hex.h>
#include <iostream>
using namespace CryptoPP;
std::string sha3File(const std::string& filename) {
SHA3_256 hash;
std::string digest, hexDigest;
FileSource(filename.c_str(), true,
new HashFilter(hash,
new StringSink(digest)
)
);
StringSource(digest, true,
new HexEncoder(new StringSink(hexDigest))
);
return hexDigest;
}
int main() {
try {
std::string hash = sha3File("firmware.bin");
std::cout << "SHA3-256: " << hash << std::endl;
} catch (const CryptoPP::Exception& e) {
std::cerr << "Error: " << e.what() << std::endl;
return 1;
}
return 0;
}Performance
The SHA-3 classes have no AlgorithmProvider() override, so they report the default "C++". Each permutation call absorbs one block of the rate, which is 136 bytes for SHA3-256 and 72 bytes for SHA3-512.
Security
Security Properties
| Variant | Output | Collision | Preimage | Capacity |
|---|---|---|---|---|
| SHA3-224 | 224-bit | 112-bit | 224-bit | 448-bit |
| SHA3-256 | 256-bit | 128-bit | 256-bit | 512-bit |
| SHA3-384 | 384-bit | 192-bit | 384-bit | 768-bit |
| SHA3-512 | 512-bit | 256-bit | 512-bit | 1024-bit |
1 and table 4. Second-preimage strength equals the preimage strength for all four.
Sponge Construction
SHA-3 uses a sponge construction with:
- Rate (r): Bits absorbed per call of the permutation, 1600 - c
- Capacity (c): Twice the digest length, c = 2d
- Permutation:
KECCAK-p[1600, 24]
FIPS 202 also states that SHA-3 is designed to resist length-extension attacks.
Test Vectors
The empty-string digest is the Len = 0 case of the NIST CAVP SHA-3 byte-oriented test vectors.
// SHA3-256(""): empty string
// a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a
SHA3_256 hash;
byte digest[32];
hash.Final(digest);
byte expected[] = {
0xa7, 0xff, 0xc6, 0xf8, 0xbf, 0x1e, 0xd7, 0x66,
0x51, 0xc1, 0x47, 0x56, 0xa0, 0x61, 0xd6, 0x62,
0xf5, 0x80, 0xff, 0x4d, 0xe4, 0x3b, 0x49, 0xfa,
0x82, 0xd8, 0x0a, 0x4b, 0x80, 0xf8, 0x43, 0x4a
};
assert(std::memcmp(digest, expected, 32) == 0);SHA-3 vs SHA-2
| Feature | SHA-2 | SHA-3 |
|---|---|---|
| Design | Message processed in 512- or 1024-bit blocks in sequence | Sponge |
| Accelerated paths in this library | SHA extensions, ARMv8, Power8 (SHA-256) | None; reports “C++” |
| Standard | FIPS 180-4 | FIPS 202 |
| Use Case | General purpose | Diversity |
When to use SHA-3:
- A standard or protocol requires SHA-3
- Algorithmic diversity alongside SHA-2
When to use SHA-2/BLAKE3:
- You want the library’s accelerated code paths
- A protocol specifies SHA-2 or BLAKE3
SHA-3 vs Keccak
Important: SHA-3 (FIPS 202) and Keccak (original) are different:
// SHA-3 (FIPS 202) - Use this for standards compliance
SHA3_256 sha3; // Domain separation: 0x06
// Keccak (original) - Pre-standardization version
Keccak_256 keccak; // Domain separation: 0x01
The two differ only in the padding byte the library XORs into the final block: 0x06 for SHA-3 and 0x01 for Keccak. The 0x06 byte is the FIPS 202 padding for the SHA-3 hash functions.
Use SHA-3 unless you specifically need Keccak for compatibility with pre-FIPS implementations.
Thread Safety
Not thread-safe. Use separate instances per thread.
When to Use SHA-3
Use SHA-3 for:
- Algorithmic Diversity - A second hash family alongside SHA-2
- Compliance - FIPS 202 requirements
Don’t use SHA-3 for:
- Pre-FIPS Keccak interoperability - Use the
Keccak_*classes instead
Exceptions
TruncatedFinal(), TruncatedVerify() and VerifyTruncatedDigest() throw InvalidArgument when the requested size is larger than the digest size.
See Also
- SHA-256 - Faster SHA-2 variant
- SHA-512 - SHA-2 with higher security
- BLAKE3 - Fastest modern hash
- Hash Functions Guide - Conceptual overview