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
use crate::{bits_to_rate, keccakf::KeccakF, Hasher, KeccakState};

/// The `SHA3` hash functions defined in [`FIPS-202`].
///
/// [`FIPS-202`]: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf
///
/// # Example
///
/// ```
/// # use tiny_keccak::{Hasher, Sha3};
/// #
/// # fn main() {
/// let input = b"hello world";
/// let mut output = [0; 32];
/// let expected = b"\
///     \x64\x4b\xcc\x7e\x56\x43\x73\x04\x09\x99\xaa\xc8\x9e\x76\x22\xf3\
///     \xca\x71\xfb\xa1\xd9\x72\xfd\x94\xa3\x1c\x3b\xfb\xf2\x4e\x39\x38\
/// ";
/// let mut sha3 = Sha3::v256();
/// sha3.update(input);
/// sha3.finalize(&mut output);
/// assert_eq!(expected, &output);
/// # }
/// ```
#[derive(Clone)]
pub struct Sha3 {
    state: KeccakState<KeccakF>,
}

impl Sha3 {
    const DELIM: u8 = 0x06;

    /// Creates  new [`Sha3`] hasher with a security level of 224 bits.
    ///
    /// [`Sha3`]: struct.Sha3.html
    pub fn v224() -> Sha3 {
        Sha3::new(224)
    }

    /// Creates  new [`Sha3`] hasher with a security level of 256 bits.
    ///
    /// [`Sha3`]: struct.Sha3.html
    pub fn v256() -> Sha3 {
        Sha3::new(256)
    }

    /// Creates  new [`Sha3`] hasher with a security level of 384 bits.
    ///
    /// [`Sha3`]: struct.Sha3.html
    pub fn v384() -> Sha3 {
        Sha3::new(384)
    }

    /// Creates  new [`Sha3`] hasher with a security level of 512 bits.
    ///
    /// [`Sha3`]: struct.Sha3.html
    pub fn v512() -> Sha3 {
        Sha3::new(512)
    }

    fn new(bits: usize) -> Sha3 {
        Sha3 {
            state: KeccakState::new(bits_to_rate(bits), Self::DELIM),
        }
    }
}

impl Hasher for Sha3 {
    fn update(&mut self, input: &[u8]) {
        self.state.update(input);
    }

    fn finalize(self, output: &mut [u8]) {
        self.state.finalize(output);
    }
}