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-sha1-hash.md. A documentation index is available at /llms.txt.

Avoid using the sha1 crate for security

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

Metadata

ID: rust-security/avoid-sha1-hash

Language: Rust

Severity: Warning

Category: Security

CWE: 328

Related CWEs:

Description

SHA-1 is cryptographically broken since the 2017 SHAttered attack demonstrated a practical chosen-prefix collision. It must not be used for digital signatures, certificate validation, password hashing, or integrity-critical operations.

Use the sha2 crate (Sha256, Sha512) or the sha3 crate for security-sensitive hashing.

Learn More

Non-Compliant Code Examples

use sha1;
use sha1::Digest;
use sha1::{Sha1, Digest as Sha1Digest};

fn invalid(data: &[u8]) -> [u8; 20] {
    // Type path through the crate
    let mut hasher = sha1::Sha1::new();

    // Fully qualified call
    let _ = sha1::Sha1::new();

    hasher.finalize().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 sha1 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