pub mod sha1;
pub mod sha256;
pub mod sha384;
pub mod sha512;
pub use sha1::{SHA1, sha1_hash};
pub use sha256::{SHA256, Sha256, sha256_hash};
pub use sha384::{SHA384, Sha384, sha384_hash};
pub use sha512::{SHA512, Sha512, sha512_hash};
pub trait HashFunction {
type Output;
fn hash(data: &[u8]) -> Self::Output;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HashAlgorithm {
Sha1,
Sha256,
Sha384,
Sha512,
}
impl HashAlgorithm {
pub fn output_size(&self) -> usize {
match self {
HashAlgorithm::Sha1 => 20,
HashAlgorithm::Sha256 => 32,
HashAlgorithm::Sha384 => 48,
HashAlgorithm::Sha512 => 64,
}
}
pub fn compute(&self, data: &[u8]) -> Vec<u8> {
match self {
HashAlgorithm::Sha1 => sha1_hash(data).to_vec(),
HashAlgorithm::Sha256 => sha256_hash(data).to_vec(),
HashAlgorithm::Sha384 => sha384_hash(data).to_vec(),
HashAlgorithm::Sha512 => sha512_hash(data).to_vec(),
}
}
}
pub struct HashBuilder {
algorithm: HashAlgorithm,
}
impl HashBuilder {
pub fn new(algorithm: HashAlgorithm) -> Self {
Self { algorithm }
}
pub fn compute(&self, data: &[u8]) -> Vec<u8> {
self.algorithm.compute(data)
}
pub fn output_size(&self) -> usize {
self.algorithm.output_size()
}
}