refactor(alg): remove sha generics

This commit is contained in:
cscherr 2025-07-18 12:08:49 +02:00
parent afff4c8aa4
commit 11677d8fe0
Signed by: cscherrNT
GPG key ID: 8E2B45BC51A27EA7
3 changed files with 9 additions and 56 deletions

16
Cargo.lock generated
View file

@ -16,7 +16,6 @@ name = "algorithms"
version = "0.1.0"
dependencies = [
"criterion",
"generic-array",
"iai",
]
@ -308,15 +307,6 @@ dependencies = [
"num",
]
[[package]]
name = "generic-array"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e8c8444bc9d71b935156cc0ccab7f622180808af7867b1daae6547d773591703"
dependencies = [
"typenum",
]
[[package]]
name = "half"
version = "1.8.3"
@ -798,12 +788,6 @@ dependencies = [
"serde_json",
]
[[package]]
name = "typenum"
version = "1.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f"
[[package]]
name = "unicode-ident"
version = "1.0.18"

View file

@ -4,7 +4,6 @@ version = "0.1.0"
edition = "2024"
[dependencies]
generic-array = "1.2.0"
[dev-dependencies]
criterion = "0.3"

View file

@ -1,6 +1,4 @@
use generic_array::GenericArray;
use crate::hash::{ffi::SHA2Result, sha2::sealed::SHA2Mode};
use crate::hash::ffi::SHA2Result;
pub const SHA2_256_HASH_BITS: usize = 256;
pub const SHA2_256_HASH_BYTES: usize = SHA2_256_HASH_BITS / 8;
@ -10,42 +8,18 @@ pub const SHA2_256_BLOCK_SIZE: usize = 2 * SHA2_256_HASH_BYTES;
pub type Digest256 = [u32; SHA2_256_HASH_PARTS];
pub(crate) const DIGEST256_EMPTY: Digest256 = [0; SHA2_256_HASH_PARTS];
pub struct Mode256;
mod sealed {
use generic_array::{ArrayLength, GenericArray};
use super::Mode256;
pub trait SHA2Mode {
type HashBits: ArrayLength;
type HashBytes: ArrayLength;
type HashParts: ArrayLength;
type BlockSize: ArrayLength;
type Digest: AsRef<[u32]> + AsMut<[u32]>;
type Block: AsRef<[u8]> + AsMut<[u8]>;
}
impl SHA2Mode for Mode256 {
type HashBits = generic_array::typenum::U256;
type HashBytes = generic_array::typenum::U32;
type HashParts = generic_array::typenum::U8;
type BlockSize = generic_array::typenum::U512;
type Digest = GenericArray<u32, Self::HashParts>;
type Block = GenericArray<u8, Self::BlockSize>;
}
impl Mode256 {}
}
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
pub struct SHA2Context<M: sealed::SHA2Mode> {
intermediate_hash: GenericArray<u32, M::HashParts>,
block: GenericArray<u8, M::BlockSize>,
pub struct SHA2_256Context {
intermediate_hash: Digest256,
block: [u8; SHA2_256_BLOCK_SIZE],
block_idx: usize,
}
impl<M: sealed::SHA2Mode> SHA2Context<M> {
impl SHA2_256Context {
pub fn new() -> Self {
Self {
intermediate_hash: Default::default(),
block: Default::default(),
block: [0; SHA2_256_BLOCK_SIZE],
block_idx: Default::default(),
}
}
@ -58,23 +32,19 @@ impl<M: sealed::SHA2Mode> SHA2Context<M> {
todo!()
}
pub fn finish(self) -> Result<M::Digest, SHA2Result> {
pub fn finish(self) -> Result<Digest256, SHA2Result> {
todo!()
}
}
impl<M: sealed::SHA2Mode> Default for SHA2Context<M> {
impl Default for SHA2_256Context {
fn default() -> Self {
Self::new()
}
}
pub fn sha2_oneshot<M: sealed::SHA2Mode>(data: &[u8]) -> Result<M::Digest, SHA2Result> {
todo!()
}
pub fn sha2_256_oneshot(data: &[u8]) -> Result<Digest256, SHA2Result> {
sha2_oneshot::<Mode256>(data).map(|dig| dig.into_array())
todo!()
}
#[cfg(test)]