Skip to content

Sha512

Spice
import "std/crypto/sha512";

Sha512 struct

SHA-512 message digest, as specified in FIPS 180-4.

SHA-512 shares its construction with SHA-256, but works on 64 bit words and therefore tends to be noticeably faster than SHA-256 on 64 bit machines, while producing a 512 bit digest. Note that a plain hash is still the wrong tool for password storage, which needs a deliberately slow, salted key derivation function.

The digest is computed incrementally, so arbitrarily large messages can be fed in chunk by chunk:

spice Sha512 hasher; hasher.update("Hello, "); hasher.update("World!"); printf("%s\n", hasher.finalize().getRaw());

For one-shot use, the free sha512 functions wrap the same steps in a single call.

The length field of the padding is 128 bits wide, but this implementation only fills its lower 64 bits. Messages therefore have to be shorter than 2^61 bytes, which no addressable input hits.

Constructors

ctor

Spice
public p Sha512.ctor()

Construct a hasher that is ready to consume the first message byte

Methods

reset

Spice
public p Sha512.reset()

Discard any consumed input and return to the initial state, ready for a new message

update

Spice
public p Sha512.update(const byte[] data, unsigned long length)

Feed the given raw bytes into the digest

Parameters

Name Type Description
data const byte[] Bytes to consume
length unsigned long Number of bytes to consume

update

Spice
public p Sha512.update(string data)

Feed the chars of the given raw string into the digest, excluding the null terminator

Parameters

Name Type Description
data string String to consume

update

Spice
public p Sha512.update(const String& data)

Feed the chars of the given string into the digest

Parameters

Name Type Description
data const String& String to consume

finalizeInto

Spice
public p Sha512.finalizeInto(byte[] digest)

Complete the digest and write its 64 raw bytes to the given output array. The hasher is reset afterwards, so it can be reused for the next message.

Parameters

Name Type Description
digest byte[] Output array, that has to offer room for at least 64 bytes

finalize

Spice
public f<String> Sha512.finalize()

Complete the digest and return it as a lowercase hex string. The hasher is reset afterwards, so it can be reused for the next message.

Returns: String — Digest as 128 lowercase hex chars

getDigestSize

Spice
public inline f<unsigned long> Sha512.getDigestSize()

Retrieve the number of bytes a SHA-512 digest occupies

Returns: unsigned long — Digest size in bytes

getBlockSize

Spice
public inline f<unsigned long> Sha512.getBlockSize()

Retrieve the number of bytes SHA-512 consumes per compression step

Returns: unsigned long — Block size in bytes

Functions

sha512

Spice
public f<String> sha512(string input)

Compute the SHA-512 digest of the given raw string

Parameters

Name Type Description
input string String to hash

Returns: String — Digest as 128 lowercase hex chars

sha512

Spice
public f<String> sha512(const String& input)

Compute the SHA-512 digest of the given string

Parameters

Name Type Description
input const String& String to hash

Returns: String — Digest as 128 lowercase hex chars

sha512

Spice
public f<String> sha512(const byte[] input, unsigned long length)

Compute the SHA-512 digest of the given raw bytes

Parameters

Name Type Description
input const byte[] Bytes to hash
length unsigned long Number of bytes to hash

Returns: String — Digest as 128 lowercase hex chars