I am interested in implementing a frontend for generic SHA2 calculation (that is supporting different SHA2 types, as in SHA2_256, SHA2_384 etc), while utilizing types-state pattern. The general structure as I see it should be similar to the following:
const SHA_256_CTX_LEN: usize = 64;
const SHA_256_HASH_LEN: usize = 32;
/* Type-states */
pub struct ShaInit;
pub struct ShaUpdate {
context: [u8; SHA_256_CTX_LEN],
}
pub struct ShaFinal {
hash: [u8; SHA_256_HASH_LEN],
}
///////
impl ShaInit {
pub fn init() -> ShaUpdate {
let mut sha_update = ShaUpdate {
context: [0u8; SHA_256_CTX_LEN],
};
/* Call actual update logic on sha_update
...
*/
sha_update
}
}
impl ShaUpdate {
pub fn update(&mut self, data: &[u8]) {
/* Perform update in-place, intermediate state is in self.context
...
*/
}
pub fn finalize(&mut self, data: &[u8]) -> ShaFinal {
let mut sha_final = ShaFinal {
hash: [0u8; SHA_256_HASH_LEN],
};
/* Call actual sha function on sha_final
...
*/
sha_final
}
}
impl ShaFinal {
pub fn get_hash(&self) -> &[u8] {
&self.hash
}
}
fn main() -> ()
{
let mut sha = ShaInit::init();
sha.update(b"Hello");
sha.update(b", ");
let sha = sha.finalize(b"World!");
println!("hash: {:?}", sha.get_hash());
}
Link to Playground
That is, each calculation is going through 3 states – uninitialized (ShaInit
), initialized (ShaUpdate
) and finalized (ShaFinal
).
It works fine as long as there is single SHA type to be calculated, but I am interested in having a more generic way to accommodate different SHA types. That is to have something like:
struct ShaType256;
struct ShaType384;
struct ShaType512;
impl ShaType for ShaType256 {}
impl ShaType for ShaType384 {}
impl ShaType for ShaType512 {}
pub struct ShaInit<T: ShaType> {...}
pub struct ShaUpdate<T: ShaType> {...}
pub struct ShaFinal<T: ShaType> {...}
The problem is that the context and the hash length (and perhaps some other state variables) are depending on the specific SHA type and basically ShaUpdate<Sha512>
structure is different field-wise from ShaUpdate<Sha256>
. The closest thing to what I am looking for that I found is the “const generics” semantics, but that would require passing constants for each varying array, while I’d like these to be “derived” somehow from the single Sha type parameter.
Is there any way to do this or something close enough to it (preferably with stable Rust)?