ref hmac/signed url

This commit is contained in:
2020-04-12 15:25:58 +08:00
parent 9945bfee8a
commit 2062140200

View File

@@ -85,15 +85,12 @@ impl OSSClient {
let current_secs = get_current_secs();
let expire_secs = current_secs + expire_in_seconds;
signed_url.push_str("?Expires=");
signed_url.push_str(&expire_secs.to_string());
signed_url.push_str("&OSSAccessKeyId=");
signed_url.push_str(&urlencoding::encode(&self.access_key_id));
signed_url.push_str("&Signature=");
signed_url.push_str(&format!("?Expires={}", &expire_secs.to_string()));
signed_url.push_str(&format!("&OSSAccessKeyId={}", &urlencoding::encode(&self.access_key_id)));
let to_be_signed = get_to_be_signed(verb, expire_secs, bucket_name, key);
let signature = calc_hmac_sha1(self.access_key_secret.as_bytes(), to_be_signed.as_bytes());
signed_url.push_str(&urlencoding::encode(&signature));
signed_url.push_str(&format!("&Signature={}", &urlencoding::encode(&signature)));
signed_url
}
@@ -113,24 +110,16 @@ fn remove_endpoint_http_or_s(endpoint: &str) -> String {
fn get_to_be_signed(verb: &str, expire_secs: u64, bucket_name: &str, key: &str) -> String {
let mut to_be_signed = String::with_capacity(512);
to_be_signed.push_str(verb);
to_be_signed.push_str("\n");
to_be_signed.push_str("\n");
to_be_signed.push_str("\n");
to_be_signed.push_str("\n\n\n");
to_be_signed.push_str(&expire_secs.to_string());
to_be_signed.push_str("\n");
to_be_signed.push_str("/");
to_be_signed.push_str(bucket_name);
to_be_signed.push_str("/");
to_be_signed.push_str(key);
to_be_signed.push_str(&format!("/{}/{}", bucket_name, key));
to_be_signed
}
fn calc_hmac_sha1(key: &[u8], message: &[u8]) -> String {
match Hmac::<Sha1>::new_varkey(key) {
Ok(mut mac) => {
mac.input(message);
base64::encode(&mac.result().code())
},
Err(e) => format!("[ERROR]Hmac error: {}", e),
}
Hmac::<Sha1>::new_varkey(key).map(|mut mac| {
mac.input(message);
base64::encode(&mac.result().code())
}).unwrap_or_else(|e| format!("[ERROR]Hmac error: {}", e))
}