Repository: TheAlgorithms/C-Plus-Plus
File: hashing/sha256.cpp (C++)

Code before:
        0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1,
        0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
        0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786,
        0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
        0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147,
        0x06CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
        0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B,
        0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
        0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A,
        0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
        0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2};

    // Pre-processing
    std::string padded_input = input;
    uint64_t input_size = input.length() * 8;  // Message length in bits

    // Append a single '1' bit
    padded_input += '\x80';

    // Pad with zeros
    while ((padded_input.length() * 8) % 512 != 448) padded_input += '\x00';

    // Append length of the original input string as a 64-bit big-endian integer
    for (uint32_t i = 64; i != 0; i -= 8) {
        padded_input += static_cast<char>((input_size >> (i - 8)) & 0xFF);
    }

    // Process message in successive 512-bit (64-byte) chunks
    for (size_t i = 0; i < padded_input.length(); i += 64) {
        uint32_t blocks[64];

        // Copy chunk into first 16 words of the message schedule array
        for (size_t j = 0; j < 16; ++j) {
            blocks[j] =
                (static_cast<uint8_t>(padded_input[i + j * 4]) << 24) |
                (static_cast<uint8_t>(padded_input[i + j * 4 + 1]) << 16) |
                (static_cast<uint8_t>(padded_input[i + j * 4 + 2]) << 8) |
                static_cast<uint8_t>(padded_input[i + j * 4 + 3]);
        }

        for (size_t j = 16; j < 64; ++j) {
            uint32_t s0 = rightRotate(blocks[j - 15], 7) ^
                          rightRotate(blocks[j - 15], 18) ^
                          (blocks[j - 15] >> 3);
            uint32_t s1 = rightRotate(blocks[j - 2], 17) ^
                          rightRotate(blocks[j - 2], 19) ^
                          (blocks[j - 2] >> 10);
            blocks[j] = blocks[j - 16] + s0 + blocks[j - 7] + s1;
        }

        // Initialize working variables

Review comment (refactor):
What do you think about about extracting it into a new function, let's say `prepare_input`, and then have a single call like
```cpp
const std::string padded_input = prepare_input(input);
```

Another thing to consider is to avoid copying the `input` string at all by having a function of the form
```cpp
char get_char(const std::string input, std::size_t pos);
```
either returning `input[pos]`, padding character or the _byte_ of the `input` length.

Code after:
 * @param n Integer to rotate
 * @param rotate Number of bits to rotate
 * @return uint32_t The rotated integer
 */
uint32_t right_rotate(uint32_t n, size_t rotate) {
    return (n >> rotate) | (n << (32 - rotate));
}

/**
 * @brief Updates the hash array
 * @param blocks Message schedule array
 * @return void
 */
void Hash::update(const std::array<uint32_t, 64> &blocks) {
    // Initialize array of round constants with first 32 bits of the fractional
    // parts of the cube roots of the first 64 primes 2..311
    const std::array<uint32_t, 64> round_constants = {
        0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1,
        0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3,
        0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786,
        0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA,
        0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147,
        0x06CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13,
        0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B,
        0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070,
        0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A,
        0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208,
        0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2};

    // Initialize working variables
    auto a = hash[0];
    auto b = hash[1];
    auto c = hash[2];
    auto d = hash[3];
    auto e = hash[4];
    auto f = hash[5];
    auto g = hash[6];
    auto h = hash[7];

    // Compression function main loop
    for (size_t block_num = 0; block_num < 64; ++block_num) {
        const auto s1 =
            right_rotate(e, 6) ^ right_rotate(e, 11) ^ right_rotate(e, 25);
        const auto ch = (e & f) ^ (~e & g);
        const auto temp1 =
            h + s1 + ch + round_constants[block_num] + blocks[block_num];
        const auto s0 =
            right_rotate(a, 2) ^ right_rotate(a, 13) ^ right_rotate(a, 22);
        const auto maj = (a & b) ^ (a & c) ^ (b & c);
        const auto temp2 = s0 + maj;