11#![ no_std]
22#![ cfg_attr( docsrs, feature( doc_auto_cfg) ) ]
3- #[ cfg( all( feature = "sha2" , not( target_os = "solana" ) ) ) ]
3+
4+ #[ cfg( all( feature = "sha2" , not( any( target_os = "solana" , target_arch = "bpf" ) ) ) ) ]
45use sha2:: { Digest , Sha256 } ;
5- use solana_hash:: Hash ;
6+ use solana_hash:: { Hash , HASH_BYTES } ;
7+ #[ cfg( any( target_os = "solana" , target_arch = "bpf" ) ) ]
8+ pub use { core:: mem:: MaybeUninit , solana_define_syscall:: definitions:: sol_sha256} ;
69
7- #[ cfg( all( feature = "sha2" , not( target_os = "solana" ) ) ) ]
10+ #[ cfg( all( feature = "sha2" , not( any ( target_os = "solana" , target_arch = "bpf" ) ) ) ) ]
811#[ derive( Clone , Default ) ]
912pub struct Hasher {
1013 hasher : Sha256 ,
1114}
1215
13- #[ cfg( all( feature = "sha2" , not( target_os = "solana" ) ) ) ]
16+ #[ cfg( all( feature = "sha2" , not( any ( target_os = "solana" , target_arch = "bpf" ) ) ) ) ]
1417impl Hasher {
1518 pub fn hash ( & mut self , val : & [ u8 ] ) {
1619 self . hasher . update ( val) ;
@@ -21,19 +24,16 @@ impl Hasher {
2124 }
2225 }
2326 pub fn result ( self ) -> Hash {
24- let bytes: [ u8 ; solana_hash :: HASH_BYTES ] = self . hasher . finalize ( ) . into ( ) ;
27+ let bytes: [ u8 ; HASH_BYTES ] = self . hasher . finalize ( ) . into ( ) ;
2528 bytes. into ( )
2629 }
2730}
2831
29- #[ cfg( target_os = "solana" ) ]
30- pub use solana_define_syscall:: definitions:: sol_sha256;
31-
3232/// Return a Sha256 hash for the given data.
3333pub fn hashv ( vals : & [ & [ u8 ] ] ) -> Hash {
3434 // Perform the calculation inline, calling this from within a program is
3535 // not supported
36- #[ cfg( not( target_os = "solana" ) ) ]
36+ #[ cfg( not( any ( target_os = "solana" , target_arch = "bpf" ) ) ) ]
3737 {
3838 #[ cfg( feature = "sha2" ) ]
3939 {
@@ -48,17 +48,18 @@ pub fn hashv(vals: &[&[u8]]) -> Hash {
4848 }
4949 }
5050 // Call via a system call to perform the calculation
51- #[ cfg( target_os = "solana" ) ]
51+ #[ cfg( any ( target_os = "solana" , target_arch = "bpf" ) ) ]
5252 {
53- let mut hash_result = [ 0 ; solana_hash :: HASH_BYTES ] ;
53+ let mut hash_result = MaybeUninit :: < [ u8 ; HASH_BYTES ] > :: uninit ( ) ;
5454 unsafe {
5555 sol_sha256 (
5656 vals as * const _ as * const u8 ,
5757 vals. len ( ) as u64 ,
5858 & mut hash_result as * mut _ as * mut u8 ,
5959 ) ;
6060 }
61- Hash :: new_from_array ( hash_result)
61+ // SAFETY: the syscall initializes the `hash_result`.
62+ Hash :: new_from_array ( unsafe { hash_result. assume_init ( ) } )
6263 }
6364}
6465
0 commit comments