#![deny(unused_extern_crates)]
#![warn(
missing_debug_implementations,
missing_docs,
rust_2018_idioms,
unreachable_pub
)]
#[macro_use]
extern crate crunchy;
#[macro_use]
extern crate failure;
use std::fmt;
pub use self::curl::*;
pub use self::iss::*;
pub use self::kerl::*;
mod curl;
mod iss;
mod keccak;
mod kerl;
type Result<T> = ::std::result::Result<T, failure::Error>;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum HashMode {
CURLP27,
CURLP81,
Kerl,
}
impl fmt::Display for HashMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{:?}", self)
}
}
pub trait Sponge
where
Self: Default + Clone + Send + 'static,
{
fn absorb(&mut self, trits: &[i8]) -> Result<()>;
fn squeeze(&mut self, out: &mut [i8]) -> Result<()>;
fn reset(&mut self);
}
pub fn hash_with_mode(mode: HashMode, trits: &[i8], out: &mut [i8]) -> Result<()> {
ensure!(
out.len() % 243 == 0,
"Output slice length isn't a multiple of 243: {}",
out.len()
);
match mode {
HashMode::CURLP27 | HashMode::CURLP81 => {
let mut curl = Curl::new(mode).unwrap();
curl.absorb(trits)?;
curl.squeeze(out)?;
}
HashMode::Kerl => {
let mut kerl = Kerl::default();
kerl.absorb(trits)?;
kerl.squeeze(out)?;
}
}
Ok(())
}