Coverage Report

Created: 2026-08-31 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/sha1-0.10.7/src/lib.rs
Line
Count
Source
1
//! Pure Rust implementation of the [SHA-1][1] cryptographic hash algorithm
2
//! with optional hardware-specific optimizations.
3
//!
4
//! # 🚨 Warning: Cryptographically Broken! 🚨
5
//!
6
//! The SHA-1 hash function should be considered cryptographically broken and
7
//! unsuitable for further use in any security critical capacity, as it is
8
//! [practically vulnerable to chosen-prefix collisions][2].
9
//!
10
//! We provide this crate for legacy interoperability purposes only.
11
//!
12
//! # Usage
13
//!
14
//! ## One-shot API
15
//!
16
//! ```rust
17
//! use hex_literal::hex;
18
//! use sha1::{Sha1, Digest};
19
//!
20
//! let result = Sha1::digest(b"hello world");
21
//! assert_eq!(result[..], hex!("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"));
22
//! ```
23
//!
24
//! ## Incremental API
25
//!
26
//! ```rust
27
//! use hex_literal::hex;
28
//! use sha1::{Sha1, Digest};
29
//!
30
//! // create a Sha1 object
31
//! let mut hasher = Sha1::new();
32
//!
33
//! // process input message
34
//! hasher.update(b"hello world");
35
//!
36
//! // acquire hash digest in the form of GenericArray,
37
//! // which in this case is equivalent to [u8; 20]
38
//! let result = hasher.finalize();
39
//! assert_eq!(result[..], hex!("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"));
40
//! ```
41
//!
42
//! Also see [RustCrypto/hashes][3] readme.
43
//!
44
//! # Note for users of `sha1 v0.6`
45
//!
46
//! This crate has been transferred to the RustCrypto organization and uses
47
//! implementation previously published as the `sha-1` crate. The previous
48
//! zero dependencies version is now published as the [`sha1_smol`] crate.
49
//!
50
//! [1]: https://en.wikipedia.org/wiki/SHA-1
51
//! [2]: https://sha-mbles.github.io/
52
//! [3]: https://github.com/RustCrypto/hashes
53
//! [`sha1_smol`]: https://github.com/mitsuhiko/sha1-smol/
54
55
#![no_std]
56
#![cfg_attr(docsrs, feature(doc_cfg))]
57
#![doc(
58
    html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
59
    html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg"
