192f3ab15Sopenharmony_ci//! The SHA family of hashes. 292f3ab15Sopenharmony_ci//! 392f3ab15Sopenharmony_ci//! SHA, or Secure Hash Algorithms, are a family of cryptographic hashing algorithms published by 492f3ab15Sopenharmony_ci//! the National Institute of Standards and Technology (NIST). Hash algorithms such as those in 592f3ab15Sopenharmony_ci//! the SHA family are used to map data of an arbitrary size to a fixed-size string of bytes. 692f3ab15Sopenharmony_ci//! As cryptographic hashing algorithms, these mappings have the property of being irreversible. 792f3ab15Sopenharmony_ci//! This property makes hash algorithms like these excellent for uses such as verifying the 892f3ab15Sopenharmony_ci//! contents of a file- if you know the hash you expect beforehand, then you can verify that the 992f3ab15Sopenharmony_ci//! data you have is correct if it hashes to the same value. 1092f3ab15Sopenharmony_ci//! 1192f3ab15Sopenharmony_ci//! # Examples 1292f3ab15Sopenharmony_ci//! 1392f3ab15Sopenharmony_ci//! When dealing with data that becomes available in chunks, such as while buffering data from IO, 1492f3ab15Sopenharmony_ci//! you can create a hasher that you can repeatedly update to add bytes to. 1592f3ab15Sopenharmony_ci//! 1692f3ab15Sopenharmony_ci//! ```rust 1792f3ab15Sopenharmony_ci//! use openssl::sha; 1892f3ab15Sopenharmony_ci//! 1992f3ab15Sopenharmony_ci//! let mut hasher = sha::Sha256::new(); 2092f3ab15Sopenharmony_ci//! 2192f3ab15Sopenharmony_ci//! hasher.update(b"Hello, "); 2292f3ab15Sopenharmony_ci//! hasher.update(b"world"); 2392f3ab15Sopenharmony_ci//! 2492f3ab15Sopenharmony_ci//! let hash = hasher.finish(); 2592f3ab15Sopenharmony_ci//! println!("Hashed \"Hello, world\" to {}", hex::encode(hash)); 2692f3ab15Sopenharmony_ci//! ``` 2792f3ab15Sopenharmony_ci//! 2892f3ab15Sopenharmony_ci//! On the other hand, if you already have access to all of the data you would like to hash, you 2992f3ab15Sopenharmony_ci//! may prefer to use the slightly simpler method of simply calling the hash function corresponding 3092f3ab15Sopenharmony_ci//! to the algorithm you want to use. 3192f3ab15Sopenharmony_ci//! 3292f3ab15Sopenharmony_ci//! ```rust 3392f3ab15Sopenharmony_ci//! use openssl::sha::sha256; 3492f3ab15Sopenharmony_ci//! 3592f3ab15Sopenharmony_ci//! let hash = sha256(b"your data or message"); 3692f3ab15Sopenharmony_ci//! println!("Hash = {}", hex::encode(hash)); 3792f3ab15Sopenharmony_ci//! ``` 3892f3ab15Sopenharmony_ciuse cfg_if::cfg_if; 3992f3ab15Sopenharmony_ciuse libc::c_void; 4092f3ab15Sopenharmony_ciuse openssl_macros::corresponds; 4192f3ab15Sopenharmony_ciuse std::mem::MaybeUninit; 4292f3ab15Sopenharmony_ci 4392f3ab15Sopenharmony_ci/// Computes the SHA1 hash of some data. 4492f3ab15Sopenharmony_ci/// 4592f3ab15Sopenharmony_ci/// # Warning 4692f3ab15Sopenharmony_ci/// 4792f3ab15Sopenharmony_ci/// SHA1 is known to be insecure - it should not be used unless required for 4892f3ab15Sopenharmony_ci/// compatibility with existing systems. 4992f3ab15Sopenharmony_ci#[corresponds(SHA1)] 5092f3ab15Sopenharmony_ci#[inline] 5192f3ab15Sopenharmony_cipub fn sha1(data: &[u8]) -> [u8; 20] { 5292f3ab15Sopenharmony_ci unsafe { 5392f3ab15Sopenharmony_ci let mut hash = MaybeUninit::<[u8; 20]>::uninit(); 5492f3ab15Sopenharmony_ci ffi::SHA1(data.as_ptr(), data.len(), hash.as_mut_ptr() as *mut _); 5592f3ab15Sopenharmony_ci hash.assume_init() 5692f3ab15Sopenharmony_ci } 5792f3ab15Sopenharmony_ci} 5892f3ab15Sopenharmony_ci 5992f3ab15Sopenharmony_ci/// Computes the SHA224 hash of some data. 6092f3ab15Sopenharmony_ci#[corresponds(SHA224)] 6192f3ab15Sopenharmony_ci#[inline] 6292f3ab15Sopenharmony_cipub fn sha224(data: &[u8]) -> [u8; 28] { 6392f3ab15Sopenharmony_ci unsafe { 6492f3ab15Sopenharmony_ci let mut hash = MaybeUninit::<[u8; 28]>::uninit(); 6592f3ab15Sopenharmony_ci ffi::SHA224(data.as_ptr(), data.len(), hash.as_mut_ptr() as *mut _); 6692f3ab15Sopenharmony_ci hash.assume_init() 6792f3ab15Sopenharmony_ci } 6892f3ab15Sopenharmony_ci} 6992f3ab15Sopenharmony_ci 7092f3ab15Sopenharmony_ci/// Computes the SHA256 hash of some data. 7192f3ab15Sopenharmony_ci#[corresponds(SHA256)] 7292f3ab15Sopenharmony_ci#[inline] 7392f3ab15Sopenharmony_cipub fn sha256(data: &[u8]) -> [u8; 32] { 7492f3ab15Sopenharmony_ci unsafe { 7592f3ab15Sopenharmony_ci let mut hash = MaybeUninit::<[u8; 32]>::uninit(); 7692f3ab15Sopenharmony_ci ffi::SHA256(data.as_ptr(), data.len(), hash.as_mut_ptr() as *mut _); 7792f3ab15Sopenharmony_ci hash.assume_init() 7892f3ab15Sopenharmony_ci } 7992f3ab15Sopenharmony_ci} 8092f3ab15Sopenharmony_ci 8192f3ab15Sopenharmony_ci/// Computes the SHA384 hash of some data. 8292f3ab15Sopenharmony_ci#[corresponds(SHA384)] 8392f3ab15Sopenharmony_ci#[inline] 8492f3ab15Sopenharmony_cipub fn sha384(data: &[u8]) -> [u8; 48] { 8592f3ab15Sopenharmony_ci unsafe { 8692f3ab15Sopenharmony_ci let mut hash = MaybeUninit::<[u8; 48]>::uninit(); 8792f3ab15Sopenharmony_ci ffi::SHA384(data.as_ptr(), data.len(), hash.as_mut_ptr() as *mut _); 8892f3ab15Sopenharmony_ci hash.assume_init() 8992f3ab15Sopenharmony_ci } 9092f3ab15Sopenharmony_ci} 9192f3ab15Sopenharmony_ci 9292f3ab15Sopenharmony_ci/// Computes the SHA512 hash of some data. 9392f3ab15Sopenharmony_ci#[corresponds(SHA512)] 9492f3ab15Sopenharmony_ci#[inline] 9592f3ab15Sopenharmony_cipub fn sha512(data: &[u8]) -> [u8; 64] { 9692f3ab15Sopenharmony_ci unsafe { 9792f3ab15Sopenharmony_ci let mut hash = MaybeUninit::<[u8; 64]>::uninit(); 9892f3ab15Sopenharmony_ci ffi::SHA512(data.as_ptr(), data.len(), hash.as_mut_ptr() as *mut _); 9992f3ab15Sopenharmony_ci hash.assume_init() 10092f3ab15Sopenharmony_ci } 10192f3ab15Sopenharmony_ci} 10292f3ab15Sopenharmony_ci 10392f3ab15Sopenharmony_cicfg_if! { 10492f3ab15Sopenharmony_ci if #[cfg(not(osslconf = "OPENSSL_NO_DEPRECATED_3_0"))] { 10592f3ab15Sopenharmony_ci /// An object which calculates a SHA1 hash of some data. 10692f3ab15Sopenharmony_ci /// 10792f3ab15Sopenharmony_ci /// # Warning 10892f3ab15Sopenharmony_ci /// 10992f3ab15Sopenharmony_ci /// SHA1 is known to be insecure - it should not be used unless required for 11092f3ab15Sopenharmony_ci /// compatibility with existing systems. 11192f3ab15Sopenharmony_ci #[derive(Clone)] 11292f3ab15Sopenharmony_ci pub struct Sha1(ffi::SHA_CTX); 11392f3ab15Sopenharmony_ci 11492f3ab15Sopenharmony_ci impl Default for Sha1 { 11592f3ab15Sopenharmony_ci #[inline] 11692f3ab15Sopenharmony_ci fn default() -> Sha1 { 11792f3ab15Sopenharmony_ci Sha1::new() 11892f3ab15Sopenharmony_ci } 11992f3ab15Sopenharmony_ci } 12092f3ab15Sopenharmony_ci 12192f3ab15Sopenharmony_ci impl Sha1 { 12292f3ab15Sopenharmony_ci /// Creates a new hasher. 12392f3ab15Sopenharmony_ci #[corresponds(SHA1_Init)] 12492f3ab15Sopenharmony_ci #[inline] 12592f3ab15Sopenharmony_ci pub fn new() -> Sha1 { 12692f3ab15Sopenharmony_ci unsafe { 12792f3ab15Sopenharmony_ci let mut ctx = MaybeUninit::uninit(); 12892f3ab15Sopenharmony_ci ffi::SHA1_Init( ctx.as_mut_ptr()); 12992f3ab15Sopenharmony_ci Sha1(ctx.assume_init()) 13092f3ab15Sopenharmony_ci } 13192f3ab15Sopenharmony_ci } 13292f3ab15Sopenharmony_ci 13392f3ab15Sopenharmony_ci /// Feeds some data into the hasher. 13492f3ab15Sopenharmony_ci /// 13592f3ab15Sopenharmony_ci /// This can be called multiple times. 13692f3ab15Sopenharmony_ci #[corresponds(SHA1_Update)] 13792f3ab15Sopenharmony_ci #[inline] 13892f3ab15Sopenharmony_ci pub fn update(&mut self, buf: &[u8]) { 13992f3ab15Sopenharmony_ci unsafe { 14092f3ab15Sopenharmony_ci ffi::SHA1_Update(&mut self.0, buf.as_ptr() as *const c_void, buf.len()); 14192f3ab15Sopenharmony_ci } 14292f3ab15Sopenharmony_ci } 14392f3ab15Sopenharmony_ci 14492f3ab15Sopenharmony_ci /// Returns the hash of the data. 14592f3ab15Sopenharmony_ci #[corresponds(SHA1_Final)] 14692f3ab15Sopenharmony_ci #[inline] 14792f3ab15Sopenharmony_ci pub fn finish(mut self) -> [u8; 20] { 14892f3ab15Sopenharmony_ci unsafe { 14992f3ab15Sopenharmony_ci let mut hash = MaybeUninit::<[u8; 20]>::uninit(); 15092f3ab15Sopenharmony_ci ffi::SHA1_Final(hash.as_mut_ptr() as *mut _, &mut self.0); 15192f3ab15Sopenharmony_ci hash.assume_init() 15292f3ab15Sopenharmony_ci } 15392f3ab15Sopenharmony_ci } 15492f3ab15Sopenharmony_ci } 15592f3ab15Sopenharmony_ci 15692f3ab15Sopenharmony_ci /// An object which calculates a SHA224 hash of some data. 15792f3ab15Sopenharmony_ci #[derive(Clone)] 15892f3ab15Sopenharmony_ci pub struct Sha224(ffi::SHA256_CTX); 15992f3ab15Sopenharmony_ci 16092f3ab15Sopenharmony_ci impl Default for Sha224 { 16192f3ab15Sopenharmony_ci #[inline] 16292f3ab15Sopenharmony_ci fn default() -> Sha224 { 16392f3ab15Sopenharmony_ci Sha224::new() 16492f3ab15Sopenharmony_ci } 16592f3ab15Sopenharmony_ci } 16692f3ab15Sopenharmony_ci 16792f3ab15Sopenharmony_ci impl Sha224 { 16892f3ab15Sopenharmony_ci /// Creates a new hasher. 16992f3ab15Sopenharmony_ci #[corresponds(SHA224_Init)] 17092f3ab15Sopenharmony_ci #[inline] 17192f3ab15Sopenharmony_ci pub fn new() -> Sha224 { 17292f3ab15Sopenharmony_ci unsafe { 17392f3ab15Sopenharmony_ci let mut ctx = MaybeUninit::uninit(); 17492f3ab15Sopenharmony_ci ffi::SHA224_Init(ctx.as_mut_ptr()); 17592f3ab15Sopenharmony_ci Sha224(ctx.assume_init()) 17692f3ab15Sopenharmony_ci } 17792f3ab15Sopenharmony_ci } 17892f3ab15Sopenharmony_ci 17992f3ab15Sopenharmony_ci /// Feeds some data into the hasher. 18092f3ab15Sopenharmony_ci /// 18192f3ab15Sopenharmony_ci /// This can be called multiple times. 18292f3ab15Sopenharmony_ci #[corresponds(SHA224_Update)] 18392f3ab15Sopenharmony_ci #[inline] 18492f3ab15Sopenharmony_ci pub fn update(&mut self, buf: &[u8]) { 18592f3ab15Sopenharmony_ci unsafe { 18692f3ab15Sopenharmony_ci ffi::SHA224_Update(&mut self.0, buf.as_ptr() as *const c_void, buf.len()); 18792f3ab15Sopenharmony_ci } 18892f3ab15Sopenharmony_ci } 18992f3ab15Sopenharmony_ci 19092f3ab15Sopenharmony_ci /// Returns the hash of the data. 19192f3ab15Sopenharmony_ci #[corresponds(SHA224_Final)] 19292f3ab15Sopenharmony_ci #[inline] 19392f3ab15Sopenharmony_ci pub fn finish(mut self) -> [u8; 28] { 19492f3ab15Sopenharmony_ci unsafe { 19592f3ab15Sopenharmony_ci let mut hash = MaybeUninit::<[u8; 28]>::uninit(); 19692f3ab15Sopenharmony_ci ffi::SHA224_Final(hash.as_mut_ptr() as *mut _, &mut self.0); 19792f3ab15Sopenharmony_ci hash.assume_init() 19892f3ab15Sopenharmony_ci } 19992f3ab15Sopenharmony_ci } 20092f3ab15Sopenharmony_ci } 20192f3ab15Sopenharmony_ci 20292f3ab15Sopenharmony_ci /// An object which calculates a SHA256 hash of some data. 20392f3ab15Sopenharmony_ci #[derive(Clone)] 20492f3ab15Sopenharmony_ci pub struct Sha256(ffi::SHA256_CTX); 20592f3ab15Sopenharmony_ci 20692f3ab15Sopenharmony_ci impl Default for Sha256 { 20792f3ab15Sopenharmony_ci #[inline] 20892f3ab15Sopenharmony_ci fn default() -> Sha256 { 20992f3ab15Sopenharmony_ci Sha256::new() 21092f3ab15Sopenharmony_ci } 21192f3ab15Sopenharmony_ci } 21292f3ab15Sopenharmony_ci 21392f3ab15Sopenharmony_ci impl Sha256 { 21492f3ab15Sopenharmony_ci /// Creates a new hasher. 21592f3ab15Sopenharmony_ci #[corresponds(SHA256_Init)] 21692f3ab15Sopenharmony_ci #[inline] 21792f3ab15Sopenharmony_ci pub fn new() -> Sha256 { 21892f3ab15Sopenharmony_ci unsafe { 21992f3ab15Sopenharmony_ci let mut ctx = MaybeUninit::uninit(); 22092f3ab15Sopenharmony_ci ffi::SHA256_Init(ctx.as_mut_ptr()); 22192f3ab15Sopenharmony_ci Sha256(ctx.assume_init()) 22292f3ab15Sopenharmony_ci } 22392f3ab15Sopenharmony_ci } 22492f3ab15Sopenharmony_ci 22592f3ab15Sopenharmony_ci /// Feeds some data into the hasher. 22692f3ab15Sopenharmony_ci /// 22792f3ab15Sopenharmony_ci /// This can be called multiple times. 22892f3ab15Sopenharmony_ci #[corresponds(SHA256_Update)] 22992f3ab15Sopenharmony_ci #[inline] 23092f3ab15Sopenharmony_ci pub fn update(&mut self, buf: &[u8]) { 23192f3ab15Sopenharmony_ci unsafe { 23292f3ab15Sopenharmony_ci ffi::SHA256_Update(&mut self.0, buf.as_ptr() as *const c_void, buf.len()); 23392f3ab15Sopenharmony_ci } 23492f3ab15Sopenharmony_ci } 23592f3ab15Sopenharmony_ci 23692f3ab15Sopenharmony_ci /// Returns the hash of the data. 23792f3ab15Sopenharmony_ci #[corresponds(SHA256_Final)] 23892f3ab15Sopenharmony_ci #[inline] 23992f3ab15Sopenharmony_ci pub fn finish(mut self) -> [u8; 32] { 24092f3ab15Sopenharmony_ci unsafe { 24192f3ab15Sopenharmony_ci let mut hash = MaybeUninit::<[u8; 32]>::uninit(); 24292f3ab15Sopenharmony_ci ffi::SHA256_Final(hash.as_mut_ptr() as *mut _, &mut self.0); 24392f3ab15Sopenharmony_ci hash.assume_init() 24492f3ab15Sopenharmony_ci } 24592f3ab15Sopenharmony_ci } 24692f3ab15Sopenharmony_ci } 24792f3ab15Sopenharmony_ci 24892f3ab15Sopenharmony_ci /// An object which calculates a SHA384 hash of some data. 24992f3ab15Sopenharmony_ci #[derive(Clone)] 25092f3ab15Sopenharmony_ci pub struct Sha384(ffi::SHA512_CTX); 25192f3ab15Sopenharmony_ci 25292f3ab15Sopenharmony_ci impl Default for Sha384 { 25392f3ab15Sopenharmony_ci #[inline] 25492f3ab15Sopenharmony_ci fn default() -> Sha384 { 25592f3ab15Sopenharmony_ci Sha384::new() 25692f3ab15Sopenharmony_ci } 25792f3ab15Sopenharmony_ci } 25892f3ab15Sopenharmony_ci 25992f3ab15Sopenharmony_ci impl Sha384 { 26092f3ab15Sopenharmony_ci /// Creates a new hasher. 26192f3ab15Sopenharmony_ci #[corresponds(SHA384_Init)] 26292f3ab15Sopenharmony_ci #[inline] 26392f3ab15Sopenharmony_ci pub fn new() -> Sha384 { 26492f3ab15Sopenharmony_ci unsafe { 26592f3ab15Sopenharmony_ci let mut ctx = MaybeUninit::uninit(); 26692f3ab15Sopenharmony_ci ffi::SHA384_Init(ctx.as_mut_ptr()); 26792f3ab15Sopenharmony_ci Sha384(ctx.assume_init()) 26892f3ab15Sopenharmony_ci } 26992f3ab15Sopenharmony_ci } 27092f3ab15Sopenharmony_ci 27192f3ab15Sopenharmony_ci /// Feeds some data into the hasher. 27292f3ab15Sopenharmony_ci /// 27392f3ab15Sopenharmony_ci /// This can be called multiple times. 27492f3ab15Sopenharmony_ci #[corresponds(SHA384_Update)] 27592f3ab15Sopenharmony_ci #[inline] 27692f3ab15Sopenharmony_ci pub fn update(&mut self, buf: &[u8]) { 27792f3ab15Sopenharmony_ci unsafe { 27892f3ab15Sopenharmony_ci ffi::SHA384_Update(&mut self.0, buf.as_ptr() as *const c_void, buf.len()); 27992f3ab15Sopenharmony_ci } 28092f3ab15Sopenharmony_ci } 28192f3ab15Sopenharmony_ci 28292f3ab15Sopenharmony_ci /// Returns the hash of the data. 28392f3ab15Sopenharmony_ci #[corresponds(SHA384_Final)] 28492f3ab15Sopenharmony_ci #[inline] 28592f3ab15Sopenharmony_ci pub fn finish(mut self) -> [u8; 48] { 28692f3ab15Sopenharmony_ci unsafe { 28792f3ab15Sopenharmony_ci let mut hash = MaybeUninit::<[u8; 48]>::uninit(); 28892f3ab15Sopenharmony_ci ffi::SHA384_Final(hash.as_mut_ptr() as *mut _, &mut self.0); 28992f3ab15Sopenharmony_ci hash.assume_init() 29092f3ab15Sopenharmony_ci } 29192f3ab15Sopenharmony_ci } 29292f3ab15Sopenharmony_ci } 29392f3ab15Sopenharmony_ci 29492f3ab15Sopenharmony_ci /// An object which calculates a SHA512 hash of some data. 29592f3ab15Sopenharmony_ci #[derive(Clone)] 29692f3ab15Sopenharmony_ci pub struct Sha512(ffi::SHA512_CTX); 29792f3ab15Sopenharmony_ci 29892f3ab15Sopenharmony_ci impl Default for Sha512 { 29992f3ab15Sopenharmony_ci #[inline] 30092f3ab15Sopenharmony_ci fn default() -> Sha512 { 30192f3ab15Sopenharmony_ci Sha512::new() 30292f3ab15Sopenharmony_ci } 30392f3ab15Sopenharmony_ci } 30492f3ab15Sopenharmony_ci 30592f3ab15Sopenharmony_ci impl Sha512 { 30692f3ab15Sopenharmony_ci /// Creates a new hasher. 30792f3ab15Sopenharmony_ci #[corresponds(SHA512_Init)] 30892f3ab15Sopenharmony_ci #[inline] 30992f3ab15Sopenharmony_ci pub fn new() -> Sha512 { 31092f3ab15Sopenharmony_ci unsafe { 31192f3ab15Sopenharmony_ci let mut ctx = MaybeUninit::uninit(); 31292f3ab15Sopenharmony_ci ffi::SHA512_Init(ctx.as_mut_ptr()); 31392f3ab15Sopenharmony_ci Sha512(ctx.assume_init()) 31492f3ab15Sopenharmony_ci } 31592f3ab15Sopenharmony_ci } 31692f3ab15Sopenharmony_ci 31792f3ab15Sopenharmony_ci /// Feeds some data into the hasher. 31892f3ab15Sopenharmony_ci /// 31992f3ab15Sopenharmony_ci /// This can be called multiple times. 32092f3ab15Sopenharmony_ci #[corresponds(SHA512_Update)] 32192f3ab15Sopenharmony_ci #[inline] 32292f3ab15Sopenharmony_ci pub fn update(&mut self, buf: &[u8]) { 32392f3ab15Sopenharmony_ci unsafe { 32492f3ab15Sopenharmony_ci ffi::SHA512_Update(&mut self.0, buf.as_ptr() as *const c_void, buf.len()); 32592f3ab15Sopenharmony_ci } 32692f3ab15Sopenharmony_ci } 32792f3ab15Sopenharmony_ci 32892f3ab15Sopenharmony_ci /// Returns the hash of the data. 32992f3ab15Sopenharmony_ci #[corresponds(SHA512_Final)] 33092f3ab15Sopenharmony_ci #[inline] 33192f3ab15Sopenharmony_ci pub fn finish(mut self) -> [u8; 64] { 33292f3ab15Sopenharmony_ci unsafe { 33392f3ab15Sopenharmony_ci let mut hash= MaybeUninit::<[u8; 64]>::uninit(); 33492f3ab15Sopenharmony_ci ffi::SHA512_Final(hash.as_mut_ptr() as *mut _, &mut self.0); 33592f3ab15Sopenharmony_ci hash.assume_init() 33692f3ab15Sopenharmony_ci } 33792f3ab15Sopenharmony_ci } 33892f3ab15Sopenharmony_ci } 33992f3ab15Sopenharmony_ci } 34092f3ab15Sopenharmony_ci} 34192f3ab15Sopenharmony_ci 34292f3ab15Sopenharmony_ci#[cfg(test)] 34392f3ab15Sopenharmony_cimod test { 34492f3ab15Sopenharmony_ci use super::*; 34592f3ab15Sopenharmony_ci 34692f3ab15Sopenharmony_ci #[test] 34792f3ab15Sopenharmony_ci fn standalone_1() { 34892f3ab15Sopenharmony_ci let data = b"abc"; 34992f3ab15Sopenharmony_ci let expected = "a9993e364706816aba3e25717850c26c9cd0d89d"; 35092f3ab15Sopenharmony_ci 35192f3ab15Sopenharmony_ci assert_eq!(hex::encode(sha1(data)), expected); 35292f3ab15Sopenharmony_ci } 35392f3ab15Sopenharmony_ci 35492f3ab15Sopenharmony_ci #[test] 35592f3ab15Sopenharmony_ci #[cfg(not(osslconf = "OPENSSL_NO_DEPRECATED_3_0"))] 35692f3ab15Sopenharmony_ci fn struct_1() { 35792f3ab15Sopenharmony_ci let expected = "a9993e364706816aba3e25717850c26c9cd0d89d"; 35892f3ab15Sopenharmony_ci 35992f3ab15Sopenharmony_ci let mut hasher = Sha1::new(); 36092f3ab15Sopenharmony_ci hasher.update(b"a"); 36192f3ab15Sopenharmony_ci hasher.update(b"bc"); 36292f3ab15Sopenharmony_ci assert_eq!(hex::encode(hasher.finish()), expected); 36392f3ab15Sopenharmony_ci } 36492f3ab15Sopenharmony_ci 36592f3ab15Sopenharmony_ci #[test] 36692f3ab15Sopenharmony_ci #[cfg(not(osslconf = "OPENSSL_NO_DEPRECATED_3_0"))] 36792f3ab15Sopenharmony_ci fn cloning_allows_incremental_hashing() { 36892f3ab15Sopenharmony_ci let expected = "a9993e364706816aba3e25717850c26c9cd0d89d"; 36992f3ab15Sopenharmony_ci 37092f3ab15Sopenharmony_ci let mut hasher = Sha1::new(); 37192f3ab15Sopenharmony_ci hasher.update(b"a"); 37292f3ab15Sopenharmony_ci 37392f3ab15Sopenharmony_ci let mut incr_hasher = hasher.clone(); 37492f3ab15Sopenharmony_ci incr_hasher.update(b"bc"); 37592f3ab15Sopenharmony_ci 37692f3ab15Sopenharmony_ci assert_eq!(hex::encode(incr_hasher.finish()), expected); 37792f3ab15Sopenharmony_ci assert_ne!(hex::encode(hasher.finish()), expected); 37892f3ab15Sopenharmony_ci } 37992f3ab15Sopenharmony_ci 38092f3ab15Sopenharmony_ci #[test] 38192f3ab15Sopenharmony_ci fn standalone_224() { 38292f3ab15Sopenharmony_ci let data = b"abc"; 38392f3ab15Sopenharmony_ci let expected = "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7"; 38492f3ab15Sopenharmony_ci 38592f3ab15Sopenharmony_ci assert_eq!(hex::encode(sha224(data)), expected); 38692f3ab15Sopenharmony_ci } 38792f3ab15Sopenharmony_ci 38892f3ab15Sopenharmony_ci #[test] 38992f3ab15Sopenharmony_ci #[cfg(not(osslconf = "OPENSSL_NO_DEPRECATED_3_0"))] 39092f3ab15Sopenharmony_ci fn struct_224() { 39192f3ab15Sopenharmony_ci let expected = "23097d223405d8228642a477bda255b32aadbce4bda0b3f7e36c9da7"; 39292f3ab15Sopenharmony_ci 39392f3ab15Sopenharmony_ci let mut hasher = Sha224::new(); 39492f3ab15Sopenharmony_ci hasher.update(b"a"); 39592f3ab15Sopenharmony_ci hasher.update(b"bc"); 39692f3ab15Sopenharmony_ci assert_eq!(hex::encode(hasher.finish()), expected); 39792f3ab15Sopenharmony_ci } 39892f3ab15Sopenharmony_ci 39992f3ab15Sopenharmony_ci #[test] 40092f3ab15Sopenharmony_ci fn standalone_256() { 40192f3ab15Sopenharmony_ci let data = b"abc"; 40292f3ab15Sopenharmony_ci let expected = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"; 40392f3ab15Sopenharmony_ci 40492f3ab15Sopenharmony_ci assert_eq!(hex::encode(sha256(data)), expected); 40592f3ab15Sopenharmony_ci } 40692f3ab15Sopenharmony_ci 40792f3ab15Sopenharmony_ci #[test] 40892f3ab15Sopenharmony_ci #[cfg(not(osslconf = "OPENSSL_NO_DEPRECATED_3_0"))] 40992f3ab15Sopenharmony_ci fn struct_256() { 41092f3ab15Sopenharmony_ci let expected = "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"; 41192f3ab15Sopenharmony_ci 41292f3ab15Sopenharmony_ci let mut hasher = Sha256::new(); 41392f3ab15Sopenharmony_ci hasher.update(b"a"); 41492f3ab15Sopenharmony_ci hasher.update(b"bc"); 41592f3ab15Sopenharmony_ci assert_eq!(hex::encode(hasher.finish()), expected); 41692f3ab15Sopenharmony_ci } 41792f3ab15Sopenharmony_ci 41892f3ab15Sopenharmony_ci #[test] 41992f3ab15Sopenharmony_ci fn standalone_384() { 42092f3ab15Sopenharmony_ci let data = b"abc"; 42192f3ab15Sopenharmony_ci let expected = 42292f3ab15Sopenharmony_ci "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e\ 42392f3ab15Sopenharmony_ci 7cc2358baeca134c825a7"; 42492f3ab15Sopenharmony_ci 42592f3ab15Sopenharmony_ci assert_eq!(hex::encode(&sha384(data)[..]), expected); 42692f3ab15Sopenharmony_ci } 42792f3ab15Sopenharmony_ci 42892f3ab15Sopenharmony_ci #[test] 42992f3ab15Sopenharmony_ci #[cfg(not(osslconf = "OPENSSL_NO_DEPRECATED_3_0"))] 43092f3ab15Sopenharmony_ci fn struct_384() { 43192f3ab15Sopenharmony_ci let expected = 43292f3ab15Sopenharmony_ci "cb00753f45a35e8bb5a03d699ac65007272c32ab0eded1631a8b605a43ff5bed8086072ba1e\ 43392f3ab15Sopenharmony_ci 7cc2358baeca134c825a7"; 43492f3ab15Sopenharmony_ci 43592f3ab15Sopenharmony_ci let mut hasher = Sha384::new(); 43692f3ab15Sopenharmony_ci hasher.update(b"a"); 43792f3ab15Sopenharmony_ci hasher.update(b"bc"); 43892f3ab15Sopenharmony_ci assert_eq!(hex::encode(&hasher.finish()[..]), expected); 43992f3ab15Sopenharmony_ci } 44092f3ab15Sopenharmony_ci 44192f3ab15Sopenharmony_ci #[test] 44292f3ab15Sopenharmony_ci fn standalone_512() { 44392f3ab15Sopenharmony_ci let data = b"abc"; 44492f3ab15Sopenharmony_ci let expected = 44592f3ab15Sopenharmony_ci "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274\ 44692f3ab15Sopenharmony_ci fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"; 44792f3ab15Sopenharmony_ci 44892f3ab15Sopenharmony_ci assert_eq!(hex::encode(&sha512(data)[..]), expected); 44992f3ab15Sopenharmony_ci } 45092f3ab15Sopenharmony_ci 45192f3ab15Sopenharmony_ci #[test] 45292f3ab15Sopenharmony_ci #[cfg(not(osslconf = "OPENSSL_NO_DEPRECATED_3_0"))] 45392f3ab15Sopenharmony_ci fn struct_512() { 45492f3ab15Sopenharmony_ci let expected = 45592f3ab15Sopenharmony_ci "ddaf35a193617abacc417349ae20413112e6fa4e89a97ea20a9eeee64b55d39a2192992a274\ 45692f3ab15Sopenharmony_ci fc1a836ba3c23a3feebbd454d4423643ce80e2a9ac94fa54ca49f"; 45792f3ab15Sopenharmony_ci 45892f3ab15Sopenharmony_ci let mut hasher = Sha512::new(); 45992f3ab15Sopenharmony_ci hasher.update(b"a"); 46092f3ab15Sopenharmony_ci hasher.update(b"bc"); 46192f3ab15Sopenharmony_ci assert_eq!(hex::encode(&hasher.finish()[..]), expected); 46292f3ab15Sopenharmony_ci } 46392f3ab15Sopenharmony_ci} 464