melodies_blake2/
lib.rs

1#![no_std]
2
3use blake2::{Blake2s256, Digest, Blake2b512};
4use generic_array::GenericArray;
5use melodies_core::crypto::HashFunction;
6use melodies_core::util::ForcedZeroizing;
7use zeroize::{ZeroizeOnDrop};
8
9#[derive(ZeroizeOnDrop)]
10pub struct BLAKE2s( ForcedZeroizing<Blake2s256> );
11#[derive(ZeroizeOnDrop)]
12pub struct BLAKE2b( ForcedZeroizing<Blake2b512> );
13
14impl HashFunction<32> for BLAKE2s {
15    fn new() -> Self where Self: Sized {
16        Self(ForcedZeroizing::new(Blake2s256::new()))
17    }
18
19    const NAME: &'static str = "BLAKE2s";
20
21    const BLOCKLEN: usize = 64;
22
23    fn update(&mut self, data: &[u8]) {
24        Digest::update(&mut *self.0, data);
25    }
26
27    fn finalize_reset(&mut self, out: &mut [u8; 32]) {
28        let out = GenericArray::from_mut_slice(out);
29        Digest::finalize_into_reset(&mut *self.0, out);
30    }
31}
32
33impl HashFunction<64> for BLAKE2b {
34    fn new() -> Self where Self: Sized {
35        Self(ForcedZeroizing::new(Blake2b512::new()))
36    }
37
38    const NAME: &'static str = "BLAKE2b";
39
40    const BLOCKLEN: usize = 128;
41
42    fn update(&mut self, data: &[u8]) {
43        Digest::update(&mut *self.0, data);
44    }
45
46    fn finalize_reset(&mut self, out: &mut [u8; 64]) {
47        let out = GenericArray::from_mut_slice(out);
48        Digest::finalize_into_reset(&mut *self.0, out);
49    }
50}