1
votes

According to this RFC about WWW Authentication (used in HTTTP) https://www.rfc-editor.org/rfc/rfc2617#page-7,

For the purposes of this document, an MD5 digest of 128 bits is
represented as 32 ASCII printable characters. The bits in the 128 bit digest are converted from most significant to least significant bit,
four bits at a time to their ASCII presentation as follows. Each four bits is represented by its familiar hexadecimal notation from the
characters 0123456789abcdef. That is, binary 0000 gets represented by the character '0', 0001, by '1', and so on up to the representation
of 1111 as 'f'.

Rust's MD5 crate implements the Digest trait: https://docs.rs/digest/0.9.0/digest/trait.Digest.html which digests to a GenericArray which consists of 8 16-bit slices.

How do I convert for this hash format from RFC? Why doesn't the md-5 crate has a simple feature that displays the digest as hexadecimal values?

The crate literal_hex does the opposite: converts from String of hexadecimal concatenated values to bytes.

1
Judging from the docs on Digest you can convert to a hex string easily using format!("{:x}", md5::Md5::digest(b"Hello world")); - does that not work for you? - Michael Anderson
Does this answer your question? How to print sha256 hash in Rust? (GenericArray) - kmdreko

1 Answers

2
votes

How do I convert for this hash format from RFC?

Format it using the x (LowerHex) format specifier:

fn main() {
    let s = md5::Md5::new();
    println!("{:x}", s.finalize());
    // d41d8cd98f00b204e9800998ecf8427e
    println!("{:x}", md5::Md5::digest(b""));
    // d41d8cd98f00b204e9800998ecf8427e
}

as the example for digest::Digest::digest shows.

Why doesn't the md-5 crate has a simple feature that displays the digest as hexadecimal values?

Because that's built into Rust.