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
77
78
79
80
81
use crate::;
use Hasher;
use ;
use HashAlgorithm;
/// `Sha256Hasher` is a type in RustyShield that facilitates the SHA-256 hashing algorithm.
///
/// A "Hasher" in cryptographic hashing encapsulates the object managing the transformation of input data into a
/// fixed-size byte sequence. The Hasher is tasked with maintaining the internal state of the hashing operation,
/// providing methods to append more data, and retrieve the resulting hash.
///
/// The `Sha256Hasher` struct conforms to Rust's `Hasher` trait, enabling interchangeability with other hashers in Rust.
/// It can be deployed wherever a `Hasher` implementing type is needed.
///
/// ## Examples
///
/// The following examples illustrate the use of `Sha256Hasher` with both `Hash` and `Hasher`, indicating the source of
/// the discrepancy:
///
///```rust
/// # use std::hash::{BuildHasher, Hash, Hasher};
/// # use rs_sha256::Sha256Hasher;
/// let data = b"hello";
///
/// // Using Hash
/// let mut sha256hasher = Sha256Hasher::default();
/// data.hash(&mut sha256hasher);
/// let result_via_hash = sha256hasher.finish();
///
/// // Using Hasher
/// let mut sha256hasher = Sha256Hasher::default();
/// sha256hasher.write(data);
/// let result_via_hasher = sha256hasher.finish();
///
/// // Simulating the Hash inners
/// let mut sha256hasher = Sha256Hasher::default();
/// sha256hasher.write_usize(data.len());
/// sha256hasher.write(data);
/// let simulated_hash_result = sha256hasher.finish();
///
/// assert_ne!(result_via_hash, result_via_hasher);
/// assert_eq!(result_via_hash, simulated_hash_result);
///```
;