-
Notifications
You must be signed in to change notification settings - Fork 1
/
cpp-sha256-hmac.hpp
210 lines (160 loc) · 4.36 KB
/
cpp-sha256-hmac.hpp
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
/*
This file is part of cpp-sha256-hmac.
Copyright (C) 2020 ReimuNotMoe
This program is free software: you can redistribute it and/or modify
it under the terms of the MIT License.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Algorithm credits: https://github.com/jb55/sha256.c
https://github.com/aperezdc/hmac-sha256
*/
#include <vector>
#include <string>
#include <cstdlib>
#include <cinttypes>
#include <cstring>
namespace YukiWorkshop::Crypto {
class SHA256 {
protected:
uint32_t state[8];
uint64_t count;
unsigned char buffer[64];
void sha256_write_byte_block();
static void __update(SHA256 *p, const void *__data, size_t __len) noexcept;
static void __finalize(SHA256 *p, void *__digest) noexcept;
public:
SHA256() {
reset();
}
void reset() noexcept;
void update(const void *__data, size_t __len) noexcept;
void update(const char *__data) noexcept {
update(__data, strlen(__data));
}
template <typename T, typename A>
void update(const std::vector<T, A>& __input) {
update(__input.data(), __input.size() * sizeof(T));
}
template <typename T>
void update(const T& __input) {
update(__input.data(), __input.size());
}
auto finalize() {
struct {
SHA256 *p;
operator std::vector<uint8_t>() {
std::vector<uint8_t> ret(32);
__finalize(p, ret.data());
return ret;
}
operator std::string() {
std::string ret;
ret.resize(32);
__finalize(p, ret.data());
return ret;
}
} ret{this};
return ret;
}
void finalize(void *__digest) noexcept;
template <typename T, typename A>
friend SHA256& operator<<(SHA256& e, const std::vector<T, A>& s) {
__update(&e, s.data(), s.size() * sizeof(T));
return e;
}
template <typename T>
friend SHA256& operator<<(SHA256& e, const T& s) {
__update(&e, s.data(), s.size());
return e;
}
friend SHA256& operator<<(SHA256& e, const char *s) {
__update(&e, s, strlen(s));
return e;
}
template <typename T, typename A>
friend void operator>>(SHA256& e, std::vector<T, A>& s) {
s.resize(32 / sizeof(T));
__finalize(&e, s.data());
}
friend void operator>>(SHA256& e, void *s) {
__finalize(&e, s);
}
};
class SHA256_HMAC : SHA256 {
private:
uint8_t kx[64];
std::vector<uint8_t> key_buf;
static void __hmac_finalize(SHA256_HMAC *p, void *__digest) noexcept;
public:
SHA256_HMAC() {
reset();
};
SHA256_HMAC(const void *__data, size_t __len) {
reset();
set_key(__data, __len);
}
template <typename T, typename A>
SHA256_HMAC(const std::vector<T, A>& __input) {
reset();
set_key(__input.data(), __input.size() * sizeof(T));
}
template <typename T>
SHA256_HMAC(const T& __input) {
reset();
set_key(__input.data(), __input.size());
}
void set_key(const void *__data, size_t __len);
template <typename T, typename A>
void set_key(const std::vector<T, A>& __input) {
set_key(__input.data(), __input.size() * sizeof(T));
}
template <typename T>
void set_key(const T& __input) {
set_key(__input.data(), __input.size());
}
using SHA256::update;
void finalize(void *__digest) noexcept {
__hmac_finalize(this, __digest);
}
auto finalize() {
struct {
SHA256_HMAC *p;
operator std::vector<uint8_t>() {
std::vector<uint8_t> ret(32);
__hmac_finalize(p, ret.data());
return ret;
}
operator std::string() {
std::string ret;
ret.resize(32);
__hmac_finalize(p, ret.data());
return ret;
}
} ret{this};
return ret;
}
template <typename T, typename A>
friend SHA256_HMAC& operator<<(SHA256_HMAC& e, const std::vector<T, A>& s) {
__update(&e, s.data(), s.size() * sizeof(T));
return e;
}
template <typename T>
friend SHA256_HMAC& operator<<(SHA256_HMAC& e, const T& s) {
__update(&e, s.data(), s.size());
return e;
}
friend SHA256_HMAC& operator<<(SHA256_HMAC& e, const char *s) {
__update(&e, s, strlen(s));
return e;
}
template <typename T, typename A>
friend void operator>>(SHA256_HMAC& e, std::vector<T, A>& s) {
s.resize(32 / sizeof(T));
__hmac_finalize(&e, s.data());
}
friend void operator>>(SHA256_HMAC& e, void *s) {
__hmac_finalize(&e, s);
}
};
}