tc_md 0.1.1

MD2, MD4 and MD5 message digests for legacy interoperability.
Documentation
  • Coverage
  • 100%
    4 out of 4 items documented1 out of 1 items with examples
  • Size
  • Source code size: 75.3 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 321.4 kB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 2s Average build duration of successful builds.
  • all releases: 2s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • TomiCheng/tc_digest
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • TomiCheng

tc_md

crates.io docs.rs CI license rustc

The MD2 (RFC 1319), MD4 (RFC 1320) and MD5 (RFC 1321) message digests, ported from Bouncy Castle, for interoperability with legacy data and protocols. Every digest implements the tc_digest traits.

All three algorithms are cryptographically broken. Collisions are cheap for each of them, chosen-prefix collisions included for MD5, and MD2 and MD4 also fall to preimage attacks. Never use them for signatures, integrity against an attacker, password hashing or any new design; use a SHA-2 digest from tc_sha instead.

The crate is no_std, needs no allocator and contains no unsafe code. It depends on tc_digest and on tc_zeroize, which wipes the digest state.

Requires Rust 1.85 or later (edition 2024).

Types

  • Md2Digest — MD2, 16-byte digest, 16-byte blocks; variable time.
  • Md4Digest — MD4, 16-byte digest, 64-byte blocks; constant time.
  • Md5Digest — MD5, 16-byte digest, 64-byte blocks; constant time.

Each digest implements TryDigest with core::convert::Infallible as its error type, and so Digest as well. It has const fn new and Default, and implements Clone, so a digest can be forked partway through a message. Display writes the algorithm name, MD2, MD4 or MD5.

do_final writes the 16-byte digest to the start of the output buffer, leaves any longer tail untouched, returns 16, and resets the digest. It panics if the buffer is shorter than 16 bytes, before changing any state.

Timing

MD4 and MD5 use only additions, rotations and bitwise operations, so their running time depends only on the message length. MD2 indexes a 256-byte S-box with bytes derived from the message, which can leak the message through cache timing: hash only public data with Md2Digest.

Wiping

Dropping a digest, or resetting it, which do_final also does, wipes its state with volatile writes through tc_zeroize: the chaining registers, the buffered block and its fill level, and the length count, or for MD2 the state, the buffered block and the checksum. A clone is wiped when it is dropped in turn. Copies the compression leaves in registers or on the stack, such as the message words it reads, and copies a move leaves behind are not wiped, and drop-based wiping requires the destructor to run. The output buffer belongs to the caller.

Usage

[dependencies]
tc_digest = "0.1.0"
tc_md = "0.1.1"

Import Digest to reach the digest's methods:

use tc_digest::Digest;
use tc_md::Md5Digest;

let mut md5 = Md5Digest::new();
md5.update(b"message ");
md5.update(b"digest");
let mut output = [0; 16];
assert_eq!(md5.do_final(&mut output), 16);
assert_eq!(output[..4], [0xf9, 0x6b, 0x69, 0x7d]);

Validation

The tests check each digest against the complete test suites of RFC 1319, RFC 1320 and RFC 1321, and MD4 and MD5 against digests computed by OpenSSL for messages on the 64-byte padding boundaries and for one million 'a' bytes. Contract tests check, for every digest, that splitting a message across updates never changes the digest, that do_final writes exactly 16 bytes and resets, that a short output buffer panics without changing state, and that a clone continues independently. A unit test checks that the block buffer's wipe clears its buffered bytes, fill level and length count, and a contract test checks that every digest runs a destructor. Another test requires every digest type and internal helper to document whether it is constant or variable time. Missing public documentation is rejected by a crate-level lint, and unsafe code is forbidden.

Run these commands from the workspace root:

cargo test -p tc_md --locked
cargo clippy -p tc_md --all-targets --locked -- -D warnings
cargo fmt -p tc_md --check
cargo doc -p tc_md --no-deps --locked

Before a release, check the archive contents and run publication validation from a committed checkout:

cargo package -p tc_md --list --locked
cargo publish -p tc_md --dry-run --locked

The archive includes both license texts, this README, the changelog, the source and the tests. It must not include target/ or other build artifacts.

License

Licensed under either the MIT license or the Apache License, Version 2.0, at your option.