aws-crt-cpp
C++ wrapper around the aws-c-* libraries. Provides Cross-Platform Transport Protocols and SSL/TLS implementations for C++.
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
bool AWS_CRT_CPP_API ComputeMD5(Allocator *allocator, const ByteCursor &input, ByteBuf &output, size_t truncateTo=0) noexcept
Definition: Hash.cpp:56
#define AWS_CRT_CPP_API
Definition: Exports.h:36
std::function< std::shared_ptr< ByoHash >(size_t digestSize, Allocator *)> CreateHashCallback
Definition: Hash.h:235
int LastError() const noexcept
Definition: Hash.h:127
Definition: Hash.h:196
Definition: Hash.h:110
aws_byte_buf ByteBuf
Definition: Types.h:30
aws_allocator Allocator
Definition: Allocator.h:14
aws_byte_cursor ByteCursor
Definition: Types.h:31
static const size_t SHA256_DIGEST_SIZE
Definition: Hash.h:19
static const size_t SHA512_DIGEST_SIZE
Definition: Hash.h:20
static const size_t MD5_DIGEST_SIZE
Definition: Hash.h:21
bool AWS_CRT_CPP_API ComputeSHA256(Allocator *allocator, const ByteCursor &input, ByteBuf &output, size_t truncateTo=0) noexcept
Definition: Hash.cpp:15
AWS_CRT_CPP_API Allocator * ApiAllocator() noexcept
Definition: Allocator.cpp:24
bool AWS_CRT_CPP_API ComputeSHA512(Allocator *allocator, const ByteCursor &input, ByteBuf &output, size_t truncateTo=0) noexcept
Definition: Hash.cpp:30
static const size_t SHA1_DIGEST_SIZE
Definition: Hash.h:18
bool AWS_CRT_CPP_API ComputeSHA1(Allocator *allocator, const ByteCursor &input, ByteBuf &output, size_t truncateTo=0) noexcept
Definition: Hash.cpp:45
Definition: Allocator.h:10