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

Code before:
std::string Hash::to_string() {
    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();
}
}  // namespace hashing

/**
 * @brief Self-test implementations
 * @returns void
 */
static void test_compute_padded_size() {
    assert(hashing::sha256::compute_padded_size(55) == 64);
    assert(hashing::sha256::compute_padded_size(56) == 128);
    assert(hashing::sha256::compute_padded_size(130) == 192);
}

static void test_extract_byte() {
    assert(hashing::sha256::extract_byte<uint32_t>(512, 0) == 0);
    assert(hashing::sha256::extract_byte<uint32_t>(512, 1) == 2);
    bool exception = false;
    try {
        hashing::sha256::extract_byte<uint32_t>(512, 5);
    } catch (const std::out_of_range) {
        exception = true;
    }
    assert(exception);
}

static void test_get_char() {
    assert(hashing::sha256::get_char("test", 3) == 't');
    assert(hashing::sha256::get_char("test", 4) == '\x80');
    assert(hashing::sha256::get_char("test", 5) == '\x00');
    assert(hashing::sha256::get_char("test", 63) == 32);
    bool exception = false;
    try {
        hashing::sha256::get_char("test", 64);
    } catch (const std::out_of_range) {
        exception = true;
    }
    assert(exception);
}

static void test_right_rotate() {
    assert(hashing::sha256::right_rotate(128, 3) == 16);
    assert(hashing::sha256::right_rotate(1, 30) == 4);
    assert(hashing::sha256::right_rotate(6, 30) == 24);
}


Review comment (suggestion):
```suggestion
    } catch (const std::out_of_range &) {
```

Code after:
 * @param input Input string
 * @return std::string The final hash value
 */
std::string sha256(const std::string &input) {
    Hash h;
    // Process message in successive 512-bit (64-byte) chunks
    for (size_t byte_num = 0; byte_num < compute_padded_size(input.length());
         byte_num += 64) {
        h.update(create_message_schedule_array(input, byte_num));
    }
    return h.to_string();
}
}  // namespace sha256
}  // namespace hashing

/**
 * @brief Self-test implementations
 * @returns void
 */
static void test_compute_padded_size() {
    assert(hashing::sha256::compute_padded_size(55) == 64);
    assert(hashing::sha256::compute_padded_size(56) == 128);
    assert(hashing::sha256::compute_padded_size(130) == 192);
}

static void test_extract_byte() {
    assert(hashing::sha256::extract_byte<uint32_t>(512, 0) == 0);
    assert(hashing::sha256::extract_byte<uint32_t>(512, 1) == 2);
    bool exception = false;
    try {
        hashing::sha256::extract_byte<uint32_t>(512, 5);
    } catch (const std::out_of_range &) {
        exception = true;
    }
    assert(exception);
}

static void test_get_char() {
    assert(hashing::sha256::get_char("test", 3) == 't');
    assert(hashing::sha256::get_char("test", 4) == '\x80');
    assert(hashing::sha256::get_char("test", 5) == '\x00');
    assert(hashing::sha256::get_char("test", 63) == 32);
    bool exception = false;
    try {
        hashing::sha256::get_char("test", 64);
    } catch (const std::out_of_range &) {
        exception = true;
    }
    assert(exception);
}