AWS IoT Device SDK C++ v2  1.34.0
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 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
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:422
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:30
Aws::Crt::Crypto::SHA1_DIGEST_SIZE
static const size_t SHA1_DIGEST_SIZE
Definition: Hash.h:18
Aws::Crt::Crypto::Hash
Definition: Hash.h:89
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:41
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:208
Aws::Crt::Crypto::MD5_DIGEST_SIZE
static const size_t MD5_DIGEST_SIZE
Definition: Hash.h:20
Aws::Crt::Crypto::Hash::operator=
Hash & operator=(const Hash &)=delete
Types.h
Aws::Crt::Crypto::ByoHash
Definition: Hash.h:170
Aws::Crt::ByteBuf
aws_byte_buf ByteBuf
Definition: Types.h:30
std
Definition: StringView.h:852
AWS_CRT_CPP_API
#define AWS_CRT_CPP_API
Definition: Exports.h:37
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