11//! Base64 encoding support
22
3- use crate :: consts:: {
4- BLOCK_SIZE_SHA256 , BLOCK_SIZE_SHA512 , MAP_SHA256 , MAP_SHA512 , PW_SIZE_SHA256 , PW_SIZE_SHA512 ,
3+ #![ cfg( feature = "simple" ) ]
4+
5+ use crate :: {
6+ consts:: { BLOCK_SIZE_SHA256 , BLOCK_SIZE_SHA512 , MAP_SHA256 , MAP_SHA512 , PW_SIZE_SHA256 } ,
7+ errors:: DecodeError ,
58} ;
69use base64ct:: { Base64ShaCrypt , Encoding } ;
710
8- #[ cfg( feature = "simple" ) ]
9- use crate :: errors:: DecodeError ;
10-
11- pub fn encode_sha512 ( source : & [ u8 ] ) -> [ u8 ; PW_SIZE_SHA512 ] {
12- let mut transposed = [ 0u8 ; BLOCK_SIZE_SHA512 ] ;
13- for ( i, & ti) in MAP_SHA512 . iter ( ) . enumerate ( ) {
14- transposed[ i] = source[ ti as usize ] ;
15- }
16- let mut buf = [ 0u8 ; PW_SIZE_SHA512 ] ;
17- Base64ShaCrypt :: encode ( & transposed, & mut buf) . unwrap ( ) ;
18- buf
19- }
20-
21- pub fn encode_sha256 ( source : & [ u8 ] ) -> [ u8 ; PW_SIZE_SHA256 ] {
22- let mut transposed = [ 0u8 ; BLOCK_SIZE_SHA256 ] ;
23- for ( i, & ti) in MAP_SHA256 . iter ( ) . enumerate ( ) {
24- transposed[ i] = source[ ti as usize ] ;
25- }
26- let mut buf = [ 0u8 ; PW_SIZE_SHA256 ] ;
27- Base64ShaCrypt :: encode ( & transposed, & mut buf) . unwrap ( ) ;
28- buf
29- }
30-
31- #[ cfg( feature = "simple" ) ]
3211pub fn decode_sha512 ( source : & [ u8 ] ) -> Result < [ u8 ; BLOCK_SIZE_SHA512 ] , DecodeError > {
3312 const BUF_SIZE : usize = 86 ;
3413 let mut buf = [ 0u8 ; BUF_SIZE ] ;
@@ -40,7 +19,6 @@ pub fn decode_sha512(source: &[u8]) -> Result<[u8; BLOCK_SIZE_SHA512], DecodeErr
4019 Ok ( transposed)
4120}
4221
43- #[ cfg( feature = "simple" ) ]
4422pub fn decode_sha256 ( source : & [ u8 ] ) -> Result < [ u8 ; BLOCK_SIZE_SHA256 ] , DecodeError > {
4523 let mut buf = [ 0u8 ; PW_SIZE_SHA256 ] ;
4624 Base64ShaCrypt :: decode ( source, & mut buf) . unwrap ( ) ;
@@ -51,41 +29,3 @@ pub fn decode_sha256(source: &[u8]) -> Result<[u8; BLOCK_SIZE_SHA256], DecodeErr
5129 }
5230 Ok ( transposed)
5331}
54-
55- mod tests {
56- #[ cfg( feature = "simple" ) ]
57- #[ test]
58- fn test_encode_decode_sha512 ( ) {
59- let original: [ u8 ; 64 ] = [
60- 0x0b , 0x5b , 0xdf , 0x7d , 0x92 , 0xe2 , 0xfc , 0xbd , 0xab , 0x57 , 0xcb , 0xf3 , 0xe0 , 0x03 ,
61- 0x16 , 0x62 , 0xd3 , 0x6e , 0xa0 , 0x57 , 0x44 , 0x8c , 0xca , 0x35 , 0xec , 0x80 , 0x75 , 0x2a ,
62- 0x37 , 0xd4 , 0xe6 , 0xfa , 0xf7 , 0xd7 , 0x78 , 0xf4 , 0x8e , 0x0b , 0x3e , 0xab , 0x23 , 0x05 ,
63- 0x15 , 0xdd , 0x79 , 0x14 , 0x45 , 0xac , 0x66 , 0x60 , 0x25 , 0x94 , 0x97 , 0x5e , 0x0f , 0x7f ,
64- 0x5f , 0xaf , 0x1a , 0xe5 , 0x08 , 0xe7 , 0x7d , 0xd4 ,
65- ] ;
66-
67- let e = super :: encode_sha512 ( & original) ;
68- let d = super :: decode_sha512 ( & e) . unwrap ( ) ;
69-
70- for i in 0 ..d. len ( ) {
71- assert_eq ! ( & original[ i] , & d[ i] ) ;
72- }
73- }
74-
75- #[ cfg( feature = "simple" ) ]
76- #[ test]
77- fn test_encode_decode_sha256 ( ) {
78- let original: [ u8 ; 32 ] = [
79- 0x0b , 0x5b , 0xdf , 0x7d , 0x92 , 0xe2 , 0xfc , 0xbd , 0xab , 0x57 , 0xcb , 0xf3 , 0xe0 , 0x03 ,
80- 0x16 , 0x62 , 0xd3 , 0x6e , 0xa0 , 0x57 , 0x44 , 0x8c , 0xca , 0x35 , 0xec , 0x80 , 0x75 , 0x2a ,
81- 0x5f , 0xaf , 0x1a , 0xe5 ,
82- ] ;
83-
84- let e = super :: encode_sha256 ( & original) ;
85- let d = super :: decode_sha256 ( & e) . unwrap ( ) ;
86-
87- for i in 0 ..d. len ( ) {
88- assert_eq ! ( & original[ i] , & d[ i] ) ;
89- }
90- }
91- }
0 commit comments