---
title: Avoid using the sha1 crate for security
description: Datadog, the leading service for cloud-scale monitoring.
breadcrumbs: >-
  Docs > Datadog Security > Code Security > Static Code Analysis (SAST) > SAST
  Rules > Avoid using the sha1 crate for security
---

# Avoid using the sha1 crate for security

{% callout %}
# Important note for users on the following Datadog sites: app.ddog-gov.com, us2.ddog-gov.com

{% alert level="danger" %}
This product is not supported for your selected [Datadog site](https://docs.datadoghq.com/getting_started/site.md). ({% placeholder "user-datadog-site-name" /%}).
{% /alert %}

{% /callout %}

## Metadata{% #metadata %}

**ID:** `rust-security/avoid-sha1-hash`

**Language:** Rust

**Severity:** Warning

**Category:** Security

**CWE**: [328](https://cwe.mitre.org/data/definitions/328.html)

**Related CWEs**:

- [916](https://cwe.mitre.org/data/definitions/916.html)

## Description{% #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{% #learn-more %}

- [CWE-328: Use of Weak Hash](https://cwe.mitre.org/data/definitions/328.html)

## Non-Compliant Code Examples{% #non-compliant-code-examples %}

```rust
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{% #compliant-code-examples %}

```rust
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)
}
```
  Seamless integrations. Try Datadog Code SecurityDatadog Code Security 
{% icon name="icon-external-link" /%}
 