For AI agents: A markdown version of this page is available at https://docs.datadoghq.com/security/code_security/static_analysis/static_analysis_rules/rust-security/avoid-md5-hash.md. A documentation index is available at /llms.txt.

Avoid using the md5 crate for security

This product is not supported for your selected Datadog site. ().

Metadata

ID: rust-security/avoid-md5-hash

Language: Rust

Severity: Warning

Category: Security

CWE: 328

Related CWEs:

Description

The md5 crate computes MD5 digests. MD5 has been cryptographically broken for over a decade — practical collisions and chosen-prefix attacks are well known — and must not be used for integrity checks, password hashing, signature schemes, or any context where a cryptographic guarantee is required. For non-security checksums such as content addressing or cache keys, MD5 is sometimes acceptable; evaluate the context before suppressing. Use the sha2 crate (Sha256, Sha512) or the sha3 crate for security-sensitive hashing.

Learn More

Non-Compliant Code Examples

use md5;
use md5::Digest;
use md5::{compute, Md5};

fn invalid(data: &[u8]) -> [u8; 16] {
    // Top-level convenience function
    let digest = md5::compute(data);

    // Type path through the crate
    let _ = md5::Md5::new();

    digest.into()
}

Compliant Code Examples

use sha2::{Digest, Sha256};

fn ok(data: &[u8]) -> Vec<u8> {
    // SHA-256 — appropriate for security-sensitive hashing
    let mut hasher = Sha256::new();
    hasher.update(data);
    hasher.finalize().to_vec()
}

// No md5 usage anywhere in this file
fn unrelated(x: u32) -> u32 {
    x.wrapping_mul(31).wrapping_add(7)
}
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Security