AWS IoT Device SDK C++ v2  1.42.1
AWS IoT Device SDK C++ v2
Hash.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include <aws/crt/Exports.h>
7 #include <aws/crt/Types.h>
8 
9 #include <aws/cal/hash.h>
10 
11 struct aws_hash;
12 namespace Aws
13 {
14  namespace Crt
15  {
16  namespace Crypto
17  {
18  static const size_t SHA1_DIGEST_SIZE = AWS_SHA1_LEN;
19  static const size_t SHA256_DIGEST_SIZE = AWS_SHA256_LEN;
20  static const size_t SHA512_DIGEST_SIZE = AWS_SHA512_LEN;
21  static const size_t MD5_DIGEST_SIZE = AWS_MD5_LEN;
22 
30  Allocator *allocator,
31  const ByteCursor &input,
32  ByteBuf &output,
33  size_t truncateTo = 0) noexcept;
34 
41  bool AWS_CRT_CPP_API
42  ComputeSHA256(const ByteCursor &input, ByteBuf &output, size_t truncateTo = 0) noexcept;
43 
51  Allocator *allocator,
52  const ByteCursor &input,
53  ByteBuf &output,
54  size_t truncateTo = 0) noexcept;
55 
62  bool AWS_CRT_CPP_API ComputeMD5(const ByteCursor &input, ByteBuf &output, size_t truncateTo = 0) noexcept;
63 
71  Allocator *allocator,
72  const ByteCursor &input,
73  ByteBuf &output,
74  size_t truncateTo = 0) noexcept;
75 
82  bool AWS_CRT_CPP_API ComputeSHA1(const ByteCursor &input, ByteBuf &output, size_t truncateTo = 0) noexcept;
83 
91  Allocator *allocator,
92  const ByteCursor &input,
93  ByteBuf &output,
94  size_t truncateTo = 0) noexcept;
95 
102  bool AWS_CRT_CPP_API
103  ComputeSHA512(const ByteCursor &input, ByteBuf &output, size_t truncateTo = 0) noexcept;
104 
110  class AWS_CRT_CPP_API Hash final
111  {
112  public:
113  ~Hash();
114  Hash(const Hash &) = delete;
115  Hash &operator=(const Hash &) = delete;
116  Hash(Hash &&toMove);
117  Hash &operator=(Hash &&toMove);
118 
122  operator bool() const noexcept;
123 
127  inline int LastError() const noexcept { return m_lastError; }
128 
132  static Hash CreateSHA256(Allocator *allocator = ApiAllocator()) noexcept;
133 
137  static Hash CreateSHA1(Allocator *allocator = ApiAllocator()) noexcept;
138 
142  static Hash CreateSHA512(Allocator *allocator = ApiAllocator()) noexcept;
143 
147  static Hash CreateMD5(Allocator *allocator = ApiAllocator()) noexcept;
148 
153  bool Update(const ByteCursor &toHash) noexcept;
154 
162  bool Digest(ByteBuf &output, size_t truncateTo = 0) noexcept;
163 
174  bool ComputeOneShot(const ByteCursor &input, ByteBuf &output, size_t truncateTo = 0) noexcept;
175 
180  size_t DigestSize() const noexcept;
181 
182  private:
183  Hash(aws_hash *hash) noexcept;
184  Hash() = delete;
185 
186  aws_hash *m_hash;
187  int m_lastError;
188  };
189 
197  {
198  public:
199  virtual ~ByoHash();
200 
205  aws_hash *SeatForCInterop(const std::shared_ptr<ByoHash> &selfRef);
206 
207  protected:
208  ByoHash(size_t digestSize, Allocator *allocator = ApiAllocator());
209 
215  virtual bool UpdateInternal(const ByteCursor &toHash) noexcept = 0;
216 
223  virtual bool DigestInternal(ByteBuf &output, size_t truncateTo = 0) noexcept = 0;
224 
225  private:
226  static void s_Destroy(struct aws_hash *hash);
227  static int s_Update(struct aws_hash *hash, const struct aws_byte_cursor *buf);
228  static int s_Finalize(struct aws_hash *hash, struct aws_byte_buf *out);
229 
230  static aws_hash_vtable s_Vtable;
231  aws_hash m_hashValue;
232  std::shared_ptr<ByoHash> m_selfReference;
233  };
234 
235  using CreateHashCallback = std::function<std::shared_ptr<ByoHash>(size_t digestSize, Allocator *)>;
236 
237  } // namespace Crypto
238  } // namespace Crt
239 } // namespace Aws
Aws::Crt::Crypto::SHA512_DIGEST_SIZE
static const size_t SHA512_DIGEST_SIZE
Definition: Hash.h:20
Aws::Crt::Crypto::ByoHash::UpdateInternal
virtual bool UpdateInternal(const ByteCursor &toHash) noexcept=0
Aws::Crt::ApiAllocator
AWS_CRT_CPP_API Allocator * ApiAllocator() noexcept
Definition: Allocator.cpp:24
Aws::Crt::LastError
AWS_CRT_CPP_API int LastError() noexcept
Definition: Api.cpp:464
Aws::Crt::Crypto::ComputeSHA1
bool AWS_CRT_CPP_API ComputeSHA1(Allocator *allocator, const ByteCursor &input, ByteBuf &output, size_t truncateTo=0) noexcept
Definition: Hash.cpp:45
Aws::Crt::Crypto::SHA1_DIGEST_SIZE
static const size_t SHA1_DIGEST_SIZE
Definition: Hash.h:18
Aws::Crt::Crypto::Hash
Definition: Hash.h:111
Aws::Crt::Crypto::ComputeMD5
bool AWS_CRT_CPP_API ComputeMD5(Allocator *allocator, const ByteCursor &input, ByteBuf &output, size_t truncateTo=0) noexcept
Definition: Hash.cpp:56
Aws::Crt::Crypto::Hash::Hash
Hash(const Hash &)=delete
Aws::Crt::Crypto::ByoHash::DigestInternal
virtual bool DigestInternal(ByteBuf &output, size_t truncateTo=0) noexcept=0
Aws::Crt::Crypto::ComputeSHA256
bool AWS_CRT_CPP_API ComputeSHA256(Allocator *allocator, const ByteCursor &input, ByteBuf &output, size_t truncateTo=0) noexcept
Definition: Hash.cpp:15
Aws::Crt::Crypto::SHA256_DIGEST_SIZE
static const size_t SHA256_DIGEST_SIZE
Definition: Hash.h:19
Aws::Crt::Crypto::CreateHashCallback
std::function< std::shared_ptr< ByoHash >(size_t digestSize, Allocator *)> CreateHashCallback
Definition: Hash.h:235
Aws::Crt::Crypto::MD5_DIGEST_SIZE
static const size_t MD5_DIGEST_SIZE
Definition: Hash.h:21
Aws::Crt::Crypto::Hash::operator=
Hash & operator=(const Hash &)=delete
Types.h
Aws::Crt::Crypto::ComputeSHA512
bool AWS_CRT_CPP_API ComputeSHA512(Allocator *allocator, const ByteCursor &input, ByteBuf &output, size_t truncateTo=0) noexcept
Definition: Hash.cpp:30
Aws::Crt::Crypto::ByoHash
Definition: Hash.h:197
Aws::Crt::ByteBuf
aws_byte_buf ByteBuf
Definition: Types.h:30
std
Definition: StringView.h:862
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:36
Aws
Definition: Allocator.h:11
Aws::Crt::ByteCursor
aws_byte_cursor ByteCursor
Definition: Types.h:31
Aws::Crt::Allocator
aws_allocator Allocator
Definition: Allocator.h:14
Exports.h