diff --git a/src/hash.rs b/src/hash.rs index 3648475fe..bf2fb9ec8 100644 --- a/src/hash.rs +++ b/src/hash.rs @@ -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)."); } }