-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add x86_64 dependency for cpufeature as it is only used for this plattform * Update Cargo.toml Co-authored-by: Leonardo Arias <[email protected]> * Remove detect-cpufeatures feature * Only compile sha2 dep on x86 * Fix compilation issue on x86 --------- Co-authored-by: Leonardo Arias <[email protected]> Co-authored-by: Michael Sproul <[email protected]>
- Loading branch information
1 parent
b6b5ff2
commit 3bfe7ec
Showing
3 changed files
with
58 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// This implementation should only be compiled on x86_64 due to its dependency on the `sha2` and | ||
// `cpufeatures` crates which do not compile on some architectures like RISC-V. | ||
#![cfg(target_arch = "x86_64")] | ||
|
||
use crate::{Sha256, Sha256Context, HASH_LEN}; | ||
use sha2::Digest; | ||
|
||
/// Implementation of SHA256 using the `sha2` crate (fastest on x86_64 CPUs with SHA extensions). | ||
pub struct Sha2CrateImpl; | ||
|
||
impl Sha256Context for sha2::Sha256 { | ||
fn new() -> Self { | ||
sha2::Digest::new() | ||
} | ||
|
||
fn update(&mut self, bytes: &[u8]) { | ||
sha2::Digest::update(self, bytes) | ||
} | ||
|
||
fn finalize(self) -> [u8; HASH_LEN] { | ||
sha2::Digest::finalize(self).into() | ||
} | ||
} | ||
|
||
impl Sha256 for Sha2CrateImpl { | ||
type Context = sha2::Sha256; | ||
|
||
fn hash(&self, input: &[u8]) -> Vec<u8> { | ||
Self::Context::digest(input).into_iter().collect() | ||
} | ||
|
||
fn hash_fixed(&self, input: &[u8]) -> [u8; HASH_LEN] { | ||
Self::Context::digest(input).into() | ||
} | ||
} |