refactor(alg): remove sha generics
This commit is contained in:
parent
afff4c8aa4
commit
11677d8fe0
3 changed files with 9 additions and 56 deletions
16
Cargo.lock
generated
16
Cargo.lock
generated
|
@ -16,7 +16,6 @@ name = "algorithms"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"criterion",
|
"criterion",
|
||||||
"generic-array",
|
|
||||||
"iai",
|
"iai",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -308,15 +307,6 @@ dependencies = [
|
||||||
"num",
|
"num",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "generic-array"
|
|
||||||
version = "1.2.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "e8c8444bc9d71b935156cc0ccab7f622180808af7867b1daae6547d773591703"
|
|
||||||
dependencies = [
|
|
||||||
"typenum",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "half"
|
name = "half"
|
||||||
version = "1.8.3"
|
version = "1.8.3"
|
||||||
|
@ -798,12 +788,6 @@ dependencies = [
|
||||||
"serde_json",
|
"serde_json",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "typenum"
|
|
||||||
version = "1.18.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "1dccffe3ce07af9386bfd29e80c0ab1a8205a2fc34e4bcd40364df902cfa8f3f"
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "unicode-ident"
|
name = "unicode-ident"
|
||||||
version = "1.0.18"
|
version = "1.0.18"
|
||||||
|
|
|
@ -4,7 +4,6 @@ version = "0.1.0"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
generic-array = "1.2.0"
|
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
criterion = "0.3"
|
criterion = "0.3"
|
||||||
|
|
|
@ -1,6 +1,4 @@
|
||||||
use generic_array::GenericArray;
|
use crate::hash::ffi::SHA2Result;
|
||||||
|
|
||||||
use crate::hash::{ffi::SHA2Result, sha2::sealed::SHA2Mode};
|
|
||||||
|
|
||||||
pub const SHA2_256_HASH_BITS: usize = 256;
|
pub const SHA2_256_HASH_BITS: usize = 256;
|
||||||
pub const SHA2_256_HASH_BYTES: usize = SHA2_256_HASH_BITS / 8;
|
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 type Digest256 = [u32; SHA2_256_HASH_PARTS];
|
||||||
pub(crate) const DIGEST256_EMPTY: Digest256 = [0; 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)]
|
#[derive(Debug, PartialEq, Eq, Clone, Hash)]
|
||||||
pub struct SHA2Context<M: sealed::SHA2Mode> {
|
pub struct SHA2_256Context {
|
||||||
intermediate_hash: GenericArray<u32, M::HashParts>,
|
intermediate_hash: Digest256,
|
||||||
block: GenericArray<u8, M::BlockSize>,
|
block: [u8; SHA2_256_BLOCK_SIZE],
|
||||||
block_idx: usize,
|
block_idx: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<M: sealed::SHA2Mode> SHA2Context<M> {
|
impl SHA2_256Context {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self {
|
||||||
intermediate_hash: Default::default(),
|
intermediate_hash: Default::default(),
|
||||||
block: Default::default(),
|
block: [0; SHA2_256_BLOCK_SIZE],
|
||||||
block_idx: Default::default(),
|
block_idx: Default::default(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,23 +32,19 @@ impl<M: sealed::SHA2Mode> SHA2Context<M> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn finish(self) -> Result<M::Digest, SHA2Result> {
|
pub fn finish(self) -> Result<Digest256, SHA2Result> {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<M: sealed::SHA2Mode> Default for SHA2Context<M> {
|
impl Default for SHA2_256Context {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self::new()
|
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> {
|
pub fn sha2_256_oneshot(data: &[u8]) -> Result<Digest256, SHA2Result> {
|
||||||
sha2_oneshot::<Mode256>(data).map(|dig| dig.into_array())
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
Loading…
Add table
Reference in a new issue