silk.crypto.md5.

Compute MD5 digests for legacy formats, including UUID version 3.

When to use#

Use Md5 only for a format that requires MD5. Use SHA-256 for new cryptographic protocols.

Details#

The streaming state does not allocate. Call make, update, and finish, or use hash.

Gotchas#

MD5 permits deliberate collisions. It does not protect data against tampering.

Examples#

Hash a message in parts#

silk
import silk.crypto.md5 { Md5 }

pub fn main() -> i32 {
  let mut state = Md5.make()
  state.update(b"a")
  state.update(b"bc")
  let digest = state.finish()
  if digest[0] != 0x90 || digest[15] != 0x72 {
    return 1
  }
  return 0
}

See also#

RFC 1321 defines the MD5 algorithm.

Import as Md5 with import silk.crypto.md5 { Md5 }.

Public declarations: 1.

Md5#

silk
pub struct Md5

A streaming MD5 state. It does not allocate and is unsuitable for security checks.

Implementation Md5#

silk
impl Md5

Associated function make#

silk
pub fn make() -> Self

Returns a state for an empty message.

Method update#

silk
pub fn update(self: &mut Self, bytes: &[u8]) -> ()

Adds bytes to the message. The length in bits wraps modulo 2^64, as MD5 requires.

Method finish#

silk
pub fn finish(self: Self) -> [u8; 16]

Consumes the state and returns its 16-byte digest in MD5 byte order.

Associated function hash#

silk
pub fn hash(bytes: &[u8]) -> [u8; 16]

Returns the 16-byte MD5 digest of bytes. It does not allocate.

Implementation Digest for Md5#

silk
impl Digest for Md5

Operation make#

silk
make: Md5.make

Returns a MD5 state for an empty message.

Operation update#

silk
update: Md5.update

Adds bytes to the end of the message.

Operation blockLength#

silk
fn blockLength(self: &Self) -> usize

Returns 64, the MD5 block length in bytes.

Operation digestLength#

silk
fn digestLength(self: &Self) -> usize

Returns 16, the MD5 digest length in bytes.

Operation objectIdentifier#

silk
fn objectIdentifier(self: &Self) -> &[u8]

Returns the content bytes of the MD5 object identifier, 1.2.840.113549.2.5.

Operation copy#

silk
fn copy(self: &Self) -> Self

Returns an independent copy of the state.

Operation finishInto#

silk
fn finishInto(self: Self, output: &mut [u8]) -> ()

Writes the 16-byte MD5 digest to the start of output and consumes the state.

Category: Cryptography.

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page