actix_hash/
lib.rs

1//! Hashing utilities for Actix Web.
2//!
3//! # Crate Features
4//! All features are enabled by default.
5//! - `blake2`: Blake2 types
6//! - `blake3`: Blake3 types
7//! - `md5`: MD5 types 🚩
8//! - `md4`: MD4 types 🚩
9//! - `sha1`: SHA-1 types 🚩
10//! - `sha2`: SHA-2 types
11//! - `sha3`: SHA-3 types
12//!
13//! # Security Warning 🚩
14//! The `md4`, `md5`, and `sha1` types are included for completeness and interoperability but they
15//! are considered cryptographically broken by modern standards. For security critical use cases,
16//! you should move to using the other algorithms.
17
18#![forbid(unsafe_code)]
19#![deny(rust_2018_idioms, nonstandard_style)]
20#![warn(future_incompatible, missing_docs)]
21#![cfg_attr(docsrs, feature(doc_auto_cfg))]
22
23mod body_hash;
24
25pub use self::body_hash::{BodyHash, BodyHashParts};
26
27macro_rules! body_hash_alias {
28    ($name:ident, $digest:path, $feature:literal, $desc:literal, $out_size:literal) => {
29        #[doc = concat!("Wraps an extractor and calculates a `", $desc, "` body checksum hash alongside.")]
30        /// # Example
31        ///
32        /// ```
33        #[doc = concat!("use actix_hash::", stringify!($name), ";")]
34        ///
35        #[doc = concat!("async fn handler(body: ", stringify!($name), "<String>) -> String {")]
36        #[doc = concat!("    assert_eq!(body.hash().len(), ", $out_size, ");")]
37        ///     body.into_parts().inner
38        /// }
39        /// #
40        /// # // test that the documented hash size is correct
41        #[doc = concat!("# type Hasher = ", stringify!($digest), ";")]
42        #[doc = concat!("# const OutSize: usize = ", $out_size, ";")]
43        /// # assert_eq!(
44        /// #     digest::generic_array::GenericArray::<u8,
45        /// #         <Hasher as digest::OutputSizeUser>::OutputSize
46        /// #     >::default().len(),
47        /// #     OutSize
48        /// # );
49        /// ```
50        #[cfg(feature = $feature)]
51        pub type $name<T> = BodyHash<T, $digest>;
52    };
53}
54
55// Obsolete
56body_hash_alias!(BodyMd4, md4::Md4, "md4", "MD4", 16);
57body_hash_alias!(BodyMd5, md5::Md5, "md5", "MD5", 16);
58body_hash_alias!(BodySha1, sha1::Sha1, "sha1", "SHA-1", 20);
59
60// SHA-2
61body_hash_alias!(BodySha224, sha2::Sha224, "sha2", "SHA-224", 28);
62body_hash_alias!(BodySha256, sha2::Sha256, "sha2", "SHA-256", 32);
63body_hash_alias!(BodySha384, sha2::Sha384, "sha2", "SHA-384", 48);
64body_hash_alias!(BodySha512, sha2::Sha512, "sha2", "SHA-512", 64);
65
66// SHA-3
67body_hash_alias!(BodySha3_224, sha3::Sha3_224, "sha3", "SHA-3-224", 28);
68body_hash_alias!(BodySha3_256, sha3::Sha3_256, "sha3", "SHA-3-256", 32);
69body_hash_alias!(BodySha3_384, sha3::Sha3_384, "sha3", "SHA-3-384", 48);
70body_hash_alias!(BodySha3_512, sha3::Sha3_512, "sha3", "SHA-3-512", 64);
71
72// Blake2
73body_hash_alias!(BodyBlake2b, blake2::Blake2b512, "blake2", "Blake2b", 64);
74body_hash_alias!(BodyBlake2s, blake2::Blake2s256, "blake2", "Blake2s", 32);
75
76// Blake3
77body_hash_alias!(BodyBlake3, blake3::Hasher, "blake3", "Blake3", 32);