Skip to content

Md5

Spice
import "std/crypto/md5";

Md5 struct

MD5 message digest, as specified in RFC 1321.

MD5 is considered cryptographically broken: practical collisions can be produced within seconds. It is provided for interoperability with existing formats and protocols (checksums, legacy file digests, ETags) only. Never use it for signatures, password storage or any other security decision - reach for std/crypto/sha256 instead.

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

spice Md5 hasher; hasher.update("Hello, "); hasher.update("World!"); printf("%s\n", hasher.finalize().getRaw()); // 65a8e27d8879283831b664bd8b7f0ad4

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

Constructors

ctor

Spice
public p Md5.ctor()

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

Methods

reset

Spice
public p Md5.reset()

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

update

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

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

finalize

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

getDigestSize

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

Retrieve the number of bytes an MD5 digest occupies

Returns: unsigned long — Digest size in bytes

getBlockSize

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

Retrieve the number of bytes MD5 consumes per compression step

Returns: unsigned long — Block size in bytes

Functions

md5

Spice
public f<String> md5(string input)

Compute the MD5 digest of the given raw string

Parameters

Name Type Description
input string String to hash

Returns: String — Digest as 32 lowercase hex chars

md5

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

Compute the MD5 digest of the given string

Parameters

Name Type Description
input const String& String to hash

Returns: String — Digest as 32 lowercase hex chars

md5

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

Compute the MD5 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 32 lowercase hex chars