Skip to content

SHA-512

Header: #include <cryptopp/sha.h> | Namespace: CryptoPP Since: Crypto++ 4.0
Thread Safety: Not thread-safe per instance; use separate instances per thread

SHA-512 is a hash function from the SHA-2 family, specified in FIPS 180-4, that produces 512-bit (64-byte) digests using 64-bit words. Its collision strength is 256 bits, against 128 bits for SHA-256.

Quick Example

#include <cryptopp/sha.h>
#include <cryptopp/hex.h>
#include <cryptopp/filters.h>
#include <iostream>

int main() {
    using namespace CryptoPP;

    SHA512 hash;
    std::string message = "Hello, World!";
    std::string digest, hexOutput;

    StringSource(message, true,
        new HashFilter(hash,
            new StringSink(digest)
        )
    );

    // Convert to hex
    StringSource(digest, true,
        new HexEncoder(new StringSink(hexOutput))
    );

    std::cout << "SHA-512: " << hexOutput << std::endl;

    return 0;
}

Usage Guidelines

Do:

  • Use SHA-512 when you need 256-bit collision resistance
  • Use with HMAC for message authentication
  • Use SHA-384 when you need a 384-bit digest; it is SHA-512 with a different initial hash value, truncated

Avoid:

  • Truncating SHA-512 output and calling it SHA-512/256; SHA-512/t uses a distinct initial hash value
  • Using for password hashing directly (we recommend Argon2id)
  • Using SHA-1 or MD5 where collision resistance matters

Class: SHA512

Cryptographic hash function producing 512-bit (64-byte) digests.

Constants

static const int DIGESTSIZE = 64;   // 64 bytes (512 bits)
static const int BLOCKSIZE = 128;   // 128 bytes (1024 bits)

Constructors

Default Constructor

SHA512();

Create SHA-512 hash object in initial state.

Example:

SHA512 hash;

Methods

Update()

void Update(const byte* input, size_t length);

Add data to the hash computation.

Parameters:

  • input - Data to hash
  • length - Length of data in bytes

Example:

SHA512 hash;
hash.Update((const byte*)"Part 1", 6);
hash.Update((const byte*)"Part 2", 6);

Final()

void Final(byte* digest);

Finalize hash and get result.

Parameters:

  • digest - Output buffer (must be 64 bytes)

Example:

SHA512 hash;
hash.Update(data, dataLen);  // const byte* data, size_t dataLen

byte digest[SHA512::DIGESTSIZE];
hash.Final(digest);

TruncatedFinal()

void TruncatedFinal(byte* digest, size_t digestSize);

Get truncated hash (first digestSize bytes).

Parameters:

  • digest - Output buffer
  • digestSize - Number of bytes to output (at most 64; a larger value throws InvalidArgument)

Note: A truncated SHA-512 digest is not SHA-512/256 or SHA-384, which start from their own initial hash values. Use SHA384 for SHA-384.

Restart()

void Restart();

Reset hash to initial state for reuse. Final() and TruncatedFinal() also restart the object.

CalculateDigest()

void CalculateDigest(byte* digest,
                     const byte* input,
                     size_t length);

One-shot hash computation.

Example:

byte digest[SHA512::DIGESTSIZE];
SHA512 hash;
hash.CalculateDigest(digest, data, dataLen);

VerifyDigest()

bool VerifyDigest(const byte* digest,
                  const byte* input,
                  size_t length);

Verify a hash digest (constant-time comparison with VerifyBufsEqual).

Returns: true if hash matches

AlgorithmProvider()

std::string AlgorithmProvider() const;

Get implementation provider.

Returns: the first match, in this order:

  • “SSE2” - SSE2 assembly
  • “NEON” or “ARMv7” - Cryptogams ARM assembly
  • “Power8” - POWER8 SHA instructions
  • “C++” - Portable implementation

Note: The library’s Intel SHA extensions (“SHANI”) and ARMv8 paths cover SHA-1 and SHA-256, not SHA-512.

Complete Example: High-Security File Hashing

#include <cryptopp/sha.h>
#include <cryptopp/files.h>
#include <cryptopp/hex.h>
#include <iostream>

using namespace CryptoPP;

