AWS SDK for C++

AWS SDK for C++ Version 1.11.612

Loading...
Searching...
No Matches
CRC.h
1
5#pragma once
6
7#include <aws/core/utils/Outcome.h>
8#include <aws/core/utils/crypto/Hash.h>
9#include <aws/core/utils/logging/LogMacros.h>
10#include <type_traits>
11
12namespace Aws {
13namespace Utils {
14namespace Crypto {
15template <typename HashT> ByteBuffer ConvertToBuffer(HashT value) {
16 static_assert(std::is_integral<HashT>::value,
17 "Must use integral type to convert to buffer");
18 Aws::Utils::ByteBuffer buffer(sizeof(HashT));
19 for (size_t i = 0; i < sizeof(HashT); ++i) {
20 size_t shiftSize = (sizeof(HashT) - 1) * 8 - i * 8;
21 buffer[i] = (value >> shiftSize) & 0xFF;
22 }
23 return buffer;
24}
25
26template <typename RunningChecksumT,
27 RunningChecksumT (*CRTChecksumFuncT)(Crt::ByteCursor,
28 RunningChecksumT),
29 ByteBuffer (*ByteBufferFuncT)(RunningChecksumT)>
30class CRCChecksum : public Hash {
31public:
32 CRCChecksum() : m_runningChecksum{0} {}
33
34 ~CRCChecksum() override = default;
35
36 HashResult Calculate(const Aws::String &str) override {
37 Aws::Crt::ByteCursor byteCursor = Aws::Crt::ByteCursorFromArray(
38 reinterpret_cast<const uint8_t *>(str.data()), str.size());
39 m_runningChecksum = CRTChecksumFuncT(byteCursor, m_runningChecksum);
40 return ByteBufferFuncT(m_runningChecksum);
41 };
42
43 HashResult Calculate(Aws::IStream &stream) override {
44 auto currentPos = stream.tellg();
45 if (stream.eof()) {
46 currentPos = 0;
47 stream.clear();
48 }
49
50 stream.seekg(0, Aws::IStream::beg);
51
52 uint8_t streamBuffer
54 while (stream.good()) {
55 stream.read(reinterpret_cast<char *>(streamBuffer),
57 const auto bytesRead = static_cast<size_t>(stream.gcount());
58
59 if (bytesRead > 0) {
60 Aws::Crt::ByteCursor byteCursor =
61 Aws::Crt::ByteCursorFromArray(streamBuffer, bytesRead);
62 m_runningChecksum = CRTChecksumFuncT(byteCursor, m_runningChecksum);
63 }
64 }
65
66 if (stream.bad()) {
67 AWS_LOGSTREAM_ERROR(
68 "CRCChecksum",
69 "Stream encountered an error while calculating CRC Checksum");
70 }
71
72 stream.clear();
73 stream.seekg(currentPos, Aws::IStream::beg);
74
75 return ByteBufferFuncT(m_runningChecksum);
76 };
77
78 void Update(unsigned char *buffer, size_t bufferSize) override {
79 Aws::Crt::ByteCursor byteCursor =
80 Aws::Crt::ByteCursorFromArray(buffer, bufferSize);
81 m_runningChecksum = CRTChecksumFuncT(byteCursor, m_runningChecksum);
82 };
83
84 HashResult GetHash() override { return ByteBufferFuncT(m_runningChecksum); };
85
86private:
87 RunningChecksumT m_runningChecksum;
88};
89} // namespace Crypto
90} // namespace Utils
91} // namespace Aws
HashResult GetHash() override
Definition CRC.h:84
HashResult Calculate(Aws::IStream &stream) override
Definition CRC.h:43
~CRCChecksum() override=default
HashResult Calculate(const Aws::String &str) override
Definition CRC.h:36
void Update(unsigned char *buffer, size_t bufferSize) override
Definition CRC.h:78
static const uint32_t INTERNAL_HASH_STREAM_BUFFER_SIZE
Definition Hash.h:50
ByteBuffer ConvertToBuffer(HashT value)
Definition CRC.h:15
std::basic_istream< char, std::char_traits< char > > IStream
std::basic_string< char, std::char_traits< char >, Aws::Allocator< char > > String
Definition AWSString.h:97