Inverse of hex_literal? i.e. how to get a hex digest of MD5

hex_literal (https://docs.rs/hex-literal/0.3.1/hex_literal/) converts from String to concatenated hex numbers.

According to this RFC about WWW Authentication (used in HTTTP) RFC 2617 - HTTP Authentication: Basic and Digest Access Authentication,

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: digest::Digest - Rust 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.

Note that md-5 is not an official rust-lang crate, it's developed as part of the independent RustCrypto project.

As for your problem, you can use one of the numerous crates for hex encoding available on crates.io, hex being the most obvious option. You also can use hex-formatting on GenericArray (the type which is currently used for hashing results by md-5), i.e. format!("{:x}", Md5::digest(b"foo")) will work as well. Although I would recommend to use a hex encoding crate nevertheless, since one day we will migrate to const generics and format!({:x}, hash) will not work (IIRC it's quite unlikely that hex-formatting support will be added for byte arrays).

It's about separation of concerns. In our opinion hashing crate should not be responsible for data encoding and instead users should use specialized crates for it.

Supports converting both ways. Even Cargo itself uses it.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.