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

Code before:
            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;
    }

    // Convert the hash to a hexadecimal string
    std::string result;
    for (size_t i = 0; i < 8; ++i) {
        for (int j = 0; j < 4; ++j) {
            uint8_t byte = (hash[i] >> (24 - j * 8)) & 0xFF;
            char buf[2];
            sprintf(buf, "%02x", byte);
            result += buf;
        }
    }

    return result;
}
}  // namespace sha256
}  // namespace hashing

/**
 * @brief Self-test implementations
 * @returns void
 */
static void test() {
    // 1st Test
    std::string input = "";
    std::string expected =
        "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
    std::string output = hashing::sha256::sha256(input);
    std::cout << "Input: " << input << std::endl;
    std::cout << "Expected: " << expected << std::endl;
    std::cout << "Output: " << output << std::endl;
    assert(output == expected);
    std::cout << "1st TEST PASSED\n\n";

Review comment (suggestion):
```suggestion
            char buf[3];
```
`buf` is one `char` [too short](https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1DIApACYAQuYukl9ZATwDKjdAGFUtAK4sGIMwDMpK4AMngMmAByPgBGmMQSAKxcpAAOqAqETgwe3r7%2BQemZjgJhEdEscQlcybaY9iUMQgRMxAS5Pn6BdQ3Zza0EZVGx8UkpCi1tHfndEwNDFVVjAJS2qF7EyOwcAPQAVHsmGgCCANR7pwACNPRHZxeXTF4ECCSnJokWALLoAHSnxwYeAUXlopwAEkwAI5eTAfAAiEAQBAIqQUIB2O2AhAQXhivzQLB2LHQhmBoIQyzu5yuMWIeEwVFOQjwLFS9FObms1lOrPZmDYgiYjVOqCZ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sgBxsRuJswCIAgCZ2ZsQDZv0DEBHhsCcAqdqcH4actjjKYK8CphECod6D3vCCiDiAvspfvtqBNvfukBWgNipDHv6DyfOdNtRtaO3VEwRfqeaexfGEQAeBsg5ubbVKGcutecetetYK/DSJYK3qxqiYHIZo/ucBOcueRucDueedutpzFccBmAKeueTftczerB/mZDOCSBAA%3D%3D%3D).

Code after:
/**
 * @brief Convert the hash to a hexadecimal string
 * @return std::string Final hash value
 */
std::string Hash::to_string() const {
    std::stringstream ss;
    for (size_t i = 0; i < 8; ++i) {
        ss << std::hex << std::setfill('0') << std::setw(8) << hash[i];
    }
    return ss.str();
}

/**
 * @brief Computes size of the padded input
 * @param input Input string
 * @return size_t Size of the padded input
 */
std::size_t compute_padded_size(const std::size_t input_size) {
    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';