Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
revoxhere
GitHub Repository: revoxhere/duino-coin
Path: blob/master/ESP_Code/DSHA1.h
925 views
1
#ifndef DSHA1_H
2
#define DSHA1_H
3
4
#include <Arduino.h>
5
6
class DSHA1 {
7
8
public:
9
static const size_t OUTPUT_SIZE = 20;
10
11
DSHA1() {
12
initialize(s);
13
}
14
15
DSHA1 &write(const unsigned char *data, size_t len) {
16
size_t bufsize = bytes % 64;
17
if (bufsize && bufsize + len >= 64) {
18
memcpy(buf + bufsize, data, 64 - bufsize);
19
bytes += 64 - bufsize;
20
data += 64 - bufsize;
21
transform(s, buf);
22
bufsize = 0;
23
}
24
while (len >= 64) {
25
transform(s, data);
26
bytes += 64;
27
data += 64;
28
len -= 64;
29
}
30
if (len > 0) {
31
memcpy(buf + bufsize, data, len);
32
bytes += len;
33
}
34
return *this;
35
}
36
37
void finalize(unsigned char hash[OUTPUT_SIZE]) {
38
const unsigned char pad[64] = {0x80};
39
unsigned char sizedesc[8];
40
writeBE64(sizedesc, bytes << 3);
41
write(pad, 1 + ((119 - (bytes % 64)) % 64));
42
write(sizedesc, 8);
43
writeBE32(hash, s[0]);
44
writeBE32(hash + 4, s[1]);
45
writeBE32(hash + 8, s[2]);
46
writeBE32(hash + 12, s[3]);
47
writeBE32(hash + 16, s[4]);
48
}
49
50
DSHA1 &reset() {
51
bytes = 0;
52
initialize(s);
53
return *this;
54
}
55
56
// Warmup the cache and get a boost in performance
57
DSHA1 &warmup() {
58
uint8_t warmup[20];
59
this->write((uint8_t *)"warmupwarmupwa", 20).finalize(warmup);
60
return *this;
61
}
62
63
private:
64
uint32_t s[5];
65
unsigned char buf[64];
66
uint64_t bytes;
67
68
const uint32_t k1 = 0x5A827999ul;
69
const uint32_t k2 = 0x6ED9EBA1ul;
70
const uint32_t k3 = 0x8F1BBCDCul;
71
const uint32_t k4 = 0xCA62C1D6ul;
72
73
uint32_t inline f1(uint32_t b, uint32_t c, uint32_t d) { return d ^ (b & (c ^ d)); }
74
uint32_t inline f2(uint32_t b, uint32_t c, uint32_t d) { return b ^ c ^ d; }
75
uint32_t inline f3(uint32_t b, uint32_t c, uint32_t d) { return (b & c) | (d & (b | c)); }
76
77
uint32_t inline left(uint32_t x) { return (x << 1) | (x >> 31); }
78
79
void inline Round(uint32_t a, uint32_t &b, uint32_t c, uint32_t d, uint32_t &e,
80
uint32_t f, uint32_t k, uint32_t w) {
81
e += ((a << 5) | (a >> 27)) + f + k + w;
82
b = (b << 30) | (b >> 2);
83
}
84
85
void initialize(uint32_t s[5]) {
86
s[0] = 0x67452301ul;
87
s[1] = 0xEFCDAB89ul;
88
s[2] = 0x98BADCFEul;
89
s[3] = 0x10325476ul;
90
s[4] = 0xC3D2E1F0ul;
91
}
92
93
void transform(uint32_t *s, const unsigned char *chunk) {
94
uint32_t a = s[0], b = s[1], c = s[2], d = s[3], e = s[4];
95
uint32_t w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15;
96
97
Round(a, b, c, d, e, f1(b, c, d), k1, w0 = readBE32(chunk + 0));
98
Round(e, a, b, c, d, f1(a, b, c), k1, w1 = readBE32(chunk + 4));
99
Round(d, e, a, b, c, f1(e, a, b), k1, w2 = readBE32(chunk + 8));
100
Round(c, d, e, a, b, f1(d, e, a), k1, w3 = readBE32(chunk + 12));
101
Round(b, c, d, e, a, f1(c, d, e), k1, w4 = readBE32(chunk + 16));
102
Round(a, b, c, d, e, f1(b, c, d), k1, w5 = readBE32(chunk + 20));
103
Round(e, a, b, c, d, f1(a, b, c), k1, w6 = readBE32(chunk + 24));
104
Round(d, e, a, b, c, f1(e, a, b), k1, w7 = readBE32(chunk + 28));
105
Round(c, d, e, a, b, f1(d, e, a), k1, w8 = readBE32(chunk + 32));
106
Round(b, c, d, e, a, f1(c, d, e), k1, w9 = readBE32(chunk + 36));
107
Round(a, b, c, d, e, f1(b, c, d), k1, w10 = readBE32(chunk + 40));
108
Round(e, a, b, c, d, f1(a, b, c), k1, w11 = readBE32(chunk + 44));
109
Round(d, e, a, b, c, f1(e, a, b), k1, w12 = readBE32(chunk + 48));
110
Round(c, d, e, a, b, f1(d, e, a), k1, w13 = readBE32(chunk + 52));
111
Round(b, c, d, e, a, f1(c, d, e), k1, w14 = readBE32(chunk + 56));
112
Round(a, b, c, d, e, f1(b, c, d), k1, w15 = readBE32(chunk + 60));
113
114
Round(e, a, b, c, d, f1(a, b, c), k1, w0 = left(w0 ^ w13 ^ w8 ^ w2));
115
Round(d, e, a, b, c, f1(e, a, b), k1, w1 = left(w1 ^ w14 ^ w9 ^ w3));
116
Round(c, d, e, a, b, f1(d, e, a), k1, w2 = left(w2 ^ w15 ^ w10 ^ w4));
117
Round(b, c, d, e, a, f1(c, d, e), k1, w3 = left(w3 ^ w0 ^ w11 ^ w5));
118
Round(a, b, c, d, e, f2(b, c, d), k2, w4 = left(w4 ^ w1 ^ w12 ^ w6));
119
Round(e, a, b, c, d, f2(a, b, c), k2, w5 = left(w5 ^ w2 ^ w13 ^ w7));
120
Round(d, e, a, b, c, f2(e, a, b), k2, w6 = left(w6 ^ w3 ^ w14 ^ w8));
121
Round(c, d, e, a, b, f2(d, e, a), k2, w7 = left(w7 ^ w4 ^ w15 ^ w9));
122
Round(b, c, d, e, a, f2(c, d, e), k2, w8 = left(w8 ^ w5 ^ w0 ^ w10));
123
Round(a, b, c, d, e, f2(b, c, d), k2, w9 = left(w9 ^ w6 ^ w1 ^ w11));
124
Round(e, a, b, c, d, f2(a, b, c), k2, w10 = left(w10 ^ w7 ^ w2 ^ w12));
125
Round(d, e, a, b, c, f2(e, a, b), k2, w11 = left(w11 ^ w8 ^ w3 ^ w13));
126
Round(c, d, e, a, b, f2(d, e, a), k2, w12 = left(w12 ^ w9 ^ w4 ^ w14));
127
Round(b, c, d, e, a, f2(c, d, e), k2, w13 = left(w13 ^ w10 ^ w5 ^ w15));
128
Round(a, b, c, d, e, f2(b, c, d), k2, w14 = left(w14 ^ w11 ^ w6 ^ w0));
129
Round(e, a, b, c, d, f2(a, b, c), k2, w15 = left(w15 ^ w12 ^ w7 ^ w1));
130
131
Round(d, e, a, b, c, f2(e, a, b), k2, w0 = left(w0 ^ w13 ^ w8 ^ w2));
132
Round(c, d, e, a, b, f2(d, e, a), k2, w1 = left(w1 ^ w14 ^ w9 ^ w3));
133
Round(b, c, d, e, a, f2(c, d, e), k2, w2 = left(w2 ^ w15 ^ w10 ^ w4));
134
Round(a, b, c, d, e, f2(b, c, d), k2, w3 = left(w3 ^ w0 ^ w11 ^ w5));
135
Round(e, a, b, c, d, f2(a, b, c), k2, w4 = left(w4 ^ w1 ^ w12 ^ w6));
136
Round(d, e, a, b, c, f2(e, a, b), k2, w5 = left(w5 ^ w2 ^ w13 ^ w7));
137
Round(c, d, e, a, b, f2(d, e, a), k2, w6 = left(w6 ^ w3 ^ w14 ^ w8));
138
Round(b, c, d, e, a, f2(c, d, e), k2, w7 = left(w7 ^ w4 ^ w15 ^ w9));
139
Round(a, b, c, d, e, f3(b, c, d), k3, w8 = left(w8 ^ w5 ^ w0 ^ w10));
140
Round(e, a, b, c, d, f3(a, b, c), k3, w9 = left(w9 ^ w6 ^ w1 ^ w11));
141
Round(d, e, a, b, c, f3(e, a, b), k3, w10 = left(w10 ^ w7 ^ w2 ^ w12));
142
Round(c, d, e, a, b, f3(d, e, a), k3, w11 = left(w11 ^ w8 ^ w3 ^ w13));
143
Round(b, c, d, e, a, f3(c, d, e), k3, w12 = left(w12 ^ w9 ^ w4 ^ w14));
144
Round(a, b, c, d, e, f3(b, c, d), k3, w13 = left(w13 ^ w10 ^ w5 ^ w15));
145
Round(e, a, b, c, d, f3(a, b, c), k3, w14 = left(w14 ^ w11 ^ w6 ^ w0));
146
Round(d, e, a, b, c, f3(e, a, b), k3, w15 = left(w15 ^ w12 ^ w7 ^ w1));
147
148
Round(c, d, e, a, b, f3(d, e, a), k3, w0 = left(w0 ^ w13 ^ w8 ^ w2));
149
Round(b, c, d, e, a, f3(c, d, e), k3, w1 = left(w1 ^ w14 ^ w9 ^ w3));
150
Round(a, b, c, d, e, f3(b, c, d), k3, w2 = left(w2 ^ w15 ^ w10 ^ w4));
151
Round(e, a, b, c, d, f3(a, b, c), k3, w3 = left(w3 ^ w0 ^ w11 ^ w5));
152
Round(d, e, a, b, c, f3(e, a, b), k3, w4 = left(w4 ^ w1 ^ w12 ^ w6));
153
Round(c, d, e, a, b, f3(d, e, a), k3, w5 = left(w5 ^ w2 ^ w13 ^ w7));
154
Round(b, c, d, e, a, f3(c, d, e), k3, w6 = left(w6 ^ w3 ^ w14 ^ w8));
155
Round(a, b, c, d, e, f3(b, c, d), k3, w7 = left(w7 ^ w4 ^ w15 ^ w9));
156
Round(e, a, b, c, d, f3(a, b, c), k3, w8 = left(w8 ^ w5 ^ w0 ^ w10));
157
Round(d, e, a, b, c, f3(e, a, b), k3, w9 = left(w9 ^ w6 ^ w1 ^ w11));
158
Round(c, d, e, a, b, f3(d, e, a), k3, w10 = left(w10 ^ w7 ^ w2 ^ w12));
159
Round(b, c, d, e, a, f3(c, d, e), k3, w11 = left(w11 ^ w8 ^ w3 ^ w13));
160
Round(a, b, c, d, e, f2(b, c, d), k4, w12 = left(w12 ^ w9 ^ w4 ^ w14));
161
Round(e, a, b, c, d, f2(a, b, c), k4, w13 = left(w13 ^ w10 ^ w5 ^ w15));
162
Round(d, e, a, b, c, f2(e, a, b), k4, w14 = left(w14 ^ w11 ^ w6 ^ w0));
163
Round(c, d, e, a, b, f2(d, e, a), k4, w15 = left(w15 ^ w12 ^ w7 ^ w1));
164
165
Round(b, c, d, e, a, f2(c, d, e), k4, w0 = left(w0 ^ w13 ^ w8 ^ w2));
166
Round(a, b, c, d, e, f2(b, c, d), k4, w1 = left(w1 ^ w14 ^ w9 ^ w3));
167
Round(e, a, b, c, d, f2(a, b, c), k4, w2 = left(w2 ^ w15 ^ w10 ^ w4));
168
Round(d, e, a, b, c, f2(e, a, b), k4, w3 = left(w3 ^ w0 ^ w11 ^ w5));
169
Round(c, d, e, a, b, f2(d, e, a), k4, w4 = left(w4 ^ w1 ^ w12 ^ w6));
170
Round(b, c, d, e, a, f2(c, d, e), k4, w5 = left(w5 ^ w2 ^ w13 ^ w7));
171
Round(a, b, c, d, e, f2(b, c, d), k4, w6 = left(w6 ^ w3 ^ w14 ^ w8));
172
Round(e, a, b, c, d, f2(a, b, c), k4, w7 = left(w7 ^ w4 ^ w15 ^ w9));
173
Round(d, e, a, b, c, f2(e, a, b), k4, w8 = left(w8 ^ w5 ^ w0 ^ w10));
174
Round(c, d, e, a, b, f2(d, e, a), k4, w9 = left(w9 ^ w6 ^ w1 ^ w11));
175
Round(b, c, d, e, a, f2(c, d, e), k4, w10 = left(w10 ^ w7 ^ w2 ^ w12));
176
Round(a, b, c, d, e, f2(b, c, d), k4, w11 = left(w11 ^ w8 ^ w3 ^ w13));
177
Round(e, a, b, c, d, f2(a, b, c), k4, w12 = left(w12 ^ w9 ^ w4 ^ w14));
178
Round(d, e, a, b, c, f2(e, a, b), k4, left(w13 ^ w10 ^ w5 ^ w15));
179
Round(c, d, e, a, b, f2(d, e, a), k4, left(w14 ^ w11 ^ w6 ^ w0));
180
Round(b, c, d, e, a, f2(c, d, e), k4, left(w15 ^ w12 ^ w7 ^ w1));
181
182
s[0] += a;
183
s[1] += b;
184
s[2] += c;
185
s[3] += d;
186
s[4] += e;
187
}
188
189
uint32_t static inline readBE32(const unsigned char *ptr) {
190
return __builtin_bswap32(*(uint32_t *)ptr);
191
}
192
193
void static inline writeBE32(unsigned char *ptr, uint32_t x) {
194
*(uint32_t *)ptr = __builtin_bswap32(x);
195
}
196
197
void static inline writeBE64(unsigned char *ptr, uint64_t x) {
198
*(uint64_t *)ptr = __builtin_bswap64(x);
199
}
200
};
201
#endif
202
203