1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! # SHA-256 `rs_sha256` - Secure Hash Algorithm 256
//!
//! SHA-256 is a member of the SHA-2 cryptographic hash functions designed by the National Institute of Standards and
//! Technology (NIST). It is commonly employed in various security communication protocols and data integrity checks.
//!
//! ## Usage
//!
//! The crate offers a straightforward API, enabling users to instantiate a new SHA-256 hasher, update it with input
//! data, and finalize to obtain the resultant hash.
//!
//! ### Example
//!
//! This is an illustration of using the SHA-256 hash function in Rust:
//!
//! ```rust
//! # use std::hash::{BuildHasher, Hasher};
//! # use rs_sha256::Sha256State;
//! let mut sha256hasher = Sha256State::default().build_hasher();
//! sha256hasher.write(b"hello world");
//! let result = sha256hasher.finish();
//! assert_eq!(result, 0xB94D27B9934D3E08);
//! ```
//!
//! Additionally, in a `HashSet` context:
//!
//! ```rust
//! # use std::{
//! # collections::HashSet,
//! # hash::{BuildHasher, Hash, Hasher}
//! # };
//! # use rs_sha256::Sha256Hasher;
//! let hello = "hello";
//! let mut sha256hasher1 = Sha256Hasher::default();
//! let mut sha256hasher2 = Sha256Hasher::default();
//! let mut sha256hasher3 = Sha256Hasher::default();
//!
//! sha256hasher1.write(hello.as_bytes());
//! hello.hash(&mut sha256hasher2);
//! sha256hasher3.write(hello.as_bytes());
//! sha256hasher3.write(&[0xFF]);
//!
//! let u64result1 = sha256hasher1.finish();
//! let u64result2 = sha256hasher2.finish();
//! let u64result3 = sha256hasher3.finish();
//!
//! assert_eq!(u64result1, 0x2CF24DBA5FB0A30E);
//! assert_eq!(u64result2, 0xFCFE450961C66DC3);
//! assert_eq!(u64result2, u64result3);
//! assert_ne!(u64result1, u64result2);
//! ```
//!
//! ## Use Cases
//!
//! SHA-256 is broadly used in various security-critical tasks, including:
//!
//! - TLS and SSL, IPsec, and SSH for network communication security.
//! - Digital signatures and certificate authorities for data integrity and sender authentication.
//! - Blockchain applications for ensuring data integrity.
//!
//! [NIST](https://www.nist.gov/) recommends SHA-256 for most applications until 2030, marking it as a secure option for
//! cryptographic functions in the current landscape.
//!
pub use HasherContext;
pub use Sha256Hasher;
pub use Sha256State;
const BYTES_LEN: usize = 32;