diff --git a/Cargo.toml b/Cargo.toml index 6407800..91a5266 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/hash.rs b/src/hash.rs index 1cd9f61..bb49c50 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -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 { @@ -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 + } +}