| 1 |
use crate::{bits_to_rate, keccakf::KeccakF, Hasher, KeccakState, Xof}; |
| 2 |
|
| 3 |
/// The `SHAKE` extendable-output functions defined in [`FIPS-202`]. |
| 4 |
/// |
| 5 |
/// # Usage |
| 6 |
/// |
| 7 |
/// ```toml |
| 8 |
/// [dependencies] |
| 9 |
/// tiny-keccak = { version = "2.0.0", features = ["shake"] } |
| 10 |
/// ``` |
| 11 |
/// |
| 12 |
/// [`FIPS-202`]: https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.202.pdf |
| 13 |
#[derive(Clone)] |
| 14 |
pub struct Shake { |
| 15 |
state: KeccakState<KeccakF>, |
| 16 |
} |
| 17 |
|
| 18 |
impl Shake { |
| 19 |
const DELIM: u8 = 0x1f; |
| 20 |
|
| 21 |
/// Creates new [`Shake`] hasher with a security level of 128 bits. |
| 22 |
/// |
| 23 |
/// [`Shake`]: struct.Shake.html |
| 24 |
pub fn v128() -> Shake { |
| 25 |
Shake::new(128) |
| 26 |
} |
| 27 |
|
| 28 |
/// Creates new [`Shake`] hasher with a security level of 256 bits. |
| 29 |
/// |
| 30 |
/// [`Shake`]: struct.Shake.html |
| 31 |
pub fn v256() -> Shake { |
| 32 |
Shake::new(256) |
| 33 |
} |
| 34 |
|
| 35 |
pub(crate) fn new(bits: usize) -> Shake { |
| 36 |
Shake { |
| 37 |
state: KeccakState::new(bits_to_rate(bits), Self::DELIM), |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
|
| 42 |
impl Hasher for Shake { |
| 43 |
fn update(&mut self, input: &[u8]) { |
| 44 |
self.state.update(input); |
| 45 |
} |
| 46 |
|
| 47 |
fn finalize(self, output: &mut [u8]) { |
| 48 |
self.state.finalize(output); |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
impl Xof for Shake { |
| 53 |
fn squeeze(&mut self, output: &mut [u8]) { |
| 54 |
self.state.squeeze(output) |
| 55 |
} |
| 56 |
} |