evidence 0.1.0

Type-level tags for cryptographic primitives
Documentation
//! SHA-2 hash implementations.

use super::DigestPrimitive;
use hybrid_array::{
    Array,
    typenum::{U32, U48, U64},
};
use sha2::Digest as _;

/// SHA-256 hash primitive (32 bytes).
#[derive(Debug, Clone, Copy)]
pub enum Sha256 {}

impl DigestPrimitive for Sha256 {
    type Size = U32;

    #[allow(clippy::expect_used)] // output size is type-level guaranteed
    fn hash(data: &[u8]) -> Array<u8, Self::Size> {
        let result = sha2::Sha256::digest(data);
        Array::try_from(result.as_slice()).expect("sha256 output is always 32 bytes")
    }
}

/// SHA-384 hash primitive (48 bytes).
#[derive(Debug, Clone, Copy)]
pub enum Sha384 {}

impl DigestPrimitive for Sha384 {
    type Size = U48;

    #[allow(clippy::expect_used)] // output size is type-level guaranteed
    fn hash(data: &[u8]) -> Array<u8, Self::Size> {
        let result = sha2::Sha384::digest(data);
        Array::try_from(result.as_slice()).expect("sha384 output is always 48 bytes")
    }
}

/// SHA-512 hash primitive (64 bytes).
#[derive(Debug, Clone, Copy)]
pub enum Sha512 {}

impl DigestPrimitive for Sha512 {
    type Size = U64;

    #[allow(clippy::expect_used)] // output size is type-level guaranteed
    fn hash(data: &[u8]) -> Array<u8, Self::Size> {
        let result = sha2::Sha512::digest(data);
        Array::try_from(result.as_slice()).expect("sha512 output is always 64 bytes")
    }
}