Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a compile error for certain feature combo. #452

Merged
merged 4 commits into from
Jul 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 15 additions & 8 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,29 @@
use crate::error::Error;

/// SHA-256 hash
#[cfg(any(feature = "sha2", feature = "ring"))]
pub fn sha256(data: &[u8]) -> Result<[u8; 32], Error> {
#[cfg(feature = "sha2")]
#[cfg(feature = "ring")]
{
// The "ring" feature takes precedence for the impl of sha256.
use ring::digest;
use std::convert::TryInto;
let hash = digest::digest(&digest::SHA256, data).as_ref().try_into()?;
return Ok(hash);
}
#[cfg(all(not(feature = "ring"), feature = "sha2"))]
{
// Only if "ring" is not enabled, but "sha2" is, does it use "sha2" for the sha256 impl.
use sha2::Digest;
let mut hasher = sha2::Sha256::new();
hasher.update(data);
let hash = hasher.finalize().into();
Ok(hash)
return Ok(hash);
}
#[cfg(feature = "ring")]
#[cfg(all(not(feature = "ring"), not(feature = "sha2")))]
{
use ring::digest;
use std::convert::TryInto;
let hash = digest::digest(&digest::SHA256, data).as_ref().try_into()?;
Ok(hash)
// If neither "ring" nor "sha2" are enabled, no sha256 impl is possible.
let _ = data;
compile_error!("The [`sha256`] function requires feature either `sha2` or `ring` but not both (and neither are currently enabled).");
}
}

Expand Down