Skip to content

Commit b12cc59

Browse files
committed
crypto: use RustCrypto utils over deprecated rust-crypto
This change started due to my unfortunate exposure to a bug impacting the ability for my project to target aarch64: DaGenix/rust-crypto#383. In that issue (and in others) it is clear that rust-crypto is now deprecated in favor of the RustCrypto crates (here we need sha2/hmac). This change drops in this replacement for rust-crypto. Signed-off-by: Paul Osborne <paul.osborne@smartthings.com>
1 parent 6846050 commit b12cc59

File tree

4 files changed

+21
-26
lines changed

4 files changed

+21
-26
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ chrono = "0.2.0"
1818
curl = "0.4.0"
1919
error-chain = "0.10"
2020
hex = "0.2.0"
21-
rust-crypto = "0.2.0"
21+
hmac = "0.1"
2222
serde_derive = "0.9"
2323
serde = "0.9"
2424
serde-xml-rs = "0.1.2"
2525
#serde_xml = "0.9"
26+
sha2 = "0.5"
2627
url = "1.2.4"
2728

src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
//! Simple access to Amazon Web Service's (AWS) Simple Storage Service (S3)
22
extern crate chrono;
3-
extern crate crypto;
43
extern crate curl;
54
#[macro_use]
65
extern crate error_chain;
76
extern crate hex;
7+
extern crate hmac;
88
#[macro_use]
99
extern crate serde_derive;
1010
extern crate serde;
1111
extern crate serde_xml_rs as serde_xml;
12+
extern crate sha2;
1213
extern crate url;
1314

1415

@@ -23,4 +24,4 @@ pub mod signing;
2324

2425
const LONG_DATE: &'static str = "%Y%m%dT%H%M%SZ";
2526
const EMPTY_PAYLOAD_SHA: &'static str = "e3b0c44298fc1c149afbf4c8996fb924\
26-
27ae41e4649b934ca495991b7852b855";
27+
27ae41e4649b934ca495991b7852b855";

src/request.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ use bucket::Bucket;
55
use chrono::{DateTime, UTC};
66
use command::Command;
77

8-
use crypto::digest::Digest;
9-
use crypto::hmac::Hmac;
10-
use crypto::mac::Mac;
11-
use crypto::sha2::Sha256;
8+
use hmac::{Hmac, Mac};
9+
use sha2::{Digest, Sha256};
1210
use curl::easy::{Easy, List, ReadError};
1311
use error::S3Result;
1412
use hex::ToHex;
@@ -90,9 +88,9 @@ impl<'a> Request<'a> {
9088
fn sha256(&self) -> String {
9189
match self.command {
9290
Command::Put { content, .. } => {
93-
let mut sha = Sha256::new();
91+
let mut sha = Sha256::default();
9492
sha.input(content);
95-
sha.result_str()
93+
sha.result().as_slice().to_hex()
9694
}
9795
_ => EMPTY_PAYLOAD_SHA.into(),
9896
}
@@ -123,7 +121,7 @@ impl<'a> Request<'a> {
123121
fn authorization(&self, headers: &Headers) -> String {
124122
let canonical_request = self.canonical_request(headers);
125123
let string_to_sign = self.string_to_sign(&canonical_request);
126-
let mut hmac = Hmac::new(Sha256::new(), &self.signing_key());
124+
let mut hmac = Hmac::<Sha256>::new(&self.signing_key());
127125
hmac.input(string_to_sign.as_bytes());
128126
let signature = hmac.result().code().to_hex();
129127
let signed_header = signing::signed_header_string(headers);
@@ -210,4 +208,4 @@ impl<'a> Request<'a> {
210208
}
211209
Ok((dst, handle.response_code()?))
212210
}
213-
}
211+
}

src/signing.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@
55
use std::str;
66

77
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};
1210
use url::Url;
1311
use region::Region;
1412
use request::Headers;
13+
use sha2::{Digest, Sha256};
1514

1615
const SHORT_DATE: &'static str = "%Y%m%d";
1716
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 {
8786
/// Generate the "string to sign" - the value to which the HMAC signing is
8887
/// applied to sign requests.
8988
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();
9190
hasher.input(canonical_req.as_bytes());
9291
format!("AWS4-HMAC-SHA256\n{timestamp}\n{scope}\n{hash}",
9392
timestamp = datetime.format(LONG_DATETIME),
9493
scope = scope_string(datetime, region),
95-
hash = hasher.result_str())
94+
hash = hasher.result().as_slice().to_hex())
9695
}
9796

9897
/// Generate the AWS signing key, derived from the secret key, date, region,
@@ -102,15 +101,14 @@ pub fn signing_key(datetime: &DateTime<UTC>,
102101
region: Region,
103102
service: &str)
104103
-> Vec<u8> {
105-
let sha256 = Sha256::new();
106104
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());
108106
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());
110108
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());
112110
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());
114112
signing_hmac.input("aws4_request".as_bytes());
115113
signing_hmac.result().code().into()
116114
}
@@ -135,9 +133,6 @@ mod tests {
135133
use std::str;
136134

137135
use chrono::{TimeZone, UTC};
138-
use crypto::hmac::Hmac;
139-
use crypto::mac::Mac;
140-
use crypto::sha2::Sha256;
141136
use hex::ToHex;
142137
use url::Url;
143138

@@ -238,7 +233,7 @@ mod tests {
238233
let expected = "f0e8bdb87c964420e857bd35b5d6ed310bd44f0170aba48dd91039c6036bdb41";
239234
let secret = "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY";
240235
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);
242237
hmac.input(string_to_sign.as_bytes());
243238
assert_eq!(expected, hmac.result().code().to_hex());
244239
}

0 commit comments

Comments
 (0)