From 44eaac8471a6c253b359a4ca06c5fb23b5bc4985 Mon Sep 17 00:00:00 2001 From: Andrew Gaul Date: Mon, 14 Sep 2020 19:09:48 +0900 Subject: [PATCH] Centralize hex conversion --- src/common_auth.cpp | 8 ++------ src/curl.cpp | 25 +++++-------------------- src/test_util.h | 20 ++++---------------- 3 files changed, 11 insertions(+), 42 deletions(-) diff --git a/src/common_auth.cpp b/src/common_auth.cpp index 2c81fbe..dcf31c8 100644 --- a/src/common_auth.cpp +++ b/src/common_auth.cpp @@ -70,20 +70,16 @@ std::string s3fs_md5sum(int fd, off_t start, off_t size) std::string s3fs_sha256sum(int fd, off_t start, off_t size) { size_t digestlen = get_sha256_digest_length(); - char sha256[2 * digestlen + 1]; unsigned char* sha256hex; if(NULL == (sha256hex = s3fs_sha256hexsum(fd, start, size))){ return std::string(""); } - memset(sha256, 0, 2 * digestlen + 1); - for(size_t pos = 0; pos < digestlen; pos++){ - snprintf(sha256 + 2 * pos, 3, "%02x", sha256hex[pos]); - } + std::string sha256 = s3fs_hex(sha256hex, digestlen); delete[] sha256hex; - return std::string(sha256); + return sha256; } /* diff --git a/src/curl.cpp b/src/curl.cpp index 11503e0..f88fccd 100644 --- a/src/curl.cpp +++ b/src/curl.cpp @@ -2492,9 +2492,7 @@ std::string S3fsCurl::CalcSignature(const std::string& method, const std::string char kSecret[128]; unsigned char *kDate, *kRegion, *kService, *kSigning, *sRequest = NULL; unsigned int kDate_len,kRegion_len, kService_len, kSigning_len, sRequest_len = 0; - char hexsRequest[64 + 1]; int kSecret_len = snprintf(kSecret, sizeof(kSecret), "AWS4%s", S3fsCurl::AWSSecretAccessKey.c_str()); - unsigned int cnt; s3fs_HMAC256(kSecret, kSecret_len, reinterpret_cast(strdate.data()), strdate.size(), &kDate, &kDate_len); s3fs_HMAC256(kDate, kDate_len, reinterpret_cast(endpoint.c_str()), endpoint.size(), &kRegion, &kRegion_len); @@ -2507,15 +2505,12 @@ std::string S3fsCurl::CalcSignature(const std::string& method, const std::string const unsigned char* cRequest = reinterpret_cast(StringCQ.c_str()); unsigned int cRequest_len = StringCQ.size(); s3fs_sha256(cRequest, cRequest_len, &sRequest, &sRequest_len); - for(cnt = 0; cnt < sRequest_len; cnt++){ - sprintf(&hexsRequest[cnt * 2], "%02x", sRequest[cnt]); - } - delete[] sRequest; StringToSign = "AWS4-HMAC-SHA256\n"; StringToSign += date8601 + "\n"; StringToSign += strdate + "/" + endpoint + "/s3/aws4_request\n"; - StringToSign += hexsRequest; + StringToSign += s3fs_hex(sRequest, sRequest_len); + delete[] sRequest; const unsigned char* cscope = reinterpret_cast(StringToSign.c_str()); unsigned int cscope_len = StringToSign.size(); @@ -2523,15 +2518,10 @@ std::string S3fsCurl::CalcSignature(const std::string& method, const std::string unsigned int md_len = 0; s3fs_HMAC256(kSigning, kSigning_len, cscope, cscope_len, &md, &md_len); - char *hexSig = new char[2 * md_len + 1]; - for(cnt = 0; cnt < md_len; cnt++){ - sprintf(&hexSig[cnt * 2], "%02x", md[cnt]); - } delete[] kSigning; - delete[] md; - Signature = hexSig; - delete[] hexSig; + Signature = s3fs_hex(md, md_len); + delete[] md; return Signature; } @@ -2550,14 +2540,9 @@ void S3fsCurl::insertV4Headers() unsigned int cRequest_len = strlen(reinterpret_cast(b_postdata)); unsigned char* sRequest = NULL; unsigned int sRequest_len = 0; - char hexsRequest[64 + 1]; - unsigned int cnt; s3fs_sha256(b_postdata, cRequest_len, &sRequest, &sRequest_len); - for(cnt = 0; cnt < sRequest_len; cnt++){ - sprintf(&hexsRequest[cnt * 2], "%02x", sRequest[cnt]); - } + payload_hash = s3fs_hex(sRequest, sRequest_len); delete[] sRequest; - payload_hash.assign(hexsRequest, &hexsRequest[sRequest_len * 2]); break; } diff --git a/src/test_util.h b/src/test_util.h index ea4ec4c..0ce2413 100644 --- a/src/test_util.h +++ b/src/test_util.h @@ -38,14 +38,8 @@ template <> void assert_equals(const std::string &x, const std::string &y, const { if (x != y) { std::cerr << x << " != " << y << " at " << file << ":" << line << std::endl; - for (unsigned i=0; i(x.c_str()), x.size()) << std::endl; + std::cerr << s3fs_hex(reinterpret_cast(y.c_str()), y.size()) << std::endl; std::exit(1); } } @@ -63,14 +57,8 @@ template <> void assert_nequals(const std::string &x, const std::string &y, cons { if (x == y) { std::cerr << x << " == " << y << " at " << file << ":" << line << std::endl; - for (unsigned i=0; i(x.c_str()), x.size()) << std::endl; + std::cerr << s3fs_hex(reinterpret_cast(y.c_str()), y.size()) << std::endl; std::exit(1); } }