-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathHash.cpp
217 lines (185 loc) · 6.8 KB
/
Hash.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/crt/crypto/Hash.h>
#include <aws/cal/hash.h>
namespace Aws
{
namespace Crt
{
namespace Crypto
{
bool ComputeSHA256(
Allocator *allocator,
const ByteCursor &input,
ByteBuf &output,
size_t truncateTo) noexcept
{
auto hash = Hash::CreateSHA256(allocator);
return hash.ComputeOneShot(input, output, truncateTo);
}
bool ComputeSHA256(const ByteCursor &input, ByteBuf &output, size_t truncateTo) noexcept
{
return ComputeSHA256(ApiAllocator(), input, output, truncateTo);
}
bool ComputeSHA1(Allocator *allocator, const ByteCursor &input, ByteBuf &output, size_t truncateTo) noexcept
{
auto hash = Hash::CreateSHA1(allocator);
return hash.ComputeOneShot(input, output, truncateTo);
}
bool ComputeSHA1(const ByteCursor &input, ByteBuf &output, size_t truncateTo) noexcept
{
return ComputeSHA1(ApiAllocator(), input, output, truncateTo);
}
bool ComputeMD5(Allocator *allocator, const ByteCursor &input, ByteBuf &output, size_t truncateTo) noexcept
{
auto hash = Hash::CreateMD5(allocator);
return hash.ComputeOneShot(input, output, truncateTo);
}
bool ComputeMD5(const ByteCursor &input, ByteBuf &output, size_t truncateTo) noexcept
{
auto hash = Hash::CreateMD5(ApiAllocator());
return hash.ComputeOneShot(input, output, truncateTo);
}
Hash::Hash(aws_hash *hash) noexcept : m_hash(hash), m_lastError(0)
{
if (!hash)
{
m_lastError = aws_last_error();
}
}
Hash::~Hash()
{
if (m_hash)
{
aws_hash_destroy(m_hash);
m_hash = nullptr;
}
}
Hash::Hash(Hash &&toMove) : m_hash(toMove.m_hash), m_lastError(toMove.m_lastError)
{
toMove.m_hash = nullptr;
}
Hash::operator bool() const noexcept
{
return m_hash != nullptr && m_hash->good;
}
Hash &Hash::operator=(Hash &&toMove)
{
if (&toMove != this)
{
*this = Hash(std::move(toMove));
}
return *this;
}
Hash Hash::CreateSHA256(Allocator *allocator) noexcept
{
return Hash(aws_sha256_new(allocator));
}
Hash Hash::CreateMD5(Allocator *allocator) noexcept
{
return Hash(aws_md5_new(allocator));
}
Hash Hash::CreateSHA1(Allocator *allocator) noexcept
{
return Hash(aws_sha1_new(allocator));
}
bool Hash::Update(const ByteCursor &toHash) noexcept
{
if (!*this)
{
return false;
}
if (AWS_OP_SUCCESS != aws_hash_update(m_hash, &toHash))
{
m_lastError = aws_last_error();
return false;
}
return true;
}
bool Hash::Digest(ByteBuf &output, size_t truncateTo) noexcept
{
if (!*this)
{
return false;
}
if (aws_hash_finalize(m_hash, &output, truncateTo) != AWS_OP_SUCCESS)
{
m_lastError = aws_last_error();
return false;
}
return true;
}
bool Hash::ComputeOneShot(const ByteCursor &input, ByteBuf &output, size_t truncateTo) noexcept
{
if (!*this || !Update(input))
{
return false;
}
return Digest(output, truncateTo);
}
size_t Hash::DigestSize() const noexcept
{
if (m_hash != nullptr)
{
return m_hash->digest_size;
}
return 0;
}
aws_hash_vtable ByoHash::s_Vtable = {
"aws-crt-cpp-byo-crypto-hash",
"aws-crt-cpp-byo-crypto",
ByoHash::s_Destroy,
ByoHash::s_Update,
ByoHash::s_Finalize,
};
ByoHash::ByoHash(size_t digestSize, Allocator *allocator)
{
AWS_ZERO_STRUCT(m_hashValue);
m_hashValue.vtable = &s_Vtable;
m_hashValue.allocator = allocator;
m_hashValue.impl = reinterpret_cast<void *>(this);
m_hashValue.digest_size = digestSize;
m_hashValue.good = true;
}
ByoHash::~ByoHash() {}
aws_hash *ByoHash::SeatForCInterop(const std::shared_ptr<ByoHash> &selfRef)
{
AWS_FATAL_ASSERT(this == selfRef.get());
m_selfReference = selfRef;
return &m_hashValue;
}
void ByoHash::s_Destroy(struct aws_hash *hash)
{
auto *byoHash = reinterpret_cast<ByoHash *>(hash->impl);
byoHash->m_selfReference = nullptr;
}
int ByoHash::s_Update(struct aws_hash *hash, const struct aws_byte_cursor *buf)
{
auto *byoHash = reinterpret_cast<ByoHash *>(hash->impl);
if (!byoHash->m_hashValue.good)
{
return aws_raise_error(AWS_ERROR_INVALID_STATE);
}
if (!byoHash->UpdateInternal(*buf))
{
byoHash->m_hashValue.good = false;
return AWS_OP_ERR;
}
return AWS_OP_SUCCESS;
}
int ByoHash::s_Finalize(struct aws_hash *hash, struct aws_byte_buf *out)
{
auto *byoHash = reinterpret_cast<ByoHash *>(hash->impl);
if (!byoHash->m_hashValue.good)
{
return aws_raise_error(AWS_ERROR_INVALID_STATE);
}
bool success = byoHash->DigestInternal(*out);
byoHash->m_hashValue.good = false;
return success ? AWS_OP_SUCCESS : AWS_OP_ERR;
}
} // namespace Crypto
} // namespace Crt
} // namespace Aws