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 MD5_DIGEST_SIZE = AWS_MD5_LEN;
21 
29  Allocator *allocator,
30  const ByteCursor &input,
31  ByteBuf &output,
32  size_t truncateTo = 0) noexcept;
33 
40  bool AWS_CRT_CPP_API
41  ComputeSHA256(const ByteCursor &input, ByteBuf &output, size_t truncateTo = 0) noexcept;
42 
50  Allocator *allocator,
51  const ByteCursor &input,
52  ByteBuf &output,
53  size_t truncateTo = 0) noexcept;
54 
61  bool AWS_CRT_CPP_API ComputeMD5(const ByteCursor &input, ByteBuf &output, size_t truncateTo = 0) noexcept;
62 
70  Allocator *allocator,
71  const ByteCursor &input,
72  ByteBuf &output,
73  size_t truncateTo = 0) noexcept;
74 
81  bool AWS_CRT_CPP_API ComputeSHA1(const ByteCursor &input, ByteBuf &output, size_t truncateTo = 0) noexcept;
82 
88  class AWS_CRT_CPP_API Hash final
89  {
90  public:
91  ~Hash();
92  Hash(const Hash &) = delete;
93  Hash &operator=(const Hash &) = delete;
94  Hash(Hash &&toMove);
95  Hash &operator=(Hash &&toMove);
96 
100  operator bool() const noexcept;
101 
105  inline int LastError() const noexcept { return m_lastError; }
106 
110  static Hash CreateSHA256(Allocator *allocator = ApiAllocator()) noexcept;
111 
115  static Hash CreateSHA1(Allocator *allocator = ApiAllocator()) noexcept;
116 
120  static Hash CreateMD5(Allocator *allocator = ApiAllocator()) noexcept;
121 
126  bool Update(const ByteCursor &toHash) noexcept;
127 
135  bool Digest(ByteBuf &output, size_t truncateTo = 0) noexcept;
136 
147  bool ComputeOneShot(const ByteCursor &input, ByteBuf &output, size_t truncateTo = 0) noexcept;
148 
153  size_t DigestSize() const noexcept;
154 
155  private:
156  Hash(aws_hash *hash) noexcept;
157  Hash() = delete;
158 
159  aws_hash *m_hash;
160  int m_lastError;
161  };
162 
170  {
171  public:
172  virtual ~ByoHash();
173 
178  aws_hash *SeatForCInterop(const std::shared_ptr<ByoHash> &selfRef);
179 
180  protected:
181  ByoHash(size_t digestSize, Allocator *allocator = ApiAllocator());
182 
188  virtual bool UpdateInternal(const ByteCursor &toHash) noexcept = 0;
189 
196  virtual bool DigestInternal(ByteBuf &output, size_t truncateTo = 0) noexcept = 0;
197 
198  private:
199  static void s_Destroy(struct aws_hash *hash);
200  static int s_Update(struct aws_hash *hash, const struct aws_byte_cursor *buf);
201  static int s_Finalize(struct aws_hash *hash, struct aws_byte_buf *out);
202 
203  static aws_hash_vtable s_Vtable;
204  aws_hash m_hashValue;
205  std::shared_ptr<ByoHash> m_selfReference;
206  };
207 
208  using CreateHashCallback = std::function<std::shared_ptr<ByoHash>(size_t digestSize, Allocator *)>;
209 
210  } // namespace Crypto
211  } // namespace Crt
212 } // namespace Aws
bool AWS_CRT_CPP_API ComputeMD5(Allocator *allocator, const ByteCursor &input, ByteBuf &output, size_t truncateTo=0) noexcept
Definition: Hash.cpp:41
#define AWS_CRT_CPP_API
Definition: Exports.h:36
std::function< std::shared_ptr< ByoHash >(size_t digestSize, Allocator *)> CreateHashCallback
Definition: Hash.h:208
int LastError() const noexcept
Definition: Hash.h:105
Definition: Hash.h:169
Definition: Hash.h:88
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 MD5_DIGEST_SIZE
Definition: Hash.h:20
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
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:30
Definition: Allocator.h:10