]>
git.proxmox.com Git - rustc.git/blob - vendor/sha-1/src/lib.rs
1 //! Pure Rust implementation of the [SHA-1][1] cryptographic hash algorithm
2 //! with optional hardware-specific optimizations.
4 //! # 🚨 Warning: Cryptographically Broken! 🚨
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].
10 //! We provide this crate for legacy interoperability purposes only.
15 //! use hex_literal::hex;
16 //! use sha1::{Sha1, Digest};
18 //! // create a Sha1 object
19 //! let mut hasher = Sha1::new();
21 //! // process input message
22 //! hasher.update(b"hello world");
24 //! // acquire hash digest in the form of GenericArray,
25 //! // which in this case is equivalent to [u8; 20]
26 //! let result = hasher.finalize();
27 //! assert_eq!(result[..], hex!("2aae6c35c94fcfb415dbe95f408b9ce91ee846ed"));
30 //! Also see [RustCrypto/hashes][3] readme.
32 //! [1]: https://en.wikipedia.org/wiki/SHA-1
33 //! [2]: https://sha-mbles.github.io/
34 //! [3]: https://github.com/RustCrypto/hashes
37 #![cfg_attr(docsrs, feature(doc_cfg))]
39 html_logo_url
= "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
40 html_favicon_url
= "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg",
41 html_root_url
= "https://docs.rs/sha-1/0.10.0"
43 #![warn(missing_docs, rust_2018_idioms)]
45 pub use digest
::{self, Digest}
;
47 use core
::{fmt, slice::from_ref}
;
51 AlgorithmName
, Block
, BlockSizeUser
, Buffer
, BufferKindUser
, CoreWrapper
, FixedOutputCore
,
52 OutputSizeUser
, Reset
, UpdateCore
,
54 generic_array
::typenum
::{Unsigned, U20, U64}
,
60 #[cfg(feature = "compress")]
61 pub use compress
::compress
;
62 #[cfg(not(feature = "compress"))]
63 use compress
::compress
;
65 const STATE_LEN
: usize = 5;
67 /// Core SHA-1 hasher state.
74 impl HashMarker
for Sha1Core {}
76 impl BlockSizeUser
for Sha1Core
{
80 impl BufferKindUser
for Sha1Core
{
81 type BufferKind
= Eager
;
84 impl OutputSizeUser
for Sha1Core
{
85 type OutputSize
= U20
;
88 impl UpdateCore
for Sha1Core
{
90 fn update_blocks(&mut self, blocks
: &[Block
<Self>]) {
91 self.block_len
+= blocks
.len() as u64;
92 compress(&mut self.h
, blocks
);
96 impl FixedOutputCore
for Sha1Core
{
98 fn finalize_fixed_core(&mut self, buffer
: &mut Buffer
<Self>, out
: &mut Output
<Self>) {
99 let bs
= Self::BlockSize
::U64
;
100 let bit_len
= 8 * (buffer
.get_pos() as u64 + bs
* self.block_len
);
103 buffer
.len64_padding_be(bit_len
, |b
| compress(&mut h
, from_ref(b
)));
104 for (chunk
, v
) in out
.chunks_exact_mut(4).zip(h
.iter()) {
105 chunk
.copy_from_slice(&v
.to_be_bytes());
110 impl Default
for Sha1Core
{
112 fn default() -> Self {
114 h
: [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0],
120 impl Reset
for Sha1Core
{
122 fn reset(&mut self) {
123 *self = Default
::default();
127 impl AlgorithmName
for Sha1Core
{
128 fn write_alg_name(f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
133 impl fmt
::Debug
for Sha1Core
{
134 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
135 f
.write_str("Sha1Core { ... }")
139 /// SHA-1 hasher state.
140 pub type Sha1
= CoreWrapper
<Sha1Core
>;