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

Code before:
        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
    uint32_t a = hash[0];
    uint32_t b = hash[1];
    uint32_t c = hash[2];
    uint32_t d = hash[3];
    uint32_t e = hash[4];
    uint32_t f = hash[5];
    uint32_t g = hash[6];
    uint32_t h = hash[7];

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

        h = g;
        g = f;
        f = e;
        e = d + temp1;
        d = c;
        c = b;
        b = a;
        a = temp1 + temp2;
    }

    // Update hash values
    hash[0] += a;
    hash[1] += b;
    hash[2] += c;
    hash[3] += d;
    hash[4] += e;
    hash[5] += f;
    hash[6] += g;
    hash[7] += h;
}

/**
 * @brief Computes the final hash array
 * @param input Input string

Review comment (suggestion):
```suggestion
        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 + k[j] + blocks[j];
        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;
```

Code after:
    if (input_size % 64 < 56) {
        return input_size + 64 - (input_size % 64);
    }
    return input_size + 128 - (input_size % 64);
}

/**
 * @brief Returns the byte at position byte_num in in_value
 * @param in_value Input value
 * @param byte_num Position of byte to be returned
 * @return uint8_t Byte at position byte_num
 */
template <typename T>
uint8_t extract_byte(const T in_value, const std::size_t byte_num) {
    if (sizeof(in_value) <= byte_num) {
        throw std::out_of_range("Byte at index byte_num does not exist");
    }
    return (in_value >> (byte_num * 8)) & 0xFF;
}

/**
 * @brief Returns the character at pos after the input is padded
 * @param input Input string
 * @param pos Position of character to be returned
 * @return char Character at the index pos in the padded string
 */
char get_char(const std::string &input, std::size_t pos) {
    const auto input_size = input.length();
    if (pos < input_size) {
        return input[pos];
    }
    if (pos == input_size) {
        return '\x80';
    }
    const auto padded_input_size = compute_padded_size(input_size);
    if (pos < padded_input_size - 8) {
        return '\x00';
    }
    if (padded_input_size <= pos) {
        throw std::out_of_range("pos is out of range");
    }
    return static_cast<char>(
        extract_byte<size_t>(input_size * 8, padded_input_size - pos - 1));
}

/**
 * @brief Creates the message schedule array
 * @param input Input string
 * @param byte_num Position of the first byte of the chunk
 * @return std::array<uint32_t, 64> Message schedule array
 */