std::string sha512File(const std::string& filename) {
    SHA512 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 = sha512File("important_document.pdf");
        std::cout << "SHA-512: " << hash << std::endl;
    } catch (const CryptoPP::Exception& e) {
        std::cerr << "Error: " << e.what() << std::endl;
        return 1;
    }
    return 0;
}

A digest stored next to the file detects corruption, not tampering: anyone who can change the file can change the digest. Keep the digest where the file’s writer cannot reach it, or sign it.

Security

Security Properties

  • Collision resistance: 256 bits
  • Preimage resistance: 512 bits
  • Second preimage: 512 - L(M) bits, where L(M) is the base-2 logarithm of the message length in 1024-bit blocks
  • Output size: 512 bits (64 bytes)
  • Block size: 1024 bits (128 bytes)
  • Standard: FIPS 180-4, NIST approved

Security Comparison

AlgorithmCollisionPreimageNotes
SHA-512256-bit512-bitFIPS 180-4
SHA-256128-bit256-bitFIPS 180-4
BLAKE3 (default output)128-bit256-bit intendedThe specification targets 128 bits for all goals

When to use SHA-512:

  • When a protocol specifies it; Ed25519, for example, uses SHA-512
  • When a 256-bit collision strength is required

Test Vectors

The “abc” digest is the one-block example from NIST’s SHA-512 example document.

// Test Vector: "abc"
// SHA-512: ddaf35a193617aba...

std::string message = "abc";
SHA512 hash;
hash.Update((const byte*)message.data(), message.size());

byte digest[SHA512::DIGESTSIZE];
hash.Final(digest);

byte expected[] = {
    0xdd, 0xaf, 0x35, 0xa1, 0x93, 0x61, 0x7a, 0xba,
    0xcc, 0x41, 0x73, 0x49, 0xae, 0x20, 0x41, 0x31,
    0x12, 0xe6, 0xfa, 0x4e, 0x89, 0xa9, 0x7e, 0xa2,
    0x0a, 0x9e, 0xee, 0xe6, 0x4b, 0x55, 0xd3, 0x9a,
    0x21, 0x92, 0x99, 0x2a, 0x27, 0x4f, 0xc1, 0xa8,
    0x36, 0xba, 0x3c, 0x23, 0xa3, 0xfe, 0xeb, 0xbd,
    0x45, 0x4d, 0x44, 0x23, 0x64, 0x3c, 0xe8, 0x0e,
    0x2a, 0x9a, 0xc9, 0x4f, 0xa5, 0x4c, 0xa4, 0x9f
};

assert(std::memcmp(digest, expected, 64) == 0);

SHA-512 Variants

SHA-384

SHA-384 is computed like SHA-512 with a different initial hash value, and its digest is the left-most 384 bits.

#include <cryptopp/sha.h>

SHA384 hash;  // 384-bit output
byte digest[SHA384::DIGESTSIZE];  // 48 bytes
hash.Final(digest);

Use case: 192-bit collision strength, between SHA-256 and SHA-512. The library has SHA384 and SHA224 but no SHA-512/224 or SHA-512/256 class.

Thread Safety

Not thread-safe. Use separate instances per thread.

// CORRECT - per-thread
void threadFunc() {
    SHA512 hash;  // Thread-local
    // ... use hash ...
}

When to Use SHA-512

Use SHA-512 for:

  1. 256-bit collision strength
  2. Protocols that specify it, such as Ed25519

Don’t use SHA-512 for:

  1. Bandwidth-Constrained - 64-byte output is larger
  2. Code that depends on Intel SHA extensions - the library uses them for SHA-1 and SHA-256 only

SHA-512 vs SHA-256

Choose SHA-512 when:

  • You need a 256-bit collision strength
  • A protocol requires it

Choose SHA-256 when:

  • 128-bit collision strength is enough
  • Smaller hashes are preferred
  • You want the library’s SHA extensions and ARMv8 paths

Exceptions

TruncatedFinal(), TruncatedVerify() and VerifyTruncatedDigest() throw InvalidArgument when the requested size is larger than 64 bytes.

See Also

References

  1. FIPS 180-4: Secure Hash Standard (SHS)
  2. NIST: Hash Functions
  3. RFC 8032: Edwards-Curve Digital Signature Algorithm (EdDSA)
  4. NIST: SHA-512 example with intermediate values