diff --git a/sdk/core/azure-core/inc/azure/core/cryptography/hash.hpp b/sdk/core/azure-core/inc/azure/core/cryptography/hash.hpp index 040bb5a39..401deccc1 100644 --- a/sdk/core/azure-core/inc/azure/core/cryptography/hash.hpp +++ b/sdk/core/azure-core/inc/azure/core/cryptography/hash.hpp @@ -9,8 +9,8 @@ #pragma once -#include #include +#include #include #include @@ -42,6 +42,11 @@ namespace Azure { namespace Core { namespace Cryptography { virtual std::vector OnFinal(const uint8_t* data, std::size_t length) = 0; public: + /** + * @brief Construct a default instance of #Azure::Core::Cryptography::Hash. + */ + Hash() = default; + /** * @brief Used to append partial binary input data to compute the hash in a streaming fashion. * @remark Once all the data has been added, call #Final() to get the computed hash value. @@ -57,10 +62,10 @@ namespace Azure { namespace Core { namespace Cryptography { throw std::invalid_argument( "Length cannot be " + std::to_string(length) + " if the data pointer is null."); } - if (m_isdone) + if (m_isDone) { throw std::runtime_error("Cannot call Append after calling Final()."); - }; + } OnAppend(data, length); } @@ -80,11 +85,11 @@ namespace Azure { namespace Core { namespace Cryptography { throw std::invalid_argument( "Length cannot be " + std::to_string(length) + " if the data pointer is null."); } - if (m_isdone) + if (m_isDone) { throw std::runtime_error("Cannot call Final() multiple times."); - }; - m_isdone = true; + } + m_isDone = true; return OnFinal(data, length); } @@ -94,15 +99,19 @@ namespace Azure { namespace Core { namespace Cryptography { * @remark Do not call this function multiple times. * @return The computed hash value corresponding to the input provided. */ - std::vector Final() { return Final(nullptr, 0); }; + std::vector Final() { return Final(nullptr, 0); } /** - * @brief Cleanup any state when destroying the instance of @Hash. + * @brief Cleanup any state when destroying the instance of #Azure::Core::Cryptography::Hash. */ - virtual ~Hash(){}; + virtual ~Hash() = default; private: - bool m_isdone = false; + bool m_isDone = false; + + // Delete the copy constructor, along with the assignment operator. + Hash(Hash const&) = delete; + void operator=(Hash const&) = delete; }; /** @@ -113,14 +122,14 @@ namespace Azure { namespace Core { namespace Cryptography { public: /** - * @brief Construct a default instance of @Md5Hash. + * @brief Construct a default instance of #Azure::Core::Cryptography::Md5Hash. */ - explicit Md5Hash(); + Md5Hash(); /** - * @brief Cleanup any state when destroying the instance of @Md5Hash. + * @brief Cleanup any state when destroying the instance of #Azure::Core::Cryptography::Md5Hash. */ - ~Md5Hash(); + ~Md5Hash() override; private: void* m_md5Context; 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 1b535db98..4913629ef 100644 --- a/sdk/storage/azure-storage-common/test/crypt_functions_test.cpp +++ b/sdk/storage/azure-storage-common/test/crypt_functions_test.cpp @@ -53,23 +53,25 @@ namespace Azure { namespace Storage { namespace Test { EXPECT_EQ(Azure::Core::Base64Encode(ComputeHash("Hello Azure!")), "DtjZpL9/o8c="); auto data = RandomBuffer(static_cast(16_MB)); - Crc64Hash crc64Single; - Crc64Hash crc64Streaming; - - 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); - crc64Streaming.Append(&data[length], s); - crc64Streaming.Append(&data[length], 0); - length += s; + Crc64Hash crc64Single; + Crc64Hash crc64Streaming; + + 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); + crc64Streaming.Append(&data[length], s); + crc64Streaming.Append(&data[length], 0); + length += s; + } + EXPECT_EQ(crc64Streaming.Final(), crc64Single.Final(data.data(), data.size())); } - EXPECT_EQ(crc64Streaming.Final(), crc64Single.Final(data.data(), data.size())); // Test concatenate - crc64Single = Crc64Hash(); - crc64Streaming = Crc64Hash(); + Crc64Hash crc64Single; + Crc64Hash crc64Streaming; std::string allData; while (allData.length() < 16_MB) {