Address implementation level feedback for Md5Hash and delete ctors. (#1647)
* Address implementation level feedback for Md5Hash and delete ctors. * Address docs feedback. * Address PR feedback * Move the copy ctor deletion from Md5Hash to the base class. * Remove re-assignment of Crc64Hash in tests.
This commit is contained in:
parent
fcd64f7bd2
commit
9015bfe399
@ -9,8 +9,8 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
#include <stdexcept>
|
||||
#include <stdint.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@ -42,6 +42,11 @@ namespace Azure { namespace Core { namespace Cryptography {
|
||||
virtual std::vector<uint8_t> 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<uint8_t> Final() { return Final(nullptr, 0); };
|
||||
std::vector<uint8_t> 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;
|
||||
|
||||
@ -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<std::size_t>(16_MB));
|
||||
Crc64Hash crc64Single;
|
||||
Crc64Hash crc64Streaming;
|
||||
|
||||
std::size_t length = 0;
|
||||
while (length < data.size())
|
||||
{
|
||||
std::size_t s = static_cast<std::size_t>(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<std::size_t>(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)
|
||||
{
|
||||
|
||||
Loading…
Reference in New Issue
Block a user