2023-04-30 23:26:36 +00:00
// SPDX-License-Identifier: CC0-1.0
2022-11-23 07:48:57 +00:00
//! SHA512_256 implementation.
//!
//! SHA512/256 is a hash function that uses the sha512 alogrithm but it truncates
//! the output to 256 bits. It has different initial constants than sha512 so it
//! produces an entirely different hash compared to sha512. More information at
2023-01-21 03:58:59 +00:00
//! <https://eprint.iacr.org/2010/548.pdf>.
2022-11-23 07:48:57 +00:00
use core ::ops ::Index ;
use core ::slice ::SliceIndex ;
2023-02-21 23:25:12 +00:00
use core ::str ;
2022-11-23 07:48:57 +00:00
2023-05-24 03:29:30 +00:00
use crate ::{ sha512 , FromSliceError } ;
2022-11-23 07:48:57 +00:00
/// Engine to compute SHA512/256 hash function.
///
/// SHA512/256 is a hash function that uses the sha512 alogrithm but it truncates
/// the output to 256 bits. It has different initial constants than sha512 so it
/// produces an entirely different hash compared to sha512. More information at
2023-01-21 03:58:59 +00:00
/// <https://eprint.iacr.org/2010/548.pdf>.
2022-11-23 07:48:57 +00:00
#[ derive(Clone) ]
pub struct HashEngine ( sha512 ::HashEngine ) ;
impl Default for HashEngine {
2023-02-21 23:04:08 +00:00
#[ rustfmt::skip ]
2022-11-23 07:48:57 +00:00
fn default ( ) -> Self {
2023-06-19 18:21:28 +00:00
HashEngine ( sha512 ::HashEngine ::sha512_256 ( ) )
2022-11-23 07:48:57 +00:00
}
}
impl crate ::HashEngine for HashEngine {
type MidState = [ u8 ; 64 ] ;
2023-02-21 23:25:12 +00:00
fn midstate ( & self ) -> [ u8 ; 64 ] { self . 0. midstate ( ) }
2022-11-23 07:48:57 +00:00
const BLOCK_SIZE : usize = sha512 ::BLOCK_SIZE ;
2023-06-19 18:21:28 +00:00
fn n_bytes_hashed ( & self ) -> usize { self . 0. n_bytes_hashed ( ) }
2022-11-23 07:48:57 +00:00
2023-02-21 23:25:12 +00:00
fn input ( & mut self , inp : & [ u8 ] ) { self . 0. input ( inp ) ; }
2022-11-23 07:48:57 +00:00
}
crate ::internal_macros ::hash_type! {
256 ,
false ,
2023-01-21 03:58:59 +00:00
" Output of the SHA512/256 hash function. \n \n SHA512/256 is a hash function that uses the sha512 alogrithm but it truncates the output to 256 bits. It has different initial constants than sha512 so it produces an entirely different hash compared to sha512. More information at <https://eprint.iacr.org/2010/548.pdf>. " ,
2022-11-23 07:48:57 +00:00
" crate::util::json_hex_string::len_32 "
}
fn from_engine ( e : HashEngine ) -> Hash {
let mut ret = [ 0 ; 32 ] ;
ret . copy_from_slice ( & sha512 ::from_engine ( e . 0 ) [ .. 32 ] ) ;
Hash ( ret )
}
#[ cfg(test) ]
mod tests {
#[ test ]
2023-01-21 03:43:48 +00:00
#[ cfg(feature = " alloc " ) ]
2022-11-23 07:48:57 +00:00
fn test ( ) {
use crate ::{ sha512_256 , Hash , HashEngine } ;
#[ derive(Clone) ]
struct Test {
input : & 'static str ,
output : Vec < u8 > ,
output_str : & 'static str ,
}
2023-02-21 23:04:08 +00:00
#[ rustfmt::skip ]
2022-11-23 07:48:57 +00:00
let tests = vec! [
// Examples from go sha512/256 tests.
Test {
input : " " ,
output : vec ! [
0xc6 , 0x72 , 0xb8 , 0xd1 , 0xef , 0x56 , 0xed , 0x28 ,
0xab , 0x87 , 0xc3 , 0x62 , 0x2c , 0x51 , 0x14 , 0x06 ,
0x9b , 0xdd , 0x3a , 0xd7 , 0xb8 , 0xf9 , 0x73 , 0x74 ,
0x98 , 0xd0 , 0xc0 , 0x1e , 0xce , 0xf0 , 0x96 , 0x7a ,
] ,
output_str : " c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a "
} ,
Test {
input : " abcdef " ,
output : vec ! [
0xe4 , 0xfd , 0xcb , 0x11 , 0xd1 , 0xac , 0x14 , 0xe6 ,
0x98 , 0x74 , 0x3a , 0xcd , 0x88 , 0x05 , 0x17 , 0x4c ,
0xea , 0x5d , 0xdc , 0x0d , 0x31 , 0x2e , 0x3e , 0x47 ,
0xf6 , 0x37 , 0x20 , 0x32 , 0x57 , 0x1b , 0xad , 0x84 ,
] ,
output_str : " e4fdcb11d1ac14e698743acd8805174cea5ddc0d312e3e47f6372032571bad84 " ,
} ,
Test {
input : " Discard medicine more than two years old. " ,
output : vec ! [
0x69 , 0x0c , 0x8a , 0xd3 , 0x91 , 0x6c , 0xef , 0xd3 ,
0xad , 0x29 , 0x22 , 0x6d , 0x98 , 0x75 , 0x96 , 0x5e ,
0x3e , 0xe9 , 0xec , 0x0d , 0x44 , 0x82 , 0xea , 0xcc ,
0x24 , 0x8f , 0x2f , 0xf4 , 0xaa , 0x0d , 0x8e , 0x5b ,
] ,
output_str : " 690c8ad3916cefd3ad29226d9875965e3ee9ec0d4482eacc248f2ff4aa0d8e5b " ,
} ,
Test {
input : " There is no reason for any individual to have a computer in their home. -Ken Olsen, 1977 " ,
output : vec ! [
0xb5 , 0xba , 0xf7 , 0x47 , 0xc3 , 0x07 , 0xf9 , 0x88 ,
0x49 , 0xec , 0x88 , 0x1c , 0xf0 , 0xd4 , 0x86 , 0x05 ,
0xae , 0x4e , 0xdd , 0x38 , 0x63 , 0x72 , 0xae , 0xa9 ,
0xb2 , 0x6e , 0x71 , 0xdb , 0x51 , 0x7e , 0x65 , 0x0b ,
] ,
output_str : " b5baf747c307f98849ec881cf0d48605ae4edd386372aea9b26e71db517e650b " ,
} ,
Test {
input : " The major problem is with sendmail. -Mark Horton " ,
output : vec ! [
0x53 , 0xed , 0x5f , 0x9b , 0x5c , 0x0b , 0x67 , 0x4a ,
0xc0 , 0xf3 , 0x42 , 0x5d , 0x9f , 0x9a , 0x5d , 0x46 ,
0x26 , 0x55 , 0xb0 , 0x7c , 0xc9 , 0x0f , 0x5d , 0x0f ,
0x69 , 0x2e , 0xec , 0x09 , 0x38 , 0x84 , 0xa6 , 0x07 ,
] ,
output_str : " 53ed5f9b5c0b674ac0f3425d9f9a5d462655b07cc90f5d0f692eec093884a607 " ,
} ,
] ;
for test in tests {
// Hash through high-level API, check hex encoding/decoding
let hash = sha512_256 ::Hash ::hash ( test . input . as_bytes ( ) ) ;
2023-01-20 00:24:48 +00:00
assert_eq! ( hash , test . output_str . parse ::< sha512_256 ::Hash > ( ) . expect ( " parse hex " ) ) ;
2022-11-23 07:48:57 +00:00
assert_eq! ( & hash [ .. ] , & test . output [ .. ] ) ;
2023-01-07 15:39:11 +00:00
assert_eq! ( & hash . to_string ( ) , & test . output_str ) ;
2022-11-23 07:48:57 +00:00
// Hash through engine, checking that we can input byte by byte
let mut engine = sha512_256 ::Hash ::engine ( ) ;
for ch in test . input . as_bytes ( ) {
engine . 0. input ( & [ * ch ] ) ;
}
let manual_hash = sha512_256 ::Hash ::from_engine ( engine ) ;
assert_eq! ( hash , manual_hash ) ;
2023-01-28 22:47:24 +00:00
assert_eq! ( hash . to_byte_array ( ) [ .. ] . as_ref ( ) , test . output . as_slice ( ) ) ;
2022-11-23 07:48:57 +00:00
}
}
}
#[ cfg(bench) ]
mod benches {
use test ::Bencher ;
2023-02-21 23:25:12 +00:00
use crate ::{ sha512_256 , Hash , HashEngine } ;
2022-11-23 07:48:57 +00:00
#[ bench ]
pub fn sha512_256_10 ( bh : & mut Bencher ) {
let mut engine = sha512_256 ::Hash ::engine ( ) ;
let bytes = [ 1 u8 ; 10 ] ;
2023-02-21 23:25:12 +00:00
bh . iter ( | | {
2022-11-23 07:48:57 +00:00
engine . input ( & bytes ) ;
} ) ;
bh . bytes = bytes . len ( ) as u64 ;
}
#[ bench ]
pub fn sha512_256_1k ( bh : & mut Bencher ) {
let mut engine = sha512_256 ::Hash ::engine ( ) ;
let bytes = [ 1 u8 ; 1024 ] ;
2023-02-21 23:25:12 +00:00
bh . iter ( | | {
2022-11-23 07:48:57 +00:00
engine . input ( & bytes ) ;
} ) ;
bh . bytes = bytes . len ( ) as u64 ;
}
#[ bench ]
pub fn sha512_256_64k ( bh : & mut Bencher ) {
let mut engine = sha512_256 ::Hash ::engine ( ) ;
let bytes = [ 1 u8 ; 65536 ] ;
2023-02-21 23:25:12 +00:00
bh . iter ( | | {
2022-11-23 07:48:57 +00:00
engine . input ( & bytes ) ;
} ) ;
bh . bytes = bytes . len ( ) as u64 ;
}
}