sha2/
lib.rs

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