Skip to content

Commit

Permalink
Combine sha256 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
clehner committed Jan 7, 2022
1 parent 130cbee commit 3567792
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions src/hash.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
use crate::error::Error;

#[cfg(feature = "sha2")]
#[cfg(any(feature = "sha2", feature = "ring"))]
pub fn sha256(data: &[u8]) -> Result<[u8; 32], Error> {
use sha2::Digest;
let mut hasher = sha2::Sha256::new();
hasher.update(data);
let hash = hasher.finalize().into();
Ok(hash)
}

#[cfg(feature = "ring")]
pub fn sha256(data: &[u8]) -> Result<[u8; 32], Error> {
use ring::digest;
use std::convert::TryInto;
let hash = digest::digest(&digest::SHA256, data).as_ref().try_into()?;
Ok(hash)
#[cfg(feature = "sha2")]
{
use sha2::Digest;
let mut hasher = sha2::Sha256::new();
hasher.update(data);
let hash = hasher.finalize().into();
Ok(hash)
}
#[cfg(feature = "ring")]
{
use ring::digest;
use std::convert::TryInto;
let hash = digest::digest(&digest::SHA256, data).as_ref().try_into()?;
Ok(hash)
}
}

#[cfg(test)]
Expand Down

0 comments on commit 3567792

Please # to comment.