5
5
use std:: str;
6
6
7
7
use chrono:: { DateTime , UTC } ;
8
- use crypto:: digest:: Digest ;
9
- use crypto:: hmac:: Hmac ;
10
- use crypto:: mac:: Mac ;
11
- use crypto:: sha2:: Sha256 ;
8
+ use hex:: ToHex ;
9
+ use hmac:: { Hmac , Mac } ;
12
10
use url:: Url ;
13
11
use region:: Region ;
14
12
use request:: Headers ;
13
+ use sha2:: { Digest , Sha256 } ;
15
14
16
15
const SHORT_DATE : & ' static str = "%Y%m%d" ;
17
16
const LONG_DATETIME : & ' static str = "%Y%m%dT%H%M%SZ" ;
@@ -87,12 +86,12 @@ pub fn scope_string(datetime: &DateTime<UTC>, region: Region) -> String {
87
86
/// Generate the "string to sign" - the value to which the HMAC signing is
88
87
/// applied to sign requests.
89
88
pub fn string_to_sign ( datetime : & DateTime < UTC > , region : Region , canonical_req : & str ) -> String {
90
- let mut hasher = Sha256 :: new ( ) ;
89
+ let mut hasher = Sha256 :: default ( ) ;
91
90
hasher. input ( canonical_req. as_bytes ( ) ) ;
92
91
format ! ( "AWS4-HMAC-SHA256\n {timestamp}\n {scope}\n {hash}" ,
93
92
timestamp = datetime. format( LONG_DATETIME ) ,
94
93
scope = scope_string( datetime, region) ,
95
- hash = hasher. result_str ( ) )
94
+ hash = hasher. result ( ) . as_slice ( ) . to_hex ( ) )
96
95
}
97
96
98
97
/// Generate the AWS signing key, derived from the secret key, date, region,
@@ -102,15 +101,14 @@ pub fn signing_key(datetime: &DateTime<UTC>,
102
101
region : Region ,
103
102
service : & str )
104
103
-> Vec < u8 > {
105
- let sha256 = Sha256 :: new ( ) ;
106
104
let secret = String :: from ( "AWS4" ) + secret_key;
107
- let mut date_hmac = Hmac :: new ( sha256 , secret. as_bytes ( ) ) ;
105
+ let mut date_hmac = Hmac :: < Sha256 > :: new ( secret. as_bytes ( ) ) ;
108
106
date_hmac. input ( datetime. format ( SHORT_DATE ) . to_string ( ) . as_bytes ( ) ) ;
109
- let mut region_hmac = Hmac :: new ( sha256 , & date_hmac. result ( ) . code ( ) ) ;
107
+ let mut region_hmac = Hmac :: < Sha256 > :: new ( & date_hmac. result ( ) . code ( ) ) ;
110
108
region_hmac. input ( region. to_string ( ) . as_bytes ( ) ) ;
111
- let mut service_hmac = Hmac :: new ( sha256 , & region_hmac. result ( ) . code ( ) ) ;
109
+ let mut service_hmac = Hmac :: < Sha256 > :: new ( & region_hmac. result ( ) . code ( ) ) ;
112
110
service_hmac. input ( service. as_bytes ( ) ) ;
113
- let mut signing_hmac = Hmac :: new ( sha256 , & service_hmac. result ( ) . code ( ) ) ;
111
+ let mut signing_hmac = Hmac :: < Sha256 > :: new ( & service_hmac. result ( ) . code ( ) ) ;
114
112
signing_hmac. input ( "aws4_request" . as_bytes ( ) ) ;
115
113
signing_hmac. result ( ) . code ( ) . into ( )
116
114
}
@@ -135,9 +133,6 @@ mod tests {
135
133
use std:: str;
136
134
137
135
use chrono:: { TimeZone , UTC } ;
138
- use crypto:: hmac:: Hmac ;
139
- use crypto:: mac:: Mac ;
140
- use crypto:: sha2:: Sha256 ;
141
136
use hex:: ToHex ;
142
137
use url:: Url ;
143
138
@@ -238,7 +233,7 @@ mod tests {
238
233
let expected = "f0e8bdb87c964420e857bd35b5d6ed310bd44f0170aba48dd91039c6036bdb41" ;
239
234
let secret = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY" ;
240
235
let signing_key = signing_key ( & datetime, secret, "us-east-1" . parse ( ) . unwrap ( ) , "s3" ) ;
241
- let mut hmac = Hmac :: new ( Sha256 :: new ( ) , & signing_key) ;
236
+ let mut hmac = Hmac :: < Sha256 > :: new ( & signing_key) ;
242
237
hmac. input ( string_to_sign. as_bytes ( ) ) ;
243
238
assert_eq ! ( expected, hmac. result( ) . code( ) . to_hex( ) ) ;
244
239
}
0 commit comments