60
)]
61
#![warn(missing_docs, rust_2018_idioms)]
62
63
pub use digest::{self, Digest};
64
65
use core::{fmt, slice::from_ref};
66
#[cfg(feature = "oid")]
67
use digest::const_oid::{AssociatedOid, ObjectIdentifier};
68
use digest::{
69
    block_buffer::Eager,
70
    core_api::{
71
        AlgorithmName, Block, BlockSizeUser, Buffer, BufferKindUser, CoreWrapper, FixedOutputCore,
72
        OutputSizeUser, Reset, UpdateCore,
73
    },
74
    typenum::{Unsigned, U20, U64},
75
    HashMarker, Output,
76
};
77
78
mod compress;
79
80
#[cfg(feature = "compress")]
81
pub use compress::compress;
82
#[cfg(not(feature = "compress"))]
83
use compress::compress;
84
85
const STATE_LEN: usize = 5;
86
87
/// Core SHA-1 hasher state.
88
#[derive(Clone)]
89
pub struct Sha1Core {
90
    h: [u32; STATE_LEN],
91
    block_len: u64,
92
}
93
94
impl HashMarker for Sha1Core {}
95
96
impl BlockSizeUser for Sha1Core {
97
    type BlockSize = U64;
98
}
99
100
impl BufferKindUser for Sha1Core {
101
    type BufferKind = Eager;
102
}
103
104
impl OutputSizeUser for Sha1Core {
105
    type OutputSize = U20;
106
}
107
108
impl UpdateCore for Sha1Core {
109
    #[inline]
110
27.5k
    fn update_blocks(&mut self, blocks: &[Block<Self>]) {
111
27.5k
        self.block_len += blocks.len() as u64;
112
27.5k
        compress(&mut self.h, blocks);
113
27.5k
    }
<sha1::Sha1Core as digest::core_api::UpdateCore>::update_blocks
Line
Count
Source
110
27.5k
    fn update_blocks(&mut self, blocks: &[Block<Self>]) {
111
27.5k
        self.block_len += blocks.len() as u64;
112
27.5k
        compress(&mut self.h, blocks);
113
27.5k
    }
Unexecuted instantiation: <sha1::Sha1Core as digest::core_api::UpdateCore>::update_blocks
114
}
115
116
impl FixedOutputCore for Sha1Core {
117
    #[inline]
118
23.6M
    fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
119
23.6M
        let bs = Self::BlockSize::U64;
120
23.6M
        let bit_len = 8 * (buffer.get_pos() as u64 + bs * self.block_len);
121
122
23.6M
        let mut h = self.h;
123
23.6M
        buffer.len64_padding_be(bit_len, |b| compress(&mut h, from_ref(b)));
<sha1::Sha1Core as digest::core_api::FixedOutputCore>::finalize_fixed_core::{closure#0}
Line
Count
Source
123
23.6M
        buffer.len64_padding_be(bit_len, |b| compress(&mut h, from_ref(b)));
Unexecuted instantiation: <sha1::Sha1Core as digest::core_api::FixedOutputCore>::finalize_fixed_core::{closure#0}
124
118M
        for (chunk, v) in out.chunks_exact_mut(4).zip(h.iter()) {
125
118M
            chunk.copy_from_slice(&v.to_be_bytes());
126
118M
        }
127
23.6M
    }
<sha1::Sha1Core as digest::core_api::FixedOutputCore>::finalize_fixed_core
Line
Count
Source
118
23.6M
    fn finalize_fixed_core(&mut self, buffer: &mut Buffer<Self>, out: &mut Output<Self>) {
119
23.6M
        let bs = Self::BlockSize::U64;
120
23.6M
        let bit_len = 8 * (buffer.get_pos() as u64 + bs * self.block_len);
121
122
23.6M
        let mut h = self.h;
123
23.6M
        buffer.len64_padding_be(bit_len, |b| compress(&mut h, from_ref(b)));
124
118M
        for (chunk, v) in out.chunks_exact_mut(4).zip(h.iter()) {
125
118M
            chunk.copy_from_slice(&v.to_be_bytes());
126
118M
        }
127
23.6M
    }
Unexecuted instantiation: <sha1::Sha1Core as digest::core_api::FixedOutputCore>::finalize_fixed_core
128
}
129
130
impl Default for Sha1Core {
131
    #[inline]
132
13.5k
    fn default() -> Self {
133
13.5k
        Self {
134
13.5k
            h: [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0],
135
13.5k
            block_len: 0,
136
13.5k
        }
137
13.5k
    }
<sha1::Sha1Core as core::default::Default>::default
Line
Count
Source
132
13.5k
    fn default() -> Self {
133
13.5k
        Self {
134
13.5k
            h: [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0],
135
13.5k
            block_len: 0,
136
13.5k
        }
137
13.5k
    }
Unexecuted instantiation: <sha1::Sha1Core as core::default::Default>::default
138
}
139
140
impl Reset for Sha1Core {
141
    #[inline]
142
0
    fn reset(&mut self) {
143
0
        *self = Default::default();
144
0
    }
145
}
146
147
impl AlgorithmName for Sha1Core {
148
0
    fn write_alg_name(f: &mut fmt::Formatter<'_>) -> fmt::Result {
149
0
        f.write_str("Sha1")
150
0
    }
151
}
152
153
impl fmt::Debug for Sha1Core {
154
0
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
155
0
        f.write_str("Sha1Core { ... }")
156
0
    }
157
}
158
159
#[cfg(feature = "oid")]
160
#[cfg_attr(docsrs, doc(cfg(feature = "oid")))]
161
impl AssociatedOid for Sha1Core {
162
    const OID: ObjectIdentifier = ObjectIdentifier::new_unwrap("1.3.14.3.2.26");
163
}
164
165
/// SHA-1 hasher state.
166
pub type Sha1 = CoreWrapper<Sha1Core>;