Skip to content

Sha256

Spice
import "std/crypto/sha256";

Sha256 struct

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

SHA-256 is the go-to general purpose cryptographic hash of this package: no collision or preimage attacks against it are known. 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 Sha256 hasher; hasher.update("Hello, "); hasher.update("World!"); // dffd6021bb2bd5b0af676290809ec3a53191dd81c7f70a4b28688a362182986f printf("%s\n", hasher.finalize().getRaw());

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

Constructors

ctor

Spice
public p Sha256.ctor()

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

Methods

reset

Spice
public p Sha256.reset()

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

update

Spice
public p Sha256.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 Sha256.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 Sha256.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 Sha256.finalizeInto(byte[] digest)

Complete the digest and write its 32 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 32 bytes

finalize

Spice
public f<String> Sha256.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 64 lowercase hex chars

getDigestSize

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

Retrieve the number of bytes a SHA-256 digest occupies

Returns: unsigned long — Digest size in bytes

getBlockSize

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

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

Returns: unsigned long — Block size in bytes

Functions

sha256

Spice
public f<String> sha256(string input)

Compute the SHA-256 digest of the given raw string

Parameters

Name Type Description
input string String to hash

Returns: String — Digest as 64 lowercase hex chars

sha256

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

Compute the SHA-256 digest of the given string

Parameters

Name Type Description
input const String& String to hash

Returns: String — Digest as 64 lowercase hex chars

sha256

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

Compute the SHA-256 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 64 lowercase hex chars