84void* hash_bs(
const void* input_bs, uint64_t input_size) {
85 auto* input =
static_cast<const uint8_t*
>(input_bs);
88 uint32_t h0 = 0x67452301, a = 0;
89 uint32_t h1 = 0xEFCDAB89, b = 0;
90 uint32_t h2 = 0x98BADCFE, c = 0;
91 uint32_t h3 = 0x10325476, d = 0;
92 uint32_t h4 = 0xC3D2E1F0, e = 0;
97 uint64_t padded_message_size = 0;
98 if (input_size % 64 < 56) {
99 padded_message_size = input_size + 64 - (input_size % 64);
101 padded_message_size = input_size + 128 - (input_size % 64);
105 std::vector<uint8_t> padded_message(padded_message_size);
108 std::copy(input, input + input_size, padded_message.begin());
111 padded_message[input_size] = 1 << 7;
112 for (uint64_t i = input_size; i % 64 != 56; i++) {
113 if (i == input_size) {
116 padded_message[i] = 0;
121 uint64_t input_bitsize = input_size * 8;
122 for (uint8_t i = 0; i < 8; i++) {
123 padded_message[padded_message_size - 8 + i] =
124 (input_bitsize >> (56 - 8 * i)) & 0xFF;
128 std::array<uint32_t, 80> blocks{};
131 for (uint64_t chunk = 0; chunk * 64 < padded_message_size; chunk++) {
133 for (uint8_t bid = 0; bid < 16; bid++) {
138 for (uint8_t cid = 0; cid < 4; cid++) {
139 blocks[bid] = (blocks[bid] << 8) +
140 padded_message[chunk * 64 + bid * 4 + cid];
144 for (uint8_t i = 16; i < 80; i++) {
146 leftRotate32bits(blocks[i - 3] ^ blocks[i - 8] ^
147 blocks[i - 14] ^ blocks[i - 16],
159 for (uint8_t i = 0; i < 80; i++) {
160 uint32_t F = 0, g = 0;
162 F = (b & c) | ((~b) & d);
168 F = (b & c) | (b & d) | (c & d);
176 uint32_t temp = leftRotate32bits(a, 5) + F + e + g + blocks[i];
179 c = leftRotate32bits(b, 30);
194 auto* sig =
new uint8_t[20];
195 for (uint8_t i = 0; i < 4; i++) {
196 sig[i] = (h0 >> (24 - 8 * i)) & 0xFF;
197 sig[i + 4] = (h1 >> (24 - 8 * i)) & 0xFF;
198 sig[i + 8] = (h2 >> (24 - 8 * i)) & 0xFF;
199 sig[i + 12] = (h3 >> (24 - 8 * i)) & 0xFF;
200 sig[i + 16] = (h4 >> (24 - 8 * i)) & 0xFF;
223 void* sig = hashing::sha1::hash(
"");
224 std::cout <<
"Hashing empty string" << std::endl;
226 std::cout << hashing::sha1::sig2hex(sig) << std::endl << std::endl;
228 assert(hashing::sha1::sig2hex(sig).
compare(
229 "da39a3ee5e6b4b0d3255bfef95601890afd80709") == 0);
233 hashing::sha1::hash(
"The quick brown fox jumps over the lazy dog");
234 std::cout <<
"Hashing The quick brown fox jumps over the lazy dog"
237 std::cout << hashing::sha1::sig2hex(sig2) << std::endl << std::endl;
239 assert(hashing::sha1::sig2hex(sig2).
compare(
240 "2fd4e1c67a2d28fced849ee1bb76e7391b93eb12") == 0);
245 hashing::sha1::hash(
"The quick brown fox jumps over the lazy dog.");
246 std::cout <<
"Hashing "
247 "The quick brown fox jumps over the lazy dog."
250 std::cout << hashing::sha1::sig2hex(sig3) << std::endl << std::endl;
252 assert(hashing::sha1::sig2hex(sig3).
compare(
253 "408d94384216f890ff7a0c3528e8bed1e0b01621") == 0);
257 void* sig4 = hashing::sha1::hash(
258 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789");
261 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
264 std::cout << hashing::sha1::sig2hex(sig4) << std::endl << std::endl;
266 assert(hashing::sha1::sig2hex(sig4).
compare(
267 "761c457bf73b14d27e9e9265c46f4b4dda11f940") == 0);