xref: /aosp_15_r20/external/crosvm/perfetto/src/hashes.rs (revision bb4ee6a4ae7042d18b07a98463b9c8b875e44b39)
1 // Copyright 2023 The ChromiumOS Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 //! Wraps various methods for calculating hashes (sha256).
6 
7 cfg_if::cfg_if! {
8     if #[cfg(feature = "pure-rust-hashes")] {
9         use sha2::Digest;
10         use sha2::Sha256;
11 
12         pub fn sha256(bytes: &[u8]) -> [u8; 32] {
13             let mut hasher = Sha256::new();
14             hasher.update(bytes);
15             hasher.finalize()[0..32].try_into().unwrap()
16         }
17     } else if #[cfg(feature = "openssl")] {
18         use openssl::sha::sha256;
19 
20         pub fn sha256(bytes: [u8]) -> [u8; 32] {
21             // We don't just re-export the library. This way, if openssl's Rust
22             // interface changes, we will get an obvious compile error here.
23             sha256(bytes)
24         }
25     } else {
26         compile_error!("Either openssl or pure-rust-hashes must be selected.");
27     }
28 }
29