| 6 |
| // -- Bruce Guenter <bruce@untroubled.org> |
| 7 | // Translation to simpler C++ Code |
| 8 | // -- Volker Grabsch <vog@notjusthosting.com> |
| 9 | // Safety fixes |
| 10 | // -- Eugene Hopkinson <slowriot at voxelstorm dot com> |
| 11 | // Adapt for project |
| 12 | // Dmitriy Khaustov <khaustov.dm@gmail.com> |
| 13 | // |
| 14 | // File created on: 2017.02.25 |
| 15 |
| 16 | // SHA1.cpp |
| 17 |
| 18 | #include "SHA1.h" |
| 19 |
| 20 | #include <sstream> |
| 21 | #include <iomanip> |
| 22 | #include <fstream> |
| 23 |
| 24 | namespace toolkit { |
| 25 |
| 26 | static const size_t BLOCK_INTS = 16; /* number of 32bit integers per SHA1 block */ |
| 27 | static const size_t BLOCK_BYTES = BLOCK_INTS * 4; |
| 28 |
| 29 |
| 30 | static void reset(uint32_t digest[], std::string &buffer, uint64_t &transforms) |
| 31 | { |
| 32 | /* SHA1 initialization constants */ |
| 33 | digest[0] = 0x67452301; |
| 34 | digest[1] = 0xefcdab89; |
| 35 | digest[2] = 0x98badcfe; |
| 36 | digest[3] = 0x10325476; |
| 37 | digest[4] = 0xc3d2e1f0; |
| 38 |
| 39 | /* Reset counters */ |
| 40 | buffer = ""; |
| 41 | transforms = 0; |
| 42 | } |
| 43 |
| 44 |
| 45 | static uint32_t rol(const uint32_t value, const size_t bits) |
| 46 | { |
| 47 | return (value << bits) | (value >> (32 - bits)); |
| 48 | } |
| 49 |
| 50 |
| 51 | static uint32_t blk(const uint32_t block[BLOCK_INTS], const size_t i) |
| 52 | { |
| 53 | return rol(block[(i+13)&15] ^ block[(i+8)&15] ^ block[(i+2)&15] ^ block[i], 1); |
| 54 | } |
| 55 |
| 56 |
| 57 | /* |
| 58 | * (R0+R1), R2, R3, R4 are the different operations used in SHA1 |
| 59 | */ |
| 60 |
| 61 | static void R0(const uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i) |
| 62 | { |
| 63 | z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rol(v, 5); |
| 64 | w = rol(w, 30); |
| 65 | } |
| 66 |
| 67 |
| 68 | static void R1(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i) |
| 69 | { |
| 70 | block[i] = blk(block, i); |
| 71 | z += ((w&(x^y))^y) + block[i] + 0x5a827999 + rol(v, 5); |
| 72 | w = rol(w, 30); |
| 73 | } |
| 74 |
| 75 |
| 76 | static void R2(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i) |
| 77 | { |
| 78 | block[i] = blk(block, i); |
| 79 | z += (w^x^y) + block[i] + 0x6ed9eba1 + rol(v, 5); |
| 80 | w = rol(w, 30); |
| 81 | } |
| 82 |
| 83 |
| 84 | static void R3(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i) |
| 85 | { |
| 86 | block[i] = blk(block, i); |
| 87 | z += (((w|x)&y)|(w&x)) + block[i] + 0x8f1bbcdc + rol(v, 5); |
| 88 | w = rol(w, 30); |
| 89 | } |
| 90 |
| 91 |
| 92 | static void R4(uint32_t block[BLOCK_INTS], const uint32_t v, uint32_t &w, const uint32_t x, const uint32_t y, uint32_t &z, const size_t i) |
| 93 | { |
| 94 | block[i] = blk(block, i); |
| 95 | z += (w^x^y) + block[i] + 0xca62c1d6 + rol(v, 5); |
| 96 | w = rol(w, 30); |
| 97 | } |
| 98 |
| 99 |
| 100 | /* |
| 101 | * Hash a single 512-bit block. This is the core of the algorithm. |
| 102 | */ |
| 103 |
| 104 | static void transform(uint32_t digest[], uint32_t block[BLOCK_INTS], uint64_t &transforms) |
| 105 | { |
| 106 | /* Copy digest[] to working vars */ |
| 107 | uint32_t a = digest[0]; |
| 108 | uint32_t b = digest[1]; |
| 109 | uint32_t c = digest[2]; |
| 110 | uint32_t d = digest[3]; |
| 111 | uint32_t e = digest[4]; |
| 112 |
| 113 | /* 4 rounds of 20 operations each. Loop unrolled. */ |
| 114 | R0(block, a, b, c, d, e, 0); |
| 115 | R0(block, e, a, b, c, d, 1); |
| 116 | R0(block, d, e, a, b, c, 2); |
| 117 | R0(block, c, d, e, a, b, 3); |
| 118 | R0(block, b, c, d, e, a, 4); |
| 119 | R0(block, a, b, c, d, e, 5); |
| 120 | R0(block, e, a, b, c, d, 6); |
| 121 | R0(block, d, e, a, b, c, 7); |
| 122 | R0(block, c, d, e, a, b, 8); |
| 123 | R0(block, b, c, d, e, a, 9); |
| 124 | R0(block, a, b, c, d, e, 10); |
| 125 | R0(block, e, a, b, c, d, 11); |
| 126 | R0(block, d, e, a, b, c, 12); |
| 127 | R0(block, c, d, e, a, b, 13); |
| 128 | R0(block, b, c, d, e, a, 14); |
| 129 | R0(block, a, b, c, d, e, 15); |
| 130 | R1(block, e, a, b, c, d, 0); |
| 131 | R1(block, d, e, a, b, c, 1); |
| 132 | R1(block, c, d, e, a, b, 2); |
| 133 | R1(block, b, c, d, e, a, 3); |
| 134 | R2(block, a, b, c, d, e, 4); |
| 135 | R2(block, e, a, b, c, d, 5); |
| 136 | R2(block, d, e, a, b, c, 6); |
| 137 | R2(block, c, d, e, a, b, 7); |
| 138 | R2(block, b, c, d, e, a, 8); |
| 139 | R2(block, a, b, c, d, e, 9); |
| 140 | R2(block, e, a, b, c, d, 10); |
| 141 | R2(block, d, e, a, b, c, 11); |
| 142 | R2(block, c, d, e, a, b, 12); |
| 143 | R2(block, b, c, d, e, a, 13); |
| 144 | R2(block, a, b, c, d, e, 14); |
| 145 | R2(block, e, a, b, c, d, 15); |
| 146 | R2(block, d, e, a, b, c, 0); |
| 147 | R2(block, c, d, e, a, b, 1); |
| 148 | R2(block, b, c, d, e, a, 2); |
| 149 | R2(block, a, b, c, d, e, 3); |
| 150 | R2(block, e, a, b, c, d, 4); |
| 151 | R2(block, d, e, a, b, c, 5); |
| 152 | R2(block, c, d, e, a, b, 6); |
| 153 | R2(block, b, c, d, e, a, 7); |
| 154 | R3(block, a, b, c, d, e, 8); |
| 155 | R3(block, e, a, b, c, d, 9); |
| 156 | R3(block, d, e, a, b, c, 10); |
| 157 | R3(block, c, d, e, a, b, 11); |
| 158 | R3(block, b, c, d, e, a, 12); |
| 159 | R3(block, a, b, c, d, e, 13); |
| 160 | R3(block, e, a, b, c, d, 14); |
| 161 | R3(block, d, e, a, b, c, 15); |
| 162 | R3(block, c, d, e, a, b, 0); |
| 163 | R3(block, b, c, d, e, a, 1); |
| 164 | R3(block, a, b, c, d, e, 2); |
| 165 | R3(block, e, a, b, c, d, 3); |
| 166 | R3(block, d, e, a, b, c, 4); |
| 167 | R3(block, c, d, e, a, b, 5); |
| 168 | R3(block, b, c, d, e, a, 6); |
| 169 | R3(block, a, b, c, d, e, 7); |
| 170 | R3(block, e, a, b, c, d, 8); |
| 171 | R3(block, d, e, a, b, c, 9); |
| 172 | R3(block, c, d, e, a, b, 10); |
| 173 | R3(block, b, c, d, e, a, 11); |
| 174 | R4(block, a, b, c, d, e, 12); |
| 175 | R4(block, e, a, b, c, d, 13); |
| 176 | R4(block, d, e, a, b, c, 14); |
| 177 | R4(block, c, d, e, a, b, 15); |
| 178 | R4(block, b, c, d, e, a, 0); |
| 179 | R4(block, a, b, c, d, e, 1); |
| 180 | R4(block, e, a, b, c, d, 2); |
| 181 | R4(block, d, e, a, b, c, 3); |
| 182 | R4(block, c, d, e, a, b, 4); |
| 183 | R4(block, b, c, d, e, a, 5); |
| 184 | R4(block, a, b, c, d, e, 6); |
| 185 | R4(block, e, a, b, c, d, 7); |
| 186 | R4(block, d, e, a, b, c, 8); |
| 187 | R4(block, c, d, e, a, b, 9); |
| 188 | R4(block, b, c, d, e, a, 10); |
| 189 | R4(block, a, b, c, d, e, 11); |
| 190 | R4(block, e, a, b, c, d, 12); |
| 191 | R4(block, d, e, a, b, c, 13); |
| 192 | R4(block, c, d, e, a, b, 14); |
| 193 | R4(block, b, c, d, e, a, 15); |
| 194 |
| 195 | /* Add the working vars back into digest[] */ |
| 196 | digest[0] += a; |
| 197 | digest[1] += b; |
| 198 | digest[2] += c; |
| 199 | digest[3] += d; |
| 200 | digest[4] += e; |
| 201 |
| 202 | /* Count the number of transformations */ |
| 203 | transforms++; |
| 204 | } |
| 205 |
| 206 |
| 207 | static void buffer_to_block(const std::string &buffer, uint32_t block[BLOCK_INTS]) |
| 208 | { |
| 209 | /* Convert the std::string (byte buffer) to a uint32_t array (MSB) */ |
| 210 | for (size_t i = 0; i < BLOCK_INTS; i++) |
| 211 | { |
| 212 | block[i] = |
| 213 | (buffer[4*i+3] & 0xFF) |
| 214 | | (buffer[4*i+2] & 0xFF) << 8 |
| 215 | | (buffer[4*i+1] & 0xff) << 16 |
| 216 | | (buffer[4*i+0] & 0xff) << 24; |
| 217 | } |
| 218 | } |
| 219 |
| 220 |
| 221 | SHA1::SHA1() |
| 222 | { |
| 223 | reset(digest, buffer, transforms); |
| 224 | } |
| 225 |
| 226 |
| 227 | void SHA1::update(const std::string &s) |
| 228 | { |
| 229 | std::istringstream is(s); |
| 230 | update(is); |
| 231 | } |
| 232 |
| 233 |
| 234 | void SHA1::update(std::istream &is) |
| 235 | { |
| 236 | while (true) |
| 237 | { |
| 238 | char sbuf[BLOCK_BYTES]; |
| 239 | is.read(sbuf, BLOCK_BYTES - buffer.size()); |
| 240 | buffer.append(sbuf, is.gcount()); |
| 241 | if (buffer.size() != BLOCK_BYTES) |
| 242 | { |
| 243 | return; |
| 244 | } |
| 245 | uint32_t block[BLOCK_INTS]; |
| 246 | buffer_to_block(buffer, block); |
| 247 | transform(digest, block, transforms); |
| 248 | buffer.clear(); |
| 249 | } |
| 250 | } |
| 251 |
| 252 |
| 253 | /* |
| 254 | * Add padding and return the message digest. |
| 255 | */ |
| 256 |
| 257 | std::string SHA1::final() |
| 258 | { |
| 259 | auto str = final_bin(); |
| 260 |
| 261 | std::ostringstream result; |
| 262 |
| 263 | for (size_t i = 0; i < str.size(); i++) |
| 264 | { |
| 265 | char b[3]; |
| 266 | sprintf(b, "%02x", static_cast<unsigned char>(str[i])); |
| 267 | result << b; |
| 268 | } |
| 269 |
| 270 | return result.str(); |
| 271 | } |
| 272 |
| 273 | std::string SHA1::final_bin() |
| 274 | { |
| 275 | /* Total number of hashed bits */ |
| 276 | uint64_t total_bits = (transforms*BLOCK_BYTES + buffer.size()) * 8; |
| 277 |
| 278 | /* Padding */ |
| 279 | buffer += 0x80; |
| 280 | size_t orig_size = buffer.size(); |
| 281 | while (buffer.size() < BLOCK_BYTES) |
| 282 | { |
| 283 | buffer += (char)0x00; |
| 284 | } |
| 285 |
| 286 | uint32_t block[BLOCK_INTS]; |
| 287 | buffer_to_block(buffer, block); |
| 288 |
| 289 | if (orig_size > BLOCK_BYTES - 8) |
| 290 | { |
| 291 | transform(digest, block, transforms); |
| 292 | for (size_t i = 0; i < BLOCK_INTS - 2; i++) |
| 293 | { |
| 294 | block[i] = 0; |
| 295 | } |
| 296 | } |
| 297 |
| 298 | /* Append total_bits, split this uint64_t into two uint32_t */ |
| 299 | block[BLOCK_INTS - 1] = total_bits; |
| 300 | block[BLOCK_INTS - 2] = (total_bits >> 32); |
| 301 | transform(digest, block, transforms); |
| 302 |
| 303 | /* Hex std::string */ |
| 304 | std::string result; |
| 305 | for (size_t i = 0; i < sizeof(digest) / sizeof(digest[0]); i++) |
| 306 | { |
| 307 | for (size_t b = 0; b < sizeof(digest[0])/sizeof(uint8_t); b++) |
| 308 | { |
| 309 | result.push_back((digest[i] >> (8 * (sizeof(digest[0]) / sizeof(uint8_t) - 1 - b))) & 0xFF); |
| 310 | } |
| 311 | } |
| 312 |
| 313 | /* Reset for next run */ |
| 314 | reset(digest, buffer, transforms); |
| 315 |
| 316 | return result; |
| 317 | } |
| 318 |
| 319 | std::string SHA1::from_file(const std::string &filename) |
| 320 | { |
| 321 | std::ifstream stream(filename.c_str(), std::ios::binary); |
| 322 | SHA1 checksum; |
| 323 | checksum.update(stream); |
| 324 | return checksum.final(); |
| 325 | } |
| 326 |
| 327 | std::string SHA1::encode(const std::string &s) |
| 328 | { |
| 329 | SHA1 sha1; |
| 330 | sha1.update(s); |
| 331 | return sha1.final(); |
| 332 | } |
| 333 |
| 334 | std::string SHA1::encode_bin(const std::string &s) |
| 335 | { |
| 336 | SHA1 sha1; |
| 337 | sha1.update(s); |
| 338 | return sha1.final_bin(); |
| 339 | } |
| 340 |
| 341 | } //namespace toolkit |