The SHA-1 algorithm itself, taking in a bytestring.
83 {
84 auto* input = static_cast<const uint8_t*>(input_bs);
85
86
87 uint32_t h0 = 0x67452301, a = 0;
88 uint32_t h1 = 0xEFCDAB89, b = 0;
89 uint32_t h2 = 0x98BADCFE, c = 0;
90 uint32_t h3 = 0x10325476, d = 0;
91 uint32_t h4 = 0xC3D2E1F0, e = 0;
92
93
94
95
96 uint64_t padded_message_size = 0;
97 if (input_size % 64 < 56) {
98 padded_message_size = input_size + 64 - (input_size % 64);
99 } else {
100 padded_message_size = input_size + 128 - (input_size % 64);
101 }
102
103
105
106
107 std::copy(input, input + input_size, padded_message.begin());
108
109
110 padded_message[input_size] = 1 << 7;
111 for (uint64_t i = input_size; i % 64 != 56; i++) {
112 if (i == input_size) {
113 continue;
114 }
115 padded_message[i] = 0;
116 }
117
118
119
120 uint64_t input_bitsize = input_size * 8;
121 for (uint8_t i = 0; i < 8; i++) {
122 padded_message[padded_message_size - 8 + i] =
123 (input_bitsize >> (56 - 8 * i)) & 0xFF;
124 }
125
126
128
129
130 for (uint64_t chunk = 0; chunk * 64 < padded_message_size; chunk++) {
131
132 for (uint8_t bid = 0; bid < 16; bid++) {
133 blocks[bid] = 0;
134
135
136
137 for (uint8_t cid = 0; cid < 4; cid++) {
138 blocks[bid] = (blocks[bid] << 8) +
139 padded_message[chunk * 64 + bid * 4 + cid];
140 }
141
142
143 for (uint8_t i = 16; i < 80; i++) {
144 blocks[i] =
146 blocks[i - 14] ^ blocks[i - 16],
147 1);
148 }
149 }
150
151 a = h0;
152 b = h1;
153 c = h2;
154 d = h3;
155 e = h4;
156
157
158 for (uint8_t i = 0; i < 80; i++) {
159 uint32_t F = 0,
g = 0;
160 if (i < 20) {
161 F = (b & c) | ((~b) & d);
163 } else if (i < 40) {
164 F = b ^ c ^ d;
166 } else if (i < 60) {
167 F = (b & c) | (b & d) | (c & d);
169 } else {
170 F = b ^ c ^ d;
172 }
173
174
176 e = d;
177 d = c;
179 b = a;
180 a = temp;
181 }
182
183 h0 += a;
184 h1 += b;
185 h2 += c;
186 h3 += d;
187 h4 += e;
188 }
189
190
191
192
193 auto* sig = new uint8_t[20];
194 for (uint8_t i = 0; i < 4; i++) {
195 sig[i] = (h0 >> (24 - 8 * i)) & 0xFF;
196 sig[i + 4] = (h1 >> (24 - 8 * i)) & 0xFF;
197 sig[i + 8] = (h2 >> (24 - 8 * i)) & 0xFF;
198 sig[i + 12] = (h3 >> (24 - 8 * i)) & 0xFF;
199 sig[i + 16] = (h4 >> (24 - 8 * i)) & 0xFF;
200 }
201
202 return sig;
203}
double g(double x)
Another test function.
Definition composite_simpson_rule.cpp:115
uint32_t leftRotate32bits(uint32_t n, std::size_t rotate)
Rotates the bits of a 32-bit unsigned integer.
Definition md5.cpp:66