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 f040e8f94..a1503ee09 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 @@ -7,6 +7,7 @@ #include #include +#include #include #include @@ -120,10 +121,12 @@ namespace Azure { namespace Storage { namespace Test { auto res = m_blockBlobClient->Download(options); ASSERT_TRUE(res->TransactionalContentHash.HasValue()); EXPECT_EQ(res->TransactionalContentHash.GetValue().Algorithm, HashAlgorithm::Md5); - EXPECT_EQ( - res->TransactionalContentHash.GetValue().Value, - Md5::Hash(m_blobContent.data(), downloadLength)); - + { + Azure::Core::Cryptography::Md5Hash instance; + EXPECT_EQ( + res->TransactionalContentHash.GetValue().Value, + instance.Final(m_blobContent.data(), downloadLength)); + } options.RangeHashAlgorithm = HashAlgorithm::Crc64; res = m_blockBlobClient->Download(options); ASSERT_TRUE(res->TransactionalContentHash.HasValue()); 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 29d656a6e..f8d80e4aa 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 @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -251,7 +252,11 @@ namespace Azure { namespace Storage { namespace Test { Blobs::UploadPageBlobPagesOptions options; ContentHash hash; hash.Algorithm = HashAlgorithm::Md5; - hash.Value = Md5::Hash(blobContent.data(), blobContent.size()); + + { + Azure::Core::Cryptography::Md5Hash instance; + hash.Value = instance.Final(blobContent.data(), blobContent.size()); + } options.TransactionalContentHash = hash; EXPECT_NO_THROW(pageBlobClient.UploadPages(0, &pageContent, options)); diff --git a/sdk/storage/azure-storage-common/CHANGELOG.md b/sdk/storage/azure-storage-common/CHANGELOG.md index 4145e1d04..4c53ad7dd 100644 --- a/sdk/storage/azure-storage-common/CHANGELOG.md +++ b/sdk/storage/azure-storage-common/CHANGELOG.md @@ -2,6 +2,9 @@ ## 12.0.0-beta.8 (Unreleased) +### Breaking Changes + +- Remove the `Azure::Storage::Md5` class from `crypt.hpp`. Use the type from `Azure::Core::Cryptography` namespace instead, from `azure/core/cryptography/hash.hpp`. ## 12.0.0-beta.7 (2021-02-03) 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 d2dace420..688f0aa0e 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 @@ -11,31 +11,6 @@ namespace Azure { namespace Storage { - class Md5 { - public: - Md5(); - ~Md5(); - - void Update(const uint8_t* data, std::size_t length); - - std::vector Digest() const; - - static std::vector Hash(const uint8_t* data, std::size_t length) - { - Md5 instance; - instance.Update(data, length); - return instance.Digest(); - } - - static std::vector Hash(const std::string& data) - { - return Hash(reinterpret_cast(data.data()), data.length()); - } - - private: - void* m_context; - }; - class Crc64 { public: void Update(const uint8_t* data, std::size_t length); diff --git a/sdk/storage/azure-storage-common/src/crypt.cpp b/sdk/storage/azure-storage-common/src/crypt.cpp index d609a115a..8a6b61fbe 100644 --- a/sdk/storage/azure-storage-common/src/crypt.cpp +++ b/sdk/storage/azure-storage-common/src/crypt.cpp @@ -18,7 +18,6 @@ #include #include #include -#include #include #endif @@ -87,7 +86,6 @@ namespace Azure { namespace Storage { { HmacSha256, Sha256, - Md5, }; struct AlgorithmProviderInstance @@ -103,10 +101,6 @@ namespace Azure { namespace Storage { { algorithmId = BCRYPT_SHA256_ALGORITHM; } - else if (type == AlgorithmType::Md5) - { - algorithmId = BCRYPT_MD5_ALGORITHM; - } else { throw std::runtime_error("unknwon algorithm type"); @@ -249,75 +243,6 @@ namespace Azure { namespace Storage { } } // namespace Details - struct Md5HashContext - { - std::string buffer; - BCRYPT_HASH_HANDLE hashHandle = nullptr; - std::size_t hashLength = 0; - }; - - Md5::Md5() - { - static Details::AlgorithmProviderInstance AlgorithmProvider(Details::AlgorithmType::Md5); - - Md5HashContext* context = new Md5HashContext; - m_context = context; - context->buffer.resize(AlgorithmProvider.ContextSize); - context->hashLength = AlgorithmProvider.HashLength; - - NTSTATUS status = BCryptCreateHash( - AlgorithmProvider.Handle, - &context->hashHandle, - reinterpret_cast(&context->buffer[0]), - static_cast(context->buffer.size()), - nullptr, - 0, - 0); - if (!BCRYPT_SUCCESS(status)) - { - throw std::runtime_error("BCryptCreateHash failed"); - } - } - - Md5::~Md5() - { - Md5HashContext* context = static_cast(m_context); - BCryptDestroyHash(context->hashHandle); - delete context; - } - - void Md5::Update(const uint8_t* data, std::size_t length) - { - Md5HashContext* context = static_cast(m_context); - - NTSTATUS status = BCryptHashData( - context->hashHandle, - reinterpret_cast(const_cast(data)), - static_cast(length), - 0); - if (!BCRYPT_SUCCESS(status)) - { - throw std::runtime_error("BCryptHashData failed"); - } - } - - std::vector Md5::Digest() const - { - Md5HashContext* context = static_cast(m_context); - std::vector hash; - hash.resize(context->hashLength); - NTSTATUS status = BCryptFinishHash( - context->hashHandle, - reinterpret_cast(&hash[0]), - static_cast(hash.size()), - 0); - if (!BCRYPT_SUCCESS(status)) - { - throw std::runtime_error("BCryptFinishHash failed"); - } - return hash; - } - #elif defined(AZ_PLATFORM_POSIX) namespace Details { @@ -352,33 +277,6 @@ namespace Azure { namespace Storage { } // namespace Details - Md5::Md5() - { - MD5_CTX* context = new MD5_CTX; - m_context = context; - MD5_Init(context); - } - - Md5::~Md5() - { - MD5_CTX* context = static_cast(m_context); - delete context; - } - - void Md5::Update(const uint8_t* data, std::size_t length) - { - MD5_CTX* context = static_cast(m_context); - MD5_Update(context, data, length); - } - - std::vector Md5::Digest() const - { - MD5_CTX* context = static_cast(m_context); - unsigned char hash[MD5_DIGEST_LENGTH]; - MD5_Final(hash, context); - return std::vector(std::begin(hash), std::end(hash)); - } - #endif static constexpr uint64_t Crc64Poly = 0x9A6C9329AC4BC9B5ULL; 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 446956cee..3569cdbc2 100644 --- a/sdk/storage/azure-storage-common/test/crypt_functions_test.cpp +++ b/sdk/storage/azure-storage-common/test/crypt_functions_test.cpp @@ -37,26 +37,6 @@ namespace Azure { namespace Storage { namespace Test { "+SBESxQVhI53mSEdZJcCBpdBkaqwzfPaVYZMAf5LP3c="); } - TEST(CryptFunctionsTest, Md5) - { - 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; - - std::size_t length = 0; - while (length < data.size()) - { - std::size_t s = static_cast(RandomInt(0, 4_MB)); - s = std::min(s, data.size() - length); - md5Instance.Update(&data[length], s); - md5Instance.Update(&data[length], 0); - length += s; - } - EXPECT_EQ(md5Instance.Digest(), Md5::Hash(data.data(), data.size())); - } - TEST(CryptFunctionsTest, Crc64) { EXPECT_EQ(Azure::Core::Base64Encode(Crc64::Hash("")), "AAAAAAAAAAA="); diff --git a/sdk/storage/azure-storage-files-shares/test/share_file_client_test.cpp b/sdk/storage/azure-storage-files-shares/test/share_file_client_test.cpp index ebb4b0fb9..4287c8754 100644 --- a/sdk/storage/azure-storage-files-shares/test/share_file_client_test.cpp +++ b/sdk/storage/azure-storage-files-shares/test/share_file_client_test.cpp @@ -7,6 +7,7 @@ #include #include +#include #include #include #include @@ -600,6 +601,12 @@ namespace Azure { namespace Storage { namespace Test { } } + static std::vector Hash(const std::string& data) + { + Azure::Core::Cryptography::Md5Hash instance; + return instance.Final(reinterpret_cast(data.data()), data.size()); + } + TEST_F(FileShareFileClientTest, RangeUploadDownload) { auto rangeSize = 1 * 1024 * 1024; @@ -640,8 +647,9 @@ namespace Azure { namespace Storage { namespace Test { { // MD5 works. memBodyStream.Rewind(); - auto md5 = Md5::Hash(rangeContent.data(), rangeContent.size()); - auto invalidMd5 = Md5::Hash(std::string("This is garbage.")); + Azure::Core::Cryptography::Md5Hash instance; + auto md5 = instance.Final(rangeContent.data(), rangeContent.size()); + auto invalidMd5 = Hash(std::string("This is garbage.")); auto fileClient = m_shareClient->GetRootDirectoryClient().GetFileClient(LowercaseRandomString(10)); Files::Shares::UploadShareFileRangeOptions uploadOptions;