Skip to content

Commit

Permalink
Implement digest::DynDigest for internal Sha512
Browse files Browse the repository at this point in the history
  • Loading branch information
mkj committed Sep 23, 2022
1 parent 58472d8 commit 5005b15
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ cosey = { version = "0.3.0", optional = true }
subtle = { version = "2.4.0", default-features = false }
zeroize = { version = "1.2.0", default-features = false }
ed25519 = { version = "1.3.0", default-features = false }
digest = { version = "0.10", default-features = false }

[dev-dependencies]
hex = "0.4.2"
Expand Down
35 changes: 35 additions & 0 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,10 @@ impl Sha512 {
// }

pub fn finalize(mut self) -> Digest {
self.finalize_borrow()
}

fn finalize_borrow(&mut self) -> Digest {
// generate padding (can be 1 or 2 blocks of 128 bytes)
let mut padding: [u8; 256] = [0u8; 256];
let padding_length = match self.unprocessed < 112 {
Expand All @@ -296,3 +300,34 @@ impl Sha512 {
self.digest
}
}

impl digest::DynDigest for Sha512 {
fn update(&mut self, data: &[u8]) {
self.update(data)
}

fn finalize_into(self, buf: &mut [u8]) -> Result<(), digest::InvalidBufferSize> {
if buf.len() < SHA512_LENGTH {
return Err(digest::InvalidBufferSize);
}
buf.copy_from_slice(&self.finalize());
Ok(())
}

fn finalize_into_reset(&mut self, buf: &mut [u8]) -> Result<(), digest::InvalidBufferSize> {
if buf.len() < SHA512_LENGTH {
return Err(digest::InvalidBufferSize);
}
buf.copy_from_slice(&self.finalize_borrow());
self.reset();
Ok(())
}

fn reset(&mut self) {
*self = Sha512::new()
}

fn output_size(&self) -> usize {
SHA512_LENGTH
}
}

0 comments on commit 5005b15

Please sign in to comment.