1 | //! An implementation of the [SHA-2][1] cryptographic hash algorithms. |
2 | //! |
3 | //! There are 6 standard algorithms specified in the SHA-2 standard: [`Sha224`], |
4 | //! [`Sha256`], [`Sha512_224`], [`Sha512_256`], [`Sha384`], and [`Sha512`]. |
5 | //! |
6 | //! Algorithmically, there are only 2 core algorithms: SHA-256 and SHA-512. |
7 | //! All other algorithms are just applications of these with different initial |
8 | //! hash values, and truncated to different digest bit lengths. The first two |
9 | //! algorithms in the list are based on SHA-256, while the last four are based |
10 | //! on SHA-512. |
11 | //! |
12 | //! # Usage |
13 | //! |
14 | //! ```rust |
15 | //! use hex_literal::hex; |
16 | //! use sha2::{Sha256, Sha512, Digest}; |
17 | //! |
18 | //! // create a Sha256 object |
19 | //! let mut hasher = Sha256::new(); |
20 | //! |
21 | //! // write input message |
22 | //! hasher.update(b"hello world" ); |
23 | //! |
24 | //! // read hash digest and consume hasher |
25 | //! let result = hasher.finalize(); |
26 | //! |
27 | //! assert_eq!(result[..], hex!(" |
28 | //! b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9 |
29 | //! " )[..]); |
30 | //! |
31 | //! // same for Sha512 |
32 | //! let mut hasher = Sha512::new(); |
33 | //! hasher.update(b"hello world" ); |
34 | //! let result = hasher.finalize(); |
35 | //! |
36 | //! assert_eq!(result[..], hex!(" |
37 | //! 309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f |
38 | //! 989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f |
39 | //! " )[..]); |
40 | //! ``` |
41 | //! |
42 | //! Also see [RustCrypto/hashes][2] readme. |
43 | //! |
44 | //! [1]: https://en.wikipedia.org/wiki/SHA-2 |
45 | //! [2]: https://github.com/RustCrypto/hashes |
46 | |
47 | #![no_std ] |
48 | #![cfg_attr (docsrs, feature(doc_cfg))] |
49 | #![doc ( |
50 | html_logo_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg" , |
51 | html_favicon_url = "https://raw.githubusercontent.com/RustCrypto/media/6ee8e381/logo.svg" |
52 | )] |
53 | #![warn (missing_docs, rust_2018_idioms)] |
54 | |
55 | pub use digest::{self, Digest}; |
56 | |
57 | #[cfg (feature = "oid" )] |
58 | use digest::const_oid::{AssociatedOid, ObjectIdentifier}; |
59 | use digest::{ |
60 | consts::{U28, U32, U48, U64}, |
61 | core_api::{CoreWrapper, CtVariableCoreWrapper}, |
62 | impl_oid_carrier, |
63 | }; |
64 | |
65 | #[rustfmt::skip] |
66 | mod consts; |
67 | mod core_api; |
68 | mod sha256; |
69 | mod sha512; |
70 | |
71 | #[cfg (feature = "compress" )] |
72 | pub use sha256::compress256; |
73 | #[cfg (feature = "compress" )] |
74 | pub use sha512::compress512; |
75 | |
76 | pub use core_api::{Sha256VarCore, Sha512VarCore}; |
77 | |
78 | impl_oid_carrier!(OidSha256, "2.16.840.1.101.3.4.2.1" ); |
79 | impl_oid_carrier!(OidSha384, "2.16.840.1.101.3.4.2.2" ); |
80 | impl_oid_carrier!(OidSha512, "2.16.840.1.101.3.4.2.3" ); |
81 | impl_oid_carrier!(OidSha224, "2.16.840.1.101.3.4.2.4" ); |
82 | impl_oid_carrier!(OidSha512_224, "2.16.840.1.101.3.4.2.5" ); |
83 | impl_oid_carrier!(OidSha512_256, "2.16.840.1.101.3.4.2.6" ); |
84 | |
85 | /// SHA-224 hasher. |
86 | pub type Sha224 = CoreWrapper<CtVariableCoreWrapper<Sha256VarCore, U28, OidSha224>>; |
87 | /// SHA-256 hasher. |
88 | pub type Sha256 = CoreWrapper<CtVariableCoreWrapper<Sha256VarCore, U32, OidSha256>>; |
89 | /// SHA-512/224 hasher. |
90 | pub type Sha512_224 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U28, OidSha512_224>>; |
91 | /// SHA-512/256 hasher. |
92 | pub type Sha512_256 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U32, OidSha512_256>>; |
93 | /// SHA-384 hasher. |
94 | pub type Sha384 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U48, OidSha384>>; |
95 | /// SHA-512 hasher. |
96 | pub type Sha512 = CoreWrapper<CtVariableCoreWrapper<Sha512VarCore, U64, OidSha512>>; |
97 | |