Module crypto/md5

crypto/md5
Stability: unstable, with a concrete structural blocker: the one-shot `md5` is a SECOND, open-coded implementation of the algorithm, where `sha1`, `sha256` and `sha512`'s one-shots all delegate to their streaming type. Two code paths compute MD5 in this file and they agree only because `tests/crypto/digest.test.yo` asserts that they do. Collapsing `md5` onto `Md5.new().update(data).finish()` is the obvious fix and it changes nothing observable, so it should happen before anything here is frozen. Beyond that, the same open question as `std/crypto/sha1`: MD5 is broken and the API gives a new caller no signal that it is the legacy option. — stable modules only change additively; this one may still change.

MD5 hash function (RFC 1321), pure Yo implementation. MD5 is considered cryptographically broken — use SHA-256 for security.

Example

{ md5_hex } :: import("std/crypto/md5");

digest := md5_hex(data);  // "5d41402abc4b2a76b9719d911017c592"

Stability

unstable, with a concrete structural blocker: the one-shot md5 is a SECOND, open-coded implementation of the algorithm, where sha1, sha256 and sha512's one-shots all delegate to their streaming type. Two code paths compute MD5 in this file and they agree only because tests/crypto/digest.test.yo asserts that they do. Collapsing md5 onto Md5.new().update(data).finish() is the obvious fix and it changes nothing observable, so it should happen before anything here is frozen. Beyond that, the same open question as std/crypto/sha1: MD5 is broken and the API gives a new caller no signal that it is the legacy option.

Types

Md5 object
Md5

MD5 streaming state (the Sha256 skeleton; little-endian length + little-endian digest extraction).

Fields

NameTypeDescription
_hArray(u32, 4)
_bufArray(u8, 64)
_buflenusize
_totalu64

Trait Implementations

impl(Md5, ...)
new : (Md5) fn() -> Md5

A hasher seeded with the RFC 1321 initial state.

Returns: Md5

update : (Md5) fn(self : Md5, data : ArrayList(u8)) -> Md5

Absorb data and return the same hasher, so calls chain. O(len). Buffers a partial 64-byte block, so how the input is split cannot change the digest.

Parameters

NameTypeNotes
selfMd5
dataArrayList(u8)

Returns: Md5

finish : (Md5) fn(self : Md5) -> Array(u8, 16)

Append the padding and the little-endian bit length, fold the last block in, and return the 16-byte digest.

Single use — the padding is written into the hasher's own buffer, so a second finish digests the padded state rather than the message.

Parameters

NameTypeNotes
selfMd5

Returns: Array(u8, 16)

impl(Md5, Digest(...))
new : (Md5) fn() -> Md5

A hasher seeded with the RFC 1321 initial state.

Returns: Md5

update : (Md5) fn(self : Md5, data : ArrayList(u8)) -> Md5

Absorb data and return the same hasher, so calls chain. O(len). Buffers a partial 64-byte block, so how the input is split cannot change the digest.

Parameters

NameTypeNotes
selfMd5
dataArrayList(u8)

Returns: Md5

digest_size : (Md5) fn() -> usize

The digest length in bytes (32/64/20/16).

Returns: usize

block_size : (Md5) fn() -> usize

The compression block size in bytes (64, or 128 for SHA-512) — what HMAC pads keys to.

Returns: usize

finish_bytes : (Md5) fn(self : Md5) -> ArrayList(u8)

Finalise and return the digest as bytes.

Parameters

NameTypeNotes
selfMd5

Returns: ArrayList(u8)

Methods
finish_hex : (Md5) fn(self : Md5) -> String

Finalise and return the lowercase hex digest.

Parameters

NameTypeNotes
selfMd5

Returns: String

Functions

md5 function
fn(data : ArrayList(u8)) -> Array(u8, usize(16))

MD5 of data in one call — the 16-byte digest.

Broken. Collisions are minutes of laptop time (RFC 6151), so this answers "did this file change by accident" and never "is this the file I was promised". Use sha256 for anything an attacker can influence. It stays for legacy interop: S3 ETags, old package manifests, HMAC-MD5 in pre-2010 protocols.

Computed by an open-coded loop rather than by driving Md5 — the two paths in this file agree, and tests/crypto/digest.test.yo is what keeps them agreeing.

Parameters

NameTypeNotes
dataArrayList(u8)

Returns: Array(u8, usize(16))

md5_hex function
fn(data : ArrayList(u8)) -> String

MD5 of data as 32 lowercase hex characters — the form checksum files publish. Same "broken, legacy interop only" caveat as md5.

Parameters

NameTypeNotes
dataArrayList(u8)

Returns: String