ref hmac/signed url
This commit is contained in:
@@ -85,15 +85,12 @@ impl OSSClient {
|
|||||||
let current_secs = get_current_secs();
|
let current_secs = get_current_secs();
|
||||||
let expire_secs = current_secs + expire_in_seconds;
|
let expire_secs = current_secs + expire_in_seconds;
|
||||||
|
|
||||||
signed_url.push_str("?Expires=");
|
signed_url.push_str(&format!("?Expires={}", &expire_secs.to_string()));
|
||||||
signed_url.push_str(&expire_secs.to_string());
|
signed_url.push_str(&format!("&OSSAccessKeyId={}", &urlencoding::encode(&self.access_key_id)));
|
||||||
signed_url.push_str("&OSSAccessKeyId=");
|
|
||||||
signed_url.push_str(&urlencoding::encode(&self.access_key_id));
|
|
||||||
signed_url.push_str("&Signature=");
|
|
||||||
|
|
||||||
let to_be_signed = get_to_be_signed(verb, expire_secs, bucket_name, key);
|
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());
|
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
|
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 {
|
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);
|
let mut to_be_signed = String::with_capacity(512);
|
||||||
to_be_signed.push_str(verb);
|
to_be_signed.push_str(verb);
|
||||||
to_be_signed.push_str("\n");
|
to_be_signed.push_str("\n\n\n");
|
||||||
to_be_signed.push_str("\n");
|
|
||||||
to_be_signed.push_str("\n");
|
|
||||||
to_be_signed.push_str(&expire_secs.to_string());
|
to_be_signed.push_str(&expire_secs.to_string());
|
||||||
to_be_signed.push_str("\n");
|
to_be_signed.push_str("\n");
|
||||||
to_be_signed.push_str("/");
|
to_be_signed.push_str(&format!("/{}/{}", bucket_name, key));
|
||||||
to_be_signed.push_str(bucket_name);
|
|
||||||
to_be_signed.push_str("/");
|
|
||||||
to_be_signed.push_str(key);
|
|
||||||
to_be_signed
|
to_be_signed
|
||||||
}
|
}
|
||||||
|
|
||||||
fn calc_hmac_sha1(key: &[u8], message: &[u8]) -> String {
|
fn calc_hmac_sha1(key: &[u8], message: &[u8]) -> String {
|
||||||
match Hmac::<Sha1>::new_varkey(key) {
|
Hmac::<Sha1>::new_varkey(key).map(|mut mac| {
|
||||||
Ok(mut mac) => {
|
mac.input(message);
|
||||||
mac.input(message);
|
base64::encode(&mac.result().code())
|
||||||
base64::encode(&mac.result().code())
|
}).unwrap_or_else(|e| format!("[ERROR]Hmac error: {}", e))
|
||||||
},
|
|
||||||
Err(e) => format!("[ERROR]Hmac error: {}", e),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user