From fb33402844be8aaeedcbcec645b8be9c69fa6242 Mon Sep 17 00:00:00 2001 From: Ahson Khan Date: Sat, 9 Jan 2021 00:12:06 -0800 Subject: [PATCH] Remove Base64 encode and decode methods from Azure::Storage and move the string-based helper to Internal. Update storage call sites. (#1298) Follow up from https://github.com/Azure/azure-sdk-for-cpp/pull/1297 as the second step to fix #1020 --- sdk/core/azure-core/src/base64.cpp | 2 +- sdk/core/azure-core/test/ut/base64.cpp | 50 ++++++ .../blobs/protocol/blob_rest_client.hpp | 150 +++++++++--------- .../src/blob_sas_builder.cpp | 8 +- .../src/block_blob_client.cpp | 4 +- .../test/blob_container_client_test.cpp | 8 +- .../test/block_blob_client_test.cpp | 7 +- .../test/page_blob_client_test.cpp | 4 +- sdk/storage/azure-storage-common/CHANGELOG.md | 2 + .../inc/azure/storage/common/crypt.hpp | 14 +- .../src/account_sas_builder.cpp | 4 +- .../azure-storage-common/src/crypt.cpp | 70 -------- .../src/shared_key_policy.cpp | 4 +- .../src/storage_common.cpp | 7 +- .../test/crypt_functions_test.cpp | 27 +--- .../src/datalake_sas_builder.cpp | 8 +- .../src/datalake_utilities.cpp | 2 +- .../src/share_sas_builder.cpp | 4 +- 18 files changed, 176 insertions(+), 199 deletions(-) diff --git a/sdk/core/azure-core/src/base64.cpp b/sdk/core/azure-core/src/base64.cpp index 5ddb97131..fa8cac3c6 100644 --- a/sdk/core/azure-core/src/base64.cpp +++ b/sdk/core/azure-core/src/base64.cpp @@ -93,4 +93,4 @@ namespace Azure { namespace Core { #endif -}} // namespace Azure::Core \ No newline at end of file +}} // namespace Azure::Core diff --git a/sdk/core/azure-core/test/ut/base64.cpp b/sdk/core/azure-core/test/ut/base64.cpp index eb27e98bf..ae1ed9f9a 100644 --- a/sdk/core/azure-core/test/ut/base64.cpp +++ b/sdk/core/azure-core/test/ut/base64.cpp @@ -3,6 +3,7 @@ #include #include +#include #include #include @@ -61,3 +62,52 @@ TEST(Base64, Basic) [](std::string expect, std::string actual) { return expect == actual; }, result, "AQIDBAUG"); EXPECT_TRUE(std::equal(subsection.begin(), subsection.end(), Base64Decode(result).begin())); } + +static thread_local std::mt19937_64 random_generator(std::random_device{}()); + +static char RandomChar() +{ + const char charset[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + std::uniform_int_distribution distribution(0, sizeof(charset) - 2); + return charset[distribution(random_generator)]; +} + +void RandomBuffer(char* buffer, std::size_t length) +{ + char* start_addr = buffer; + char* end_addr = buffer + length; + + const std::size_t rand_int_size = sizeof(uint64_t); + + while (uintptr_t(start_addr) % rand_int_size != 0 && start_addr < end_addr) + { + *(start_addr++) = RandomChar(); + } + + std::uniform_int_distribution distribution(0ULL, std::numeric_limits::max()); + while (start_addr + rand_int_size <= end_addr) + { + *reinterpret_cast(start_addr) = distribution(random_generator); + start_addr += rand_int_size; + } + while (start_addr < end_addr) + { + *(start_addr++) = RandomChar(); + } +} + +inline void RandomBuffer(uint8_t* buffer, std::size_t length) +{ + RandomBuffer(reinterpret_cast(buffer), length); +} + +TEST(Base64, Roundtrip) +{ + for (std::size_t len : {0, 10, 100, 1000, 10000}) + { + std::vector data; + data.resize(len); + RandomBuffer(data.data(), data.size()); + EXPECT_EQ(Base64Decode(Base64Encode(data)), data); + } +} diff --git a/sdk/storage/azure-storage-blobs/inc/azure/storage/blobs/protocol/blob_rest_client.hpp b/sdk/storage/azure-storage-blobs/inc/azure/storage/blobs/protocol/blob_rest_client.hpp index 86df2fd5a..2163e41f0 100644 --- a/sdk/storage/azure-storage-blobs/inc/azure/storage/blobs/protocol/blob_rest_client.hpp +++ b/sdk/storage/azure-storage-blobs/inc/azure/storage/blobs/protocol/blob_rest_client.hpp @@ -4343,7 +4343,7 @@ namespace Azure { namespace Storage { namespace Blobs { path.size() == 2 && path[0] == XmlTagName::k_Properties && path[1] == XmlTagName::k_ContentMD5) { - ret.HttpHeaders.ContentHash.Value = Base64Decode(node.Value); + ret.HttpHeaders.ContentHash.Value = Azure::Core::Base64Decode(node.Value); } else if ( path.size() == 2 && path[0] == XmlTagName::k_Properties @@ -4443,7 +4443,7 @@ namespace Azure { namespace Storage { namespace Blobs { path.size() == 2 && path[0] == XmlTagName::k_Properties && path[1] == XmlTagName::k_EncryptionKeySHA256) { - ret.EncryptionKeySha256 = Base64Decode(node.Value); + ret.EncryptionKeySha256 = Azure::Core::Base64Decode(node.Value); } else if ( path.size() == 2 && path[0] == XmlTagName::k_Properties @@ -4788,7 +4788,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.EncryptionKeySha256.HasValue()) { request.AddHeader( - "x-ms-encryption-key-sha256", Base64Encode(options.EncryptionKeySha256.GetValue())); + "x-ms-encryption-key-sha256", Azure::Core::Base64Encode(options.EncryptionKeySha256.GetValue())); } if (options.EncryptionAlgorithm.HasValue()) { @@ -4847,7 +4847,7 @@ namespace Azure { namespace Storage { namespace Blobs { { ContentHash hash; hash.Algorithm = HashAlgorithm::Md5; - hash.Value = Base64Decode(content_md5_iterator->second); + hash.Value = Azure::Core::Base64Decode(content_md5_iterator->second); response.TransactionalContentHash = std::move(hash); } auto x_ms_content_crc64_iterator = headers.find("x-ms-content-crc64"); @@ -4855,7 +4855,7 @@ namespace Azure { namespace Storage { namespace Blobs { { ContentHash hash; hash.Algorithm = HashAlgorithm::Crc64; - hash.Value = Base64Decode(x_ms_content_crc64_iterator->second); + hash.Value = Azure::Core::Base64Decode(x_ms_content_crc64_iterator->second); response.TransactionalContentHash = std::move(hash); } } @@ -4882,14 +4882,14 @@ namespace Azure { namespace Storage { namespace Blobs { auto content_md5__iterator = httpResponse.GetHeaders().find("content-md5"); if (content_md5__iterator != httpResponse.GetHeaders().end()) { - response.HttpHeaders.ContentHash.Value = Base64Decode(content_md5__iterator->second); + response.HttpHeaders.ContentHash.Value = Azure::Core::Base64Decode(content_md5__iterator->second); } auto x_ms_blob_content_md5__iterator = httpResponse.GetHeaders().find("x-ms-blob-content-md5"); if (x_ms_blob_content_md5__iterator != httpResponse.GetHeaders().end()) { response.HttpHeaders.ContentHash.Value - = Base64Decode(x_ms_blob_content_md5__iterator->second); + = Azure::Core::Base64Decode(x_ms_blob_content_md5__iterator->second); } auto content_disposition__iterator = httpResponse.GetHeaders().find("content-disposition"); @@ -4910,7 +4910,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (x_ms_encryption_key_sha256__iterator != httpResponse.GetHeaders().end()) { response.EncryptionKeySha256 - = Base64Decode(x_ms_encryption_key_sha256__iterator->second); + = Azure::Core::Base64Decode(x_ms_encryption_key_sha256__iterator->second); } auto x_ms_encryption_scope__iterator = httpResponse.GetHeaders().find("x-ms-encryption-scope"); @@ -5220,7 +5220,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.EncryptionKeySha256.HasValue()) { request.AddHeader( - "x-ms-encryption-key-sha256", Base64Encode(options.EncryptionKeySha256.GetValue())); + "x-ms-encryption-key-sha256", Azure::Core::Base64Encode(options.EncryptionKeySha256.GetValue())); } if (options.EncryptionAlgorithm.HasValue()) { @@ -5335,14 +5335,14 @@ namespace Azure { namespace Storage { namespace Blobs { auto content_md5__iterator = httpResponse.GetHeaders().find("content-md5"); if (content_md5__iterator != httpResponse.GetHeaders().end()) { - response.HttpHeaders.ContentHash.Value = Base64Decode(content_md5__iterator->second); + response.HttpHeaders.ContentHash.Value = Azure::Core::Base64Decode(content_md5__iterator->second); } auto x_ms_blob_content_md5__iterator = httpResponse.GetHeaders().find("x-ms-blob-content-md5"); if (x_ms_blob_content_md5__iterator != httpResponse.GetHeaders().end()) { response.HttpHeaders.ContentHash.Value - = Base64Decode(x_ms_blob_content_md5__iterator->second); + = Azure::Core::Base64Decode(x_ms_blob_content_md5__iterator->second); } auto content_disposition__iterator = httpResponse.GetHeaders().find("content-disposition"); @@ -5375,7 +5375,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (x_ms_encryption_key_sha256__iterator != httpResponse.GetHeaders().end()) { response.EncryptionKeySha256 - = Base64Decode(x_ms_encryption_key_sha256__iterator->second); + = Azure::Core::Base64Decode(x_ms_encryption_key_sha256__iterator->second); } auto x_ms_encryption_scope__iterator = httpResponse.GetHeaders().find("x-ms-encryption-scope"); @@ -5523,10 +5523,10 @@ namespace Azure { namespace Storage { namespace Blobs { { request.AddHeader("x-ms-blob-cache-control", options.HttpHeaders.CacheControl); } - if (!Base64Encode(options.HttpHeaders.ContentHash.Value).empty()) + if (!Azure::Core::Base64Encode(options.HttpHeaders.ContentHash.Value).empty()) { request.AddHeader( - "x-ms-blob-content-md5", Base64Encode(options.HttpHeaders.ContentHash.Value)); + "x-ms-blob-content-md5", Azure::Core::Base64Encode(options.HttpHeaders.ContentHash.Value)); } if (!options.HttpHeaders.ContentDisposition.empty()) { @@ -5630,7 +5630,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.EncryptionKeySha256.HasValue()) { request.AddHeader( - "x-ms-encryption-key-sha256", Base64Encode(options.EncryptionKeySha256.GetValue())); + "x-ms-encryption-key-sha256", Azure::Core::Base64Encode(options.EncryptionKeySha256.GetValue())); } if (options.EncryptionAlgorithm.HasValue()) { @@ -5977,7 +5977,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.EncryptionKeySha256.HasValue()) { request.AddHeader( - "x-ms-encryption-key-sha256", Base64Encode(options.EncryptionKeySha256.GetValue())); + "x-ms-encryption-key-sha256", Azure::Core::Base64Encode(options.EncryptionKeySha256.GetValue())); } if (options.EncryptionAlgorithm.HasValue()) { @@ -6043,7 +6043,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (x_ms_encryption_key_sha256__iterator != httpResponse.GetHeaders().end()) { response.EncryptionKeySha256 - = Base64Decode(x_ms_encryption_key_sha256__iterator->second); + = Azure::Core::Base64Decode(x_ms_encryption_key_sha256__iterator->second); } auto x_ms_encryption_scope__iterator = httpResponse.GetHeaders().find("x-ms-encryption-scope"); @@ -6723,7 +6723,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.EncryptionKeySha256.HasValue()) { request.AddHeader( - "x-ms-encryption-key-sha256", Base64Encode(options.EncryptionKeySha256.GetValue())); + "x-ms-encryption-key-sha256", Azure::Core::Base64Encode(options.EncryptionKeySha256.GetValue())); } if (options.EncryptionAlgorithm.HasValue()) { @@ -6739,13 +6739,13 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.TransactionalContentHash.GetValue().Algorithm == HashAlgorithm::Md5) { request.AddHeader( - "Content-MD5", Base64Encode(options.TransactionalContentHash.GetValue().Value)); + "Content-MD5", Azure::Core::Base64Encode(options.TransactionalContentHash.GetValue().Value)); } else if (options.TransactionalContentHash.GetValue().Algorithm == HashAlgorithm::Crc64) { request.AddHeader( "x-ms-content-crc64", - Base64Encode(options.TransactionalContentHash.GetValue().Value)); + Azure::Core::Base64Encode(options.TransactionalContentHash.GetValue().Value)); } } if (!options.HttpHeaders.ContentType.empty()) @@ -6764,10 +6764,10 @@ namespace Azure { namespace Storage { namespace Blobs { { request.AddHeader("x-ms-blob-cache-control", options.HttpHeaders.CacheControl); } - if (!Base64Encode(options.HttpHeaders.ContentHash.Value).empty()) + if (!Azure::Core::Base64Encode(options.HttpHeaders.ContentHash.Value).empty()) { request.AddHeader( - "x-ms-blob-content-md5", Base64Encode(options.HttpHeaders.ContentHash.Value)); + "x-ms-blob-content-md5", Azure::Core::Base64Encode(options.HttpHeaders.ContentHash.Value)); } if (!options.HttpHeaders.ContentDisposition.empty()) { @@ -6834,7 +6834,7 @@ namespace Azure { namespace Storage { namespace Blobs { { ContentHash hash; hash.Algorithm = HashAlgorithm::Md5; - hash.Value = Base64Decode(content_md5_iterator->second); + hash.Value = Azure::Core::Base64Decode(content_md5_iterator->second); response.TransactionalContentHash = std::move(hash); } auto x_ms_content_crc64_iterator = headers.find("x-ms-content-crc64"); @@ -6842,7 +6842,7 @@ namespace Azure { namespace Storage { namespace Blobs { { ContentHash hash; hash.Algorithm = HashAlgorithm::Crc64; - hash.Value = Base64Decode(x_ms_content_crc64_iterator->second); + hash.Value = Azure::Core::Base64Decode(x_ms_content_crc64_iterator->second); response.TransactionalContentHash = std::move(hash); } } @@ -6858,7 +6858,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (x_ms_encryption_key_sha256__iterator != httpResponse.GetHeaders().end()) { response.EncryptionKeySha256 - = Base64Decode(x_ms_encryption_key_sha256__iterator->second); + = Azure::Core::Base64Decode(x_ms_encryption_key_sha256__iterator->second); } auto x_ms_encryption_scope__iterator = httpResponse.GetHeaders().find("x-ms-encryption-scope"); @@ -6907,13 +6907,13 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.TransactionalContentHash.GetValue().Algorithm == HashAlgorithm::Md5) { request.AddHeader( - "Content-MD5", Base64Encode(options.TransactionalContentHash.GetValue().Value)); + "Content-MD5", Azure::Core::Base64Encode(options.TransactionalContentHash.GetValue().Value)); } else if (options.TransactionalContentHash.GetValue().Algorithm == HashAlgorithm::Crc64) { request.AddHeader( "x-ms-content-crc64", - Base64Encode(options.TransactionalContentHash.GetValue().Value)); + Azure::Core::Base64Encode(options.TransactionalContentHash.GetValue().Value)); } } if (options.LeaseId.HasValue()) @@ -6927,7 +6927,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.EncryptionKeySha256.HasValue()) { request.AddHeader( - "x-ms-encryption-key-sha256", Base64Encode(options.EncryptionKeySha256.GetValue())); + "x-ms-encryption-key-sha256", Azure::Core::Base64Encode(options.EncryptionKeySha256.GetValue())); } if (options.EncryptionAlgorithm.HasValue()) { @@ -6955,7 +6955,7 @@ namespace Azure { namespace Storage { namespace Blobs { { ContentHash hash; hash.Algorithm = HashAlgorithm::Md5; - hash.Value = Base64Decode(content_md5_iterator->second); + hash.Value = Azure::Core::Base64Decode(content_md5_iterator->second); response.TransactionalContentHash = std::move(hash); } auto x_ms_content_crc64_iterator = headers.find("x-ms-content-crc64"); @@ -6963,7 +6963,7 @@ namespace Azure { namespace Storage { namespace Blobs { { ContentHash hash; hash.Algorithm = HashAlgorithm::Crc64; - hash.Value = Base64Decode(x_ms_content_crc64_iterator->second); + hash.Value = Azure::Core::Base64Decode(x_ms_content_crc64_iterator->second); response.TransactionalContentHash = std::move(hash); } } @@ -6974,7 +6974,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (x_ms_encryption_key_sha256__iterator != httpResponse.GetHeaders().end()) { response.EncryptionKeySha256 - = Base64Decode(x_ms_encryption_key_sha256__iterator->second); + = Azure::Core::Base64Decode(x_ms_encryption_key_sha256__iterator->second); } auto x_ms_encryption_scope__iterator = httpResponse.GetHeaders().find("x-ms-encryption-scope"); @@ -7041,13 +7041,13 @@ namespace Azure { namespace Storage { namespace Blobs { { request.AddHeader( "x-ms-source-content-md5", - Base64Encode(options.TransactionalContentHash.GetValue().Value)); + Azure::Core::Base64Encode(options.TransactionalContentHash.GetValue().Value)); } else if (options.TransactionalContentHash.GetValue().Algorithm == HashAlgorithm::Crc64) { request.AddHeader( "x-ms-source-content-crc64", - Base64Encode(options.TransactionalContentHash.GetValue().Value)); + Azure::Core::Base64Encode(options.TransactionalContentHash.GetValue().Value)); } } if (options.LeaseId.HasValue()) @@ -7061,7 +7061,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.EncryptionKeySha256.HasValue()) { request.AddHeader( - "x-ms-encryption-key-sha256", Base64Encode(options.EncryptionKeySha256.GetValue())); + "x-ms-encryption-key-sha256", Azure::Core::Base64Encode(options.EncryptionKeySha256.GetValue())); } if (options.EncryptionAlgorithm.HasValue()) { @@ -7111,7 +7111,7 @@ namespace Azure { namespace Storage { namespace Blobs { { ContentHash hash; hash.Algorithm = HashAlgorithm::Md5; - hash.Value = Base64Decode(content_md5_iterator->second); + hash.Value = Azure::Core::Base64Decode(content_md5_iterator->second); response.TransactionalContentHash = std::move(hash); } auto x_ms_content_crc64_iterator = headers.find("x-ms-content-crc64"); @@ -7119,7 +7119,7 @@ namespace Azure { namespace Storage { namespace Blobs { { ContentHash hash; hash.Algorithm = HashAlgorithm::Crc64; - hash.Value = Base64Decode(x_ms_content_crc64_iterator->second); + hash.Value = Azure::Core::Base64Decode(x_ms_content_crc64_iterator->second); response.TransactionalContentHash = std::move(hash); } } @@ -7130,7 +7130,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (x_ms_encryption_key_sha256__iterator != httpResponse.GetHeaders().end()) { response.EncryptionKeySha256 - = Base64Decode(x_ms_encryption_key_sha256__iterator->second); + = Azure::Core::Base64Decode(x_ms_encryption_key_sha256__iterator->second); } auto x_ms_encryption_scope__iterator = httpResponse.GetHeaders().find("x-ms-encryption-scope"); @@ -7203,10 +7203,10 @@ namespace Azure { namespace Storage { namespace Blobs { { request.AddHeader("x-ms-blob-cache-control", options.HttpHeaders.CacheControl); } - if (!Base64Encode(options.HttpHeaders.ContentHash.Value).empty()) + if (!Azure::Core::Base64Encode(options.HttpHeaders.ContentHash.Value).empty()) { request.AddHeader( - "x-ms-blob-content-md5", Base64Encode(options.HttpHeaders.ContentHash.Value)); + "x-ms-blob-content-md5", Azure::Core::Base64Encode(options.HttpHeaders.ContentHash.Value)); } if (!options.HttpHeaders.ContentDisposition.empty()) { @@ -7228,7 +7228,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.EncryptionKeySha256.HasValue()) { request.AddHeader( - "x-ms-encryption-key-sha256", Base64Encode(options.EncryptionKeySha256.GetValue())); + "x-ms-encryption-key-sha256", Azure::Core::Base64Encode(options.EncryptionKeySha256.GetValue())); } if (options.EncryptionAlgorithm.HasValue()) { @@ -7295,7 +7295,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (x_ms_encryption_key_sha256__iterator != httpResponse.GetHeaders().end()) { response.EncryptionKeySha256 - = Base64Decode(x_ms_encryption_key_sha256__iterator->second); + = Azure::Core::Base64Decode(x_ms_encryption_key_sha256__iterator->second); } auto x_ms_encryption_scope__iterator = httpResponse.GetHeaders().find("x-ms-encryption-scope"); @@ -7572,10 +7572,10 @@ namespace Azure { namespace Storage { namespace Blobs { { request.AddHeader("x-ms-blob-cache-control", options.HttpHeaders.CacheControl); } - if (!Base64Encode(options.HttpHeaders.ContentHash.Value).empty()) + if (!Azure::Core::Base64Encode(options.HttpHeaders.ContentHash.Value).empty()) { request.AddHeader( - "x-ms-blob-content-md5", Base64Encode(options.HttpHeaders.ContentHash.Value)); + "x-ms-blob-content-md5", Azure::Core::Base64Encode(options.HttpHeaders.ContentHash.Value)); } if (!options.HttpHeaders.ContentDisposition.empty()) { @@ -7608,7 +7608,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.EncryptionKeySha256.HasValue()) { request.AddHeader( - "x-ms-encryption-key-sha256", Base64Encode(options.EncryptionKeySha256.GetValue())); + "x-ms-encryption-key-sha256", Azure::Core::Base64Encode(options.EncryptionKeySha256.GetValue())); } if (options.EncryptionAlgorithm.HasValue()) { @@ -7671,7 +7671,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (x_ms_encryption_key_sha256__iterator != httpResponse.GetHeaders().end()) { response.EncryptionKeySha256 - = Base64Decode(x_ms_encryption_key_sha256__iterator->second); + = Azure::Core::Base64Decode(x_ms_encryption_key_sha256__iterator->second); } auto x_ms_encryption_scope__iterator = httpResponse.GetHeaders().find("x-ms-encryption-scope"); @@ -7735,13 +7735,13 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.TransactionalContentHash.GetValue().Algorithm == HashAlgorithm::Md5) { request.AddHeader( - "Content-MD5", Base64Encode(options.TransactionalContentHash.GetValue().Value)); + "Content-MD5", Azure::Core::Base64Encode(options.TransactionalContentHash.GetValue().Value)); } else if (options.TransactionalContentHash.GetValue().Algorithm == HashAlgorithm::Crc64) { request.AddHeader( "x-ms-content-crc64", - Base64Encode(options.TransactionalContentHash.GetValue().Value)); + Azure::Core::Base64Encode(options.TransactionalContentHash.GetValue().Value)); } } request.AddHeader("x-ms-page-write", "update"); @@ -7774,7 +7774,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.EncryptionKeySha256.HasValue()) { request.AddHeader( - "x-ms-encryption-key-sha256", Base64Encode(options.EncryptionKeySha256.GetValue())); + "x-ms-encryption-key-sha256", Azure::Core::Base64Encode(options.EncryptionKeySha256.GetValue())); } if (options.EncryptionAlgorithm.HasValue()) { @@ -7832,7 +7832,7 @@ namespace Azure { namespace Storage { namespace Blobs { { ContentHash hash; hash.Algorithm = HashAlgorithm::Md5; - hash.Value = Base64Decode(content_md5_iterator->second); + hash.Value = Azure::Core::Base64Decode(content_md5_iterator->second); response.TransactionalContentHash = std::move(hash); } auto x_ms_content_crc64_iterator = headers.find("x-ms-content-crc64"); @@ -7840,7 +7840,7 @@ namespace Azure { namespace Storage { namespace Blobs { { ContentHash hash; hash.Algorithm = HashAlgorithm::Crc64; - hash.Value = Base64Decode(x_ms_content_crc64_iterator->second); + hash.Value = Azure::Core::Base64Decode(x_ms_content_crc64_iterator->second); response.TransactionalContentHash = std::move(hash); } } @@ -7853,7 +7853,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (x_ms_encryption_key_sha256__iterator != httpResponse.GetHeaders().end()) { response.EncryptionKeySha256 - = Base64Decode(x_ms_encryption_key_sha256__iterator->second); + = Azure::Core::Base64Decode(x_ms_encryption_key_sha256__iterator->second); } auto x_ms_encryption_scope__iterator = httpResponse.GetHeaders().find("x-ms-encryption-scope"); @@ -7928,13 +7928,13 @@ namespace Azure { namespace Storage { namespace Blobs { { request.AddHeader( "x-ms-source-content-md5", - Base64Encode(options.TransactionalContentHash.GetValue().Value)); + Azure::Core::Base64Encode(options.TransactionalContentHash.GetValue().Value)); } else if (options.TransactionalContentHash.GetValue().Algorithm == HashAlgorithm::Crc64) { request.AddHeader( "x-ms-source-content-crc64", - Base64Encode(options.TransactionalContentHash.GetValue().Value)); + Azure::Core::Base64Encode(options.TransactionalContentHash.GetValue().Value)); } } request.AddHeader("x-ms-page-write", "update"); @@ -7967,7 +7967,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.EncryptionKeySha256.HasValue()) { request.AddHeader( - "x-ms-encryption-key-sha256", Base64Encode(options.EncryptionKeySha256.GetValue())); + "x-ms-encryption-key-sha256", Azure::Core::Base64Encode(options.EncryptionKeySha256.GetValue())); } if (options.EncryptionAlgorithm.HasValue()) { @@ -8025,7 +8025,7 @@ namespace Azure { namespace Storage { namespace Blobs { { ContentHash hash; hash.Algorithm = HashAlgorithm::Md5; - hash.Value = Base64Decode(content_md5_iterator->second); + hash.Value = Azure::Core::Base64Decode(content_md5_iterator->second); response.TransactionalContentHash = std::move(hash); } auto x_ms_content_crc64_iterator = headers.find("x-ms-content-crc64"); @@ -8033,7 +8033,7 @@ namespace Azure { namespace Storage { namespace Blobs { { ContentHash hash; hash.Algorithm = HashAlgorithm::Crc64; - hash.Value = Base64Decode(x_ms_content_crc64_iterator->second); + hash.Value = Azure::Core::Base64Decode(x_ms_content_crc64_iterator->second); response.TransactionalContentHash = std::move(hash); } } @@ -8046,7 +8046,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (x_ms_encryption_key_sha256__iterator != httpResponse.GetHeaders().end()) { response.EncryptionKeySha256 - = Base64Decode(x_ms_encryption_key_sha256__iterator->second); + = Azure::Core::Base64Decode(x_ms_encryption_key_sha256__iterator->second); } auto x_ms_encryption_scope__iterator = httpResponse.GetHeaders().find("x-ms-encryption-scope"); @@ -8132,7 +8132,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.EncryptionKeySha256.HasValue()) { request.AddHeader( - "x-ms-encryption-key-sha256", Base64Encode(options.EncryptionKeySha256.GetValue())); + "x-ms-encryption-key-sha256", Azure::Core::Base64Encode(options.EncryptionKeySha256.GetValue())); } if (options.EncryptionAlgorithm.HasValue()) { @@ -8254,7 +8254,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.EncryptionKeySha256.HasValue()) { request.AddHeader( - "x-ms-encryption-key-sha256", Base64Encode(options.EncryptionKeySha256.GetValue())); + "x-ms-encryption-key-sha256", Azure::Core::Base64Encode(options.EncryptionKeySha256.GetValue())); } if (options.EncryptionAlgorithm.HasValue()) { @@ -8728,10 +8728,10 @@ namespace Azure { namespace Storage { namespace Blobs { { request.AddHeader("x-ms-blob-cache-control", options.HttpHeaders.CacheControl); } - if (!Base64Encode(options.HttpHeaders.ContentHash.Value).empty()) + if (!Azure::Core::Base64Encode(options.HttpHeaders.ContentHash.Value).empty()) { request.AddHeader( - "x-ms-blob-content-md5", Base64Encode(options.HttpHeaders.ContentHash.Value)); + "x-ms-blob-content-md5", Azure::Core::Base64Encode(options.HttpHeaders.ContentHash.Value)); } if (!options.HttpHeaders.ContentDisposition.empty()) { @@ -8754,7 +8754,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.EncryptionKeySha256.HasValue()) { request.AddHeader( - "x-ms-encryption-key-sha256", Base64Encode(options.EncryptionKeySha256.GetValue())); + "x-ms-encryption-key-sha256", Azure::Core::Base64Encode(options.EncryptionKeySha256.GetValue())); } if (options.EncryptionAlgorithm.HasValue()) { @@ -8817,7 +8817,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (x_ms_encryption_key_sha256__iterator != httpResponse.GetHeaders().end()) { response.EncryptionKeySha256 - = Base64Decode(x_ms_encryption_key_sha256__iterator->second); + = Azure::Core::Base64Decode(x_ms_encryption_key_sha256__iterator->second); } auto x_ms_encryption_scope__iterator = httpResponse.GetHeaders().find("x-ms-encryption-scope"); @@ -8870,13 +8870,13 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.TransactionalContentHash.GetValue().Algorithm == HashAlgorithm::Md5) { request.AddHeader( - "Content-MD5", Base64Encode(options.TransactionalContentHash.GetValue().Value)); + "Content-MD5", Azure::Core::Base64Encode(options.TransactionalContentHash.GetValue().Value)); } else if (options.TransactionalContentHash.GetValue().Algorithm == HashAlgorithm::Crc64) { request.AddHeader( "x-ms-content-crc64", - Base64Encode(options.TransactionalContentHash.GetValue().Value)); + Azure::Core::Base64Encode(options.TransactionalContentHash.GetValue().Value)); } } if (options.LeaseId.HasValue()) @@ -8900,7 +8900,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.EncryptionKeySha256.HasValue()) { request.AddHeader( - "x-ms-encryption-key-sha256", Base64Encode(options.EncryptionKeySha256.GetValue())); + "x-ms-encryption-key-sha256", Azure::Core::Base64Encode(options.EncryptionKeySha256.GetValue())); } if (options.EncryptionAlgorithm.HasValue()) { @@ -8958,7 +8958,7 @@ namespace Azure { namespace Storage { namespace Blobs { { ContentHash hash; hash.Algorithm = HashAlgorithm::Md5; - hash.Value = Base64Decode(content_md5_iterator->second); + hash.Value = Azure::Core::Base64Decode(content_md5_iterator->second); response.TransactionalContentHash = std::move(hash); } auto x_ms_content_crc64_iterator = headers.find("x-ms-content-crc64"); @@ -8966,7 +8966,7 @@ namespace Azure { namespace Storage { namespace Blobs { { ContentHash hash; hash.Algorithm = HashAlgorithm::Crc64; - hash.Value = Base64Decode(x_ms_content_crc64_iterator->second); + hash.Value = Azure::Core::Base64Decode(x_ms_content_crc64_iterator->second); response.TransactionalContentHash = std::move(hash); } } @@ -8981,7 +8981,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (x_ms_encryption_key_sha256__iterator != httpResponse.GetHeaders().end()) { response.EncryptionKeySha256 - = Base64Decode(x_ms_encryption_key_sha256__iterator->second); + = Azure::Core::Base64Decode(x_ms_encryption_key_sha256__iterator->second); } auto x_ms_encryption_scope__iterator = httpResponse.GetHeaders().find("x-ms-encryption-scope"); @@ -9048,13 +9048,13 @@ namespace Azure { namespace Storage { namespace Blobs { { request.AddHeader( "x-ms-source-content-md5", - Base64Encode(options.TransactionalContentHash.GetValue().Value)); + Azure::Core::Base64Encode(options.TransactionalContentHash.GetValue().Value)); } else if (options.TransactionalContentHash.GetValue().Algorithm == HashAlgorithm::Crc64) { request.AddHeader( "x-ms-source-content-crc64", - Base64Encode(options.TransactionalContentHash.GetValue().Value)); + Azure::Core::Base64Encode(options.TransactionalContentHash.GetValue().Value)); } } if (options.LeaseId.HasValue()) @@ -9078,7 +9078,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (options.EncryptionKeySha256.HasValue()) { request.AddHeader( - "x-ms-encryption-key-sha256", Base64Encode(options.EncryptionKeySha256.GetValue())); + "x-ms-encryption-key-sha256", Azure::Core::Base64Encode(options.EncryptionKeySha256.GetValue())); } if (options.EncryptionAlgorithm.HasValue()) { @@ -9136,7 +9136,7 @@ namespace Azure { namespace Storage { namespace Blobs { { ContentHash hash; hash.Algorithm = HashAlgorithm::Md5; - hash.Value = Base64Decode(content_md5_iterator->second); + hash.Value = Azure::Core::Base64Decode(content_md5_iterator->second); response.TransactionalContentHash = std::move(hash); } auto x_ms_content_crc64_iterator = headers.find("x-ms-content-crc64"); @@ -9144,7 +9144,7 @@ namespace Azure { namespace Storage { namespace Blobs { { ContentHash hash; hash.Algorithm = HashAlgorithm::Crc64; - hash.Value = Base64Decode(x_ms_content_crc64_iterator->second); + hash.Value = Azure::Core::Base64Decode(x_ms_content_crc64_iterator->second); response.TransactionalContentHash = std::move(hash); } } @@ -9159,7 +9159,7 @@ namespace Azure { namespace Storage { namespace Blobs { if (x_ms_encryption_key_sha256__iterator != httpResponse.GetHeaders().end()) { response.EncryptionKeySha256 - = Base64Decode(x_ms_encryption_key_sha256__iterator->second); + = Azure::Core::Base64Decode(x_ms_encryption_key_sha256__iterator->second); } auto x_ms_encryption_scope__iterator = httpResponse.GetHeaders().find("x-ms-encryption-scope"); diff --git a/sdk/storage/azure-storage-blobs/src/blob_sas_builder.cpp b/sdk/storage/azure-storage-blobs/src/blob_sas_builder.cpp index 7c4dcb0df..455c084af 100644 --- a/sdk/storage/azure-storage-blobs/src/blob_sas_builder.cpp +++ b/sdk/storage/azure-storage-blobs/src/blob_sas_builder.cpp @@ -141,9 +141,9 @@ namespace Azure { namespace Storage { namespace Sas { + snapshotVersion + "\n" + CacheControl + "\n" + ContentDisposition + "\n" + ContentEncoding + "\n" + ContentLanguage + "\n" + ContentType; - std::string signature = Base64Encode(Storage::Details::HmacSha256( + std::string signature = Azure::Core::Base64Encode(Storage::Details::HmacSha256( std::vector(stringToSign.begin(), stringToSign.end()), - Base64Decode(credential.GetAccountKey()))); + Azure::Core::Base64Decode(credential.GetAccountKey()))); Azure::Core::Http::Url builder; builder.AppendQueryParameter( @@ -241,9 +241,9 @@ namespace Azure { namespace Storage { namespace Sas { + CacheControl + "\n" + ContentDisposition + "\n" + ContentEncoding + "\n" + ContentLanguage + "\n" + ContentType; - std::string signature = Base64Encode(Storage::Details::HmacSha256( + std::string signature = Azure::Core::Base64Encode(Storage::Details::HmacSha256( std::vector(stringToSign.begin(), stringToSign.end()), - Base64Decode(userDelegationKey.Value))); + Azure::Core::Base64Decode(userDelegationKey.Value))); Azure::Core::Http::Url builder; builder.AppendQueryParameter( diff --git a/sdk/storage/azure-storage-blobs/src/block_blob_client.cpp b/sdk/storage/azure-storage-blobs/src/block_blob_client.cpp index 0f4c6b794..4b38d2f21 100644 --- a/sdk/storage/azure-storage-blobs/src/block_blob_client.cpp +++ b/sdk/storage/azure-storage-blobs/src/block_blob_client.cpp @@ -139,7 +139,7 @@ namespace Azure { namespace Storage { namespace Blobs { constexpr std::size_t BlockIdLength = 64; std::string blockId = std::to_string(id); blockId = std::string(BlockIdLength - blockId.length(), '0') + blockId; - return Base64Encode(blockId); + return Internal::Base64EncodeText(blockId); }; auto uploadBlockFunc = [&](int64_t offset, int64_t length, int64_t chunkId, int64_t numChunks) { @@ -220,7 +220,7 @@ namespace Azure { namespace Storage { namespace Blobs { constexpr std::size_t BlockIdLength = 64; std::string blockId = std::to_string(id); blockId = std::string(BlockIdLength - blockId.length(), '0') + blockId; - return Base64Encode(blockId); + return Internal::Base64EncodeText(blockId); }; auto uploadBlockFunc = [&](int64_t offset, int64_t length, int64_t chunkId, int64_t numChunks) { diff --git a/sdk/storage/azure-storage-blobs/test/blob_container_client_test.cpp b/sdk/storage/azure-storage-blobs/test/blob_container_client_test.cpp index 3d29b24aa..82524a62d 100644 --- a/sdk/storage/azure-storage-blobs/test/blob_container_client_test.cpp +++ b/sdk/storage/azure-storage-blobs/test/blob_container_client_test.cpp @@ -523,7 +523,7 @@ namespace Azure { namespace Storage { namespace Test { std::vector aes256Key; aes256Key.resize(32); RandomBuffer(&aes256Key[0], aes256Key.size()); - key.Key = Base64Encode(aes256Key); + key.Key = Azure::Core::Base64Encode(aes256Key); key.KeyHash = Details::Sha256(aes256Key); key.Algorithm = Blobs::Models::EncryptionAlgorithmType::Aes256; return key; @@ -544,8 +544,8 @@ namespace Azure { namespace Storage { namespace Test { auto blockBlob = containerClient.GetBlockBlobClient(blockBlobName); bodyStream.Rewind(); EXPECT_NO_THROW(blockBlob.Upload(&bodyStream)); - std::string blockId1 = Base64Encode("1"); - std::string blockId2 = Base64Encode("2"); + std::string blockId1 = Internal::Base64EncodeText("1"); + std::string blockId2 = Internal::Base64EncodeText("2"); bodyStream.Rewind(); EXPECT_NO_THROW(blockBlob.StageBlock(blockId1, &bodyStream)); EXPECT_NO_THROW(blockBlob.StageBlockFromUri(blockId2, copySourceBlob.GetUrl() + GetSas())); @@ -1045,7 +1045,7 @@ namespace Azure { namespace Storage { namespace Test { } { - std::string blockId = Base64Encode("1"); + std::string blockId = Internal::Base64EncodeText("1"); std::vector blockIds = {blockId}; content.Rewind(); blockBlobClient.StageBlock(blockId, &content); diff --git a/sdk/storage/azure-storage-blobs/test/block_blob_client_test.cpp b/sdk/storage/azure-storage-blobs/test/block_blob_client_test.cpp index ccbd10552..2d1ddc331 100644 --- a/sdk/storage/azure-storage-blobs/test/block_blob_client_test.cpp +++ b/sdk/storage/azure-storage-blobs/test/block_blob_client_test.cpp @@ -97,7 +97,8 @@ namespace Azure { namespace Storage { namespace Test { std::vector( m_blobContent.begin() + static_cast(options.Range.GetValue().Offset), m_blobContent.begin() - + static_cast(options.Range.GetValue().Offset + options.Range.GetValue().Length.GetValue()))); + + static_cast( + options.Range.GetValue().Offset + options.Range.GetValue().Length.GetValue()))); EXPECT_FALSE(res->ContentRange.GetValue().empty()); } @@ -268,8 +269,8 @@ namespace Azure { namespace Storage { namespace Test { TEST_F(BlockBlobClientTest, StageBlock) { - const std::string blockId1 = Azure::Storage::Base64Encode("0"); - const std::string blockId2 = Azure::Storage::Base64Encode("1"); + const std::string blockId1 = Azure::Storage::Internal::Base64EncodeText("0"); + const std::string blockId2 = Azure::Storage::Internal::Base64EncodeText("1"); auto blockBlobClient = Azure::Storage::Blobs::BlockBlobClient::CreateFromConnectionString( StandardStorageConnectionString(), m_containerName, RandomString()); std::vector block1Content; diff --git a/sdk/storage/azure-storage-blobs/test/page_blob_client_test.cpp b/sdk/storage/azure-storage-blobs/test/page_blob_client_test.cpp index 048a13337..3d385cd2b 100644 --- a/sdk/storage/azure-storage-blobs/test/page_blob_client_test.cpp +++ b/sdk/storage/azure-storage-blobs/test/page_blob_client_test.cpp @@ -227,7 +227,7 @@ namespace Azure { namespace Storage { namespace Test { EXPECT_NO_THROW(pageBlobClient.UploadPages(0, &pageContent, options)); pageContent.Rewind(); - hash.Value = Base64Decode(DummyMd5); + hash.Value = Azure::Core::Base64Decode(DummyMd5); options.TransactionalContentHash = hash; EXPECT_THROW(pageBlobClient.UploadPages(0, &pageContent, options), StorageException); } @@ -251,7 +251,7 @@ namespace Azure { namespace Storage { namespace Test { EXPECT_NO_THROW(pageBlobClient.UploadPages(0, &pageContent, options)); pageContent.Rewind(); - hash.Value = Base64Decode(DummyCrc64); + hash.Value = Azure::Core::Base64Decode(DummyCrc64); options.TransactionalContentHash = hash; EXPECT_THROW(pageBlobClient.UploadPages(0, &pageContent, options), StorageException); } diff --git a/sdk/storage/azure-storage-common/CHANGELOG.md b/sdk/storage/azure-storage-common/CHANGELOG.md index b5b91d1cc..749c8c4e8 100644 --- a/sdk/storage/azure-storage-common/CHANGELOG.md +++ b/sdk/storage/azure-storage-common/CHANGELOG.md @@ -15,6 +15,8 @@ - Move Account SAS into `Azure::Storage::Sas` namespace. - All date time related strings are now changed to `Azure::Core::DateTime` type. - Move version strings into `Details` namespace. +- Move `Base64Encode` and `Base64Decode` from the `Azure::Storage` namespace to `Azure::Core`. +- Rename the string accepting overload of `Base64Encode` to `Base64EncodeText` and make it internal by moving it to the `Azure::Storage::Internal` namespace. ## 12.0.0-beta.5 (2020-11-13) diff --git a/sdk/storage/azure-storage-common/inc/azure/storage/common/crypt.hpp b/sdk/storage/azure-storage-common/inc/azure/storage/common/crypt.hpp index 16a0e0efe..29b20c1f0 100644 --- a/sdk/storage/azure-storage-common/inc/azure/storage/common/crypt.hpp +++ b/sdk/storage/azure-storage-common/inc/azure/storage/common/crypt.hpp @@ -7,14 +7,16 @@ #include #include +#include + namespace Azure { namespace Storage { - std::string Base64Encode(const std::vector& data); - inline std::string Base64Encode(const std::string& text) - { - return Base64Encode(std::vector(text.begin(), text.end())); - } - std::vector Base64Decode(const std::string& text); + namespace Internal { + inline std::string Base64EncodeText(const std::string& text) + { + return Azure::Core::Base64Encode(std::vector(text.begin(), text.end())); + } + } // namespace Internal class Md5 { public: diff --git a/sdk/storage/azure-storage-common/src/account_sas_builder.cpp b/sdk/storage/azure-storage-common/src/account_sas_builder.cpp index 3292c7258..064823bde 100644 --- a/sdk/storage/azure-storage-common/src/account_sas_builder.cpp +++ b/sdk/storage/azure-storage-common/src/account_sas_builder.cpp @@ -102,9 +102,9 @@ namespace Azure { namespace Storage { namespace Sas { + (IPRange.HasValue() ? IPRange.GetValue() : "") + "\n" + protocol + "\n" + Storage::Details::DefaultSasVersion + "\n"; - std::string signature = Base64Encode(Storage::Details::HmacSha256( + std::string signature = Azure::Core::Base64Encode(Storage::Details::HmacSha256( std::vector(stringToSign.begin(), stringToSign.end()), - Base64Decode(credential.GetAccountKey()))); + Azure::Core::Base64Decode(credential.GetAccountKey()))); Azure::Core::Http::Url builder; builder.AppendQueryParameter( diff --git a/sdk/storage/azure-storage-common/src/crypt.cpp b/sdk/storage/azure-storage-common/src/crypt.cpp index 9b3b7fbcc..d609a115a 100644 --- a/sdk/storage/azure-storage-common/src/crypt.cpp +++ b/sdk/storage/azure-storage-common/src/crypt.cpp @@ -318,43 +318,6 @@ namespace Azure { namespace Storage { return hash; } - std::string Base64Encode(const std::vector& data) - { - std::string encoded; - // According to RFC 4648, the encoded length should be ceiling(n / 3) * 4 - DWORD encodedLength = static_cast((data.size() + 2) / 3 * 4); - encoded.resize(encodedLength); - - CryptBinaryToStringA( - reinterpret_cast(data.data()), - static_cast(data.size()), - CRYPT_STRING_BASE64 | CRYPT_STRING_NOCRLF, - static_cast(&encoded[0]), - &encodedLength); - - return encoded; - } - - std::vector Base64Decode(const std::string& text) - { - std::vector decoded; - // According to RFC 4648, the encoded length should be ceiling(n / 3) * 4, so we can infer an - // upper bound here - DWORD decodedLength = DWORD(text.length() / 4 * 3); - decoded.resize(decodedLength); - - CryptStringToBinaryA( - text.data(), - static_cast(text.length()), - CRYPT_STRING_BASE64 | CRYPT_STRING_STRICT, - reinterpret_cast(decoded.data()), - &decodedLength, - nullptr, - nullptr); - decoded.resize(decodedLength); - return decoded; - } - #elif defined(AZ_PLATFORM_POSIX) namespace Details { @@ -416,39 +379,6 @@ namespace Azure { namespace Storage { return std::vector(std::begin(hash), std::end(hash)); } - std::string Base64Encode(const std::vector& data) - { - BIO* bio = BIO_new(BIO_s_mem()); - bio = BIO_push(BIO_new(BIO_f_base64()), bio); - BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); - BIO_write(bio, data.data(), static_cast(data.size())); - BIO_flush(bio); - BUF_MEM* bufferPtr; - BIO_get_mem_ptr(bio, &bufferPtr); - BIO_set_close(bio, BIO_NOCLOSE); - BIO_free_all(bio); - - return std::string(bufferPtr->data, bufferPtr->length); - } - - std::vector Base64Decode(const std::string& text) - { - std::vector decoded; - // According to RFC 4648, the encoded length should be ceiling(n / 3) * 4, so we can infer an - // upper bound here - std::size_t maxDecodedLength = text.length() / 4 * 3; - decoded.resize(maxDecodedLength); - - BIO* bio = BIO_new_mem_buf(text.data(), -1); - bio = BIO_push(BIO_new(BIO_f_base64()), bio); - BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); - int decodedLength = BIO_read(bio, &decoded[0], static_cast(text.length())); - BIO_free_all(bio); - - decoded.resize(decodedLength); - return decoded; - } - #endif static constexpr uint64_t Crc64Poly = 0x9A6C9329AC4BC9B5ULL; diff --git a/sdk/storage/azure-storage-common/src/shared_key_policy.cpp b/sdk/storage/azure-storage-common/src/shared_key_policy.cpp index 5f5d9d7c4..018607fd2 100644 --- a/sdk/storage/azure-storage-common/src/shared_key_policy.cpp +++ b/sdk/storage/azure-storage-common/src/shared_key_policy.cpp @@ -81,8 +81,8 @@ namespace Azure { namespace Storage { namespace Details { // remove last linebreak string_to_sign.pop_back(); - return Base64Encode(Details::HmacSha256( + return Azure::Core::Base64Encode(Details::HmacSha256( std::vector(string_to_sign.begin(), string_to_sign.end()), - Base64Decode(m_credential->GetAccountKey()))); + Azure::Core::Base64Decode(m_credential->GetAccountKey()))); } }}} // namespace Azure::Storage::Details diff --git a/sdk/storage/azure-storage-common/src/storage_common.cpp b/sdk/storage/azure-storage-common/src/storage_common.cpp index d5be1e087..c56b52e06 100644 --- a/sdk/storage/azure-storage-common/src/storage_common.cpp +++ b/sdk/storage/azure-storage-common/src/storage_common.cpp @@ -19,12 +19,15 @@ namespace Azure { namespace Storage { ContentHash FromBase64String(const std::string& base64String, HashAlgorithm algorithm) { ContentHash hash; - hash.Value = Base64Decode(base64String); + hash.Value = Azure::Core::Base64Decode(base64String); hash.Algorithm = algorithm; return hash; } - std::string ToBase64String(const ContentHash& hash) { return Base64Encode(hash.Value); } + std::string ToBase64String(const ContentHash& hash) + { + return Azure::Core::Base64Encode(hash.Value); + } } // namespace Details }} // namespace Azure::Storage diff --git a/sdk/storage/azure-storage-common/test/crypt_functions_test.cpp b/sdk/storage/azure-storage-common/test/crypt_functions_test.cpp index 1e582b3e5..446956cee 100644 --- a/sdk/storage/azure-storage-common/test/crypt_functions_test.cpp +++ b/sdk/storage/azure-storage-common/test/crypt_functions_test.cpp @@ -15,24 +15,13 @@ namespace Azure { namespace Storage { namespace Test { return std::vector(start, start + strlen(text)); } - TEST(CryptFunctionsTest, Base64) - { - for (std::size_t len : {0, 10, 100, 1000, 10000}) - { - std::vector data; - data.resize(len); - RandomBuffer(data.data(), data.size()); - EXPECT_EQ(Base64Decode(Base64Encode(data)), data); - } - } - TEST(CryptFunctionsTest, Sha256) { EXPECT_EQ( - Base64Encode(Details::Sha256(ToBinaryVector(""))), + Azure::Core::Base64Encode(Details::Sha256(ToBinaryVector(""))), "47DEQpj8HBSa+/TImW+5JCeuQeRkm5NMpJWZG3hSuFU="); EXPECT_EQ( - Base64Encode(Details::Sha256(ToBinaryVector("Hello Azure!"))), + Azure::Core::Base64Encode(Details::Sha256(ToBinaryVector("Hello Azure!"))), "Mjzwx2mqGHb9FSgjm33ShNmXYndkgvwA6tQmEiskOHg="); } @@ -41,17 +30,17 @@ namespace Azure { namespace Storage { namespace Test { std::string key = "8CwtGFF1mGR4bPEP9eZ0x1fxKiQ3Ca5N"; std::vector binaryKey(key.begin(), key.end()); EXPECT_EQ( - Base64Encode(Details::HmacSha256(ToBinaryVector(""), binaryKey)), + Azure::Core::Base64Encode(Details::HmacSha256(ToBinaryVector(""), binaryKey)), "fFy2T+EuCvAgouw/vB/RAJ75z7jwTj+uiURebkFKF5M="); EXPECT_EQ( - Base64Encode(Details::HmacSha256(ToBinaryVector("Hello Azure!"), binaryKey)), + Azure::Core::Base64Encode(Details::HmacSha256(ToBinaryVector("Hello Azure!"), binaryKey)), "+SBESxQVhI53mSEdZJcCBpdBkaqwzfPaVYZMAf5LP3c="); } TEST(CryptFunctionsTest, Md5) { - EXPECT_EQ(Base64Encode(Md5::Hash("")), "1B2M2Y8AsgTpgAmY7PhCfg=="); - EXPECT_EQ(Base64Encode(Md5::Hash("Hello Azure!")), "Pz8543xut4RVSbb2g52Mww=="); + EXPECT_EQ(Azure::Core::Base64Encode(Md5::Hash("")), "1B2M2Y8AsgTpgAmY7PhCfg=="); + EXPECT_EQ(Azure::Core::Base64Encode(Md5::Hash("Hello Azure!")), "Pz8543xut4RVSbb2g52Mww=="); auto data = RandomBuffer(static_cast(16_MB)); Md5 md5Instance; @@ -70,8 +59,8 @@ namespace Azure { namespace Storage { namespace Test { TEST(CryptFunctionsTest, Crc64) { - EXPECT_EQ(Base64Encode(Crc64::Hash("")), "AAAAAAAAAAA="); - EXPECT_EQ(Base64Encode(Crc64::Hash("Hello Azure!")), "DtjZpL9/o8c="); + EXPECT_EQ(Azure::Core::Base64Encode(Crc64::Hash("")), "AAAAAAAAAAA="); + EXPECT_EQ(Azure::Core::Base64Encode(Crc64::Hash("Hello Azure!")), "DtjZpL9/o8c="); auto data = RandomBuffer(static_cast(16_MB)); Crc64 crc64Instance; diff --git a/sdk/storage/azure-storage-files-datalake/src/datalake_sas_builder.cpp b/sdk/storage/azure-storage-files-datalake/src/datalake_sas_builder.cpp index 8cca0969d..58e9a5978 100644 --- a/sdk/storage/azure-storage-files-datalake/src/datalake_sas_builder.cpp +++ b/sdk/storage/azure-storage-files-datalake/src/datalake_sas_builder.cpp @@ -136,9 +136,9 @@ namespace Azure { namespace Storage { namespace Sas { + "\n" + CacheControl + "\n" + ContentDisposition + "\n" + ContentEncoding + "\n" + ContentLanguage + "\n" + ContentType; - std::string signature = Base64Encode(Storage::Details::HmacSha256( + std::string signature = Azure::Core::Base64Encode(Storage::Details::HmacSha256( std::vector(stringToSign.begin(), stringToSign.end()), - Base64Decode(credential.GetAccountKey()))); + Azure::Core::Base64Decode(credential.GetAccountKey()))); Azure::Core::Http::Url builder; builder.AppendQueryParameter( @@ -225,9 +225,9 @@ namespace Azure { namespace Storage { namespace Sas { + Storage::Details::DefaultSasVersion + "\n" + resource + "\n" + "\n" + CacheControl + "\n" + ContentDisposition + "\n" + ContentEncoding + "\n" + ContentLanguage + "\n" + ContentType; - std::string signature = Base64Encode(Storage::Details::HmacSha256( + std::string signature = Azure::Core::Base64Encode(Storage::Details::HmacSha256( std::vector(stringToSign.begin(), stringToSign.end()), - Base64Decode(userDelegationKey.Value))); + Azure::Core::Base64Decode(userDelegationKey.Value))); Azure::Core::Http::Url builder; builder.AppendQueryParameter( diff --git a/sdk/storage/azure-storage-files-datalake/src/datalake_utilities.cpp b/sdk/storage/azure-storage-files-datalake/src/datalake_utilities.cpp index 33d076ff2..6fcda534e 100644 --- a/sdk/storage/azure-storage-files-datalake/src/datalake_utilities.cpp +++ b/sdk/storage/azure-storage-files-datalake/src/datalake_utilities.cpp @@ -39,7 +39,7 @@ namespace Azure { namespace Storage { namespace Files { namespace DataLake { nam std::string result; for (const auto& pair : dataLakePropertiesMap) { - result.append(pair.first + "=" + Base64Encode(pair.second) + ","); + result.append(pair.first + "=" + Internal::Base64EncodeText(pair.second) + ","); } if (!result.empty()) { diff --git a/sdk/storage/azure-storage-files-shares/src/share_sas_builder.cpp b/sdk/storage/azure-storage-files-shares/src/share_sas_builder.cpp index 407325bf9..43be70ff9 100644 --- a/sdk/storage/azure-storage-files-shares/src/share_sas_builder.cpp +++ b/sdk/storage/azure-storage-files-shares/src/share_sas_builder.cpp @@ -96,9 +96,9 @@ namespace Azure { namespace Storage { namespace Sas { + "\n" + protocol + "\n" + Storage::Details::DefaultSasVersion + "\n" + CacheControl + "\n" + ContentDisposition + "\n" + ContentEncoding + "\n" + ContentLanguage + "\n" + ContentType; - std::string signature = Base64Encode(Storage::Details::HmacSha256( + std::string signature = Azure::Core::Base64Encode(Storage::Details::HmacSha256( std::vector(stringToSign.begin(), stringToSign.end()), - Base64Decode(credential.GetAccountKey()))); + Azure::Core::Base64Decode(credential.GetAccountKey()))); Azure::Core::Http::Url builder; builder.AppendQueryParameter(