azure-core
Loading...
Searching...
No Matches
hash.hpp
Go to the documentation of this file.
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
10#pragma once
11
13
14#include <memory>
15#include <stdexcept>
16#include <stdint.h>
17#include <string>
18#include <vector>
19
20namespace Azure { namespace Core { namespace Cryptography {
21
26 class Hash {
27 private:
36 virtual void OnAppend(const uint8_t* data, size_t length) = 0;
37
46 virtual std::vector<uint8_t> OnFinal(const uint8_t* data, size_t length) = 0;
47
48 protected:
53 Hash() = default;
54
55 public:
65 void Append(const uint8_t* data, size_t length)
66 {
67 AZURE_ASSERT(data || length == 0);
68 AZURE_ASSERT_MSG(!m_isDone, "Cannot call Append after calling Final().");
69 OnAppend(data, length);
70 }
71
81 std::vector<uint8_t> Final(const uint8_t* data, size_t length)
82 {
83 AZURE_ASSERT(data || length == 0);
84 AZURE_ASSERT_MSG(!m_isDone, "Cannot call Final() multiple times.");
85 m_isDone = true;
86 return OnFinal(data, length);
87 }
88
96 std::vector<uint8_t> Final() { return Final(nullptr, 0); }
97
102 virtual ~Hash() = default;
103
104 private:
105 bool m_isDone = false;
106
111 Hash(Hash const&) = delete;
112
117 void operator=(Hash const&) = delete;
118 };
119
129 class Md5Hash final : public Hash {
130
131 public:
137
142 ~Md5Hash() override;
143
144 private:
145 std::unique_ptr<Hash> m_implementation;
146
155 std::vector<uint8_t> OnFinal(const uint8_t* data, size_t length) override;
156
166 void OnAppend(const uint8_t* data, size_t length) override;
167 };
168
169}}} // namespace Azure::Core::Cryptography
Provide assert macros to use with pre-conditions.
#define AZURE_ASSERT(exp)
Azure specific assert macro.
Definition azure_assert.hpp:51
#define AZURE_ASSERT_MSG(exp, msg)
Azure specific assert macro with message.
Definition azure_assert.hpp:53
Represents the base class for hash algorithms which map binary data of an arbitrary length to small b...
Definition hash.hpp:26
virtual ~Hash()=default
Destructs Hash.
Hash()=default
Constructs a default instance of Hash.
std::vector< uint8_t > Final(const uint8_t *data, size_t length)
Computes the hash value of the specified binary input data, including any previously appended.
Definition hash.hpp:81
void Append(const uint8_t *data, size_t length)
Used to append partial binary input data to compute the hash in a streaming fashion.
Definition hash.hpp:65
std::vector< uint8_t > Final()
Computes the hash value of all the binary input data appended to the instance so far.
Definition hash.hpp:96
Represents the class for the MD5 hash function which maps binary data of an arbitrary length to small...
Definition hash.hpp:129
~Md5Hash() override
Destructs Md5Hash.
Md5Hash()
Construct a default instance of Azure::Core::Cryptography::Md5Hash.
Compute the hash value for the input binary data, using SHA256, SHA384 and SHA512.
Definition azure_assert.hpp:57