use sha-1/hmac

This commit is contained in:
2020-04-04 23:34:42 +08:00
parent 6e116f7003
commit b1a9e6f8f2
7 changed files with 111 additions and 266 deletions

View File

@@ -2,17 +2,12 @@ use std::{
fs::File,
time::SystemTime,
};
use crypto::{
mac::{
Mac,
MacResult,
},
hmac::Hmac,
sha1::Sha1,
};
use reqwest::{
Response,
use sha1::Sha1;
use hmac::{
Hmac,
Mac,
};
use reqwest::Response;
use rust_util::*;
pub const DEFAULT_URL_VALID_IN_SECS: u64 = 1000;
@@ -101,7 +96,7 @@ impl OSSClient {
signed_url.push_str("&Signature=");
let to_be_signed = get_to_be_signed(verb, expire_secs, bucket_name, key);
let signature = to_base64(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.as_str()));
signed_url
@@ -134,14 +129,14 @@ fn get_to_be_signed(verb: &str, expire_secs: u64, bucket_name: &str, key: &str)
to_be_signed
}
fn to_base64(mac_result: MacResult) -> String {
base64::encode(mac_result.code())
}
fn calc_hmac_sha1(key: &[u8], message: &[u8]) -> MacResult {
let mut hmac = Hmac::new(Sha1::new(), key);
hmac.input(message);
hmac.result()
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),
}
}
fn get_current_secs() -> u64 {