Skip to content
Snippets Groups Projects

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

Merged Éloïs requested to merge elois/hash-opti into dev
3 files
+ 110
12
Compare changes
  • Side-by-side
  • Inline

Files

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