Skip to content
Snippets Groups Projects

[opti] crypto: use sha2-asm in supported arch

Files

+ 24
3
@@ -16,11 +16,16 @@
//! Provide wrappers for cryptographic hashs
use crate::bases::*;
use crypto::digest::Digest;
use crypto::sha2::Sha256;
use rand::{thread_rng, Rng};
use std::fmt::{Debug, Display, Error, Formatter};
#[cfg(not(all(unix, any(target_arch = "x86", target_arch = "x86_64"))))]
use crypto::digest::Digest;
#[cfg(not(all(unix, any(target_arch = "x86", target_arch = "x86_64"))))]
use crypto::sha2::Sha256;
#[cfg(all(unix, any(target_arch = "x86", target_arch = "x86_64")))]
use sha2::{Digest, Sha256};
/// A hash wrapper.
///
/// A hash is often provided as string composed of 64 hexadecimal character (0 to 9 then A to F).
@@ -61,6 +66,22 @@ impl Hash {
Hash(hash_bytes_arr)
}
#[cfg(all(unix, any(target_arch = "x86", target_arch = "x86_64")))]
/// Compute hash of any binary datas
pub fn compute(datas: &[u8]) -> Hash {
let hasher = Sha256::new();
let mut hash = Hash::default();
hash.0
.copy_from_slice(hasher.chain(datas).result().as_slice());
hash
}
#[cfg(all(unix, any(target_arch = "x86", target_arch = "x86_64")))]
#[inline]
/// Compute hash of a string
pub fn compute_str(str_datas: &str) -> Hash {
Hash::compute(str_datas.as_bytes())
}
#[cfg(not(all(unix, any(target_arch = "x86", target_arch = "x86_64"))))]
/// Compute hash of any binary datas
pub fn compute(datas: &[u8]) -> Hash {
let mut sha = Sha256::new();
@@ -69,7 +90,7 @@ impl Hash {
sha.result(&mut hash_buffer);
Hash(hash_buffer)
}
#[cfg(not(all(unix, any(target_arch = "x86", target_arch = "x86_64"))))]
/// Compute hash of a string
pub fn compute_str(str_datas: &str) -> Hash {
let mut sha256 = Sha256::new();
Loading