checksums: add functions for returning results as a hex string

This commit is contained in:
Moritz Bunkus 2021-03-20 18:09:02 +01:00
parent 07eaeb0cda
commit 3669c5bc58
No known key found for this signature in database
GPG Key ID: 74AF00ADF2E32C85
2 changed files with 32 additions and 0 deletions

View File

@ -91,6 +91,36 @@ calculate_as_uint(algorithm_e algorithm,
return dynamic_cast<uint_result_c &>(*worker).get_result_as_uint();
}
std::string
calculate_as_hex_string(algorithm_e algorithm,
memory_c const &buffer,
uint64_t initial_value) {
return calculate_as_hex_string(algorithm, buffer.get_buffer(), buffer.get_size(), initial_value);
}
std::string
calculate_as_hex_string(algorithm_e algorithm,
void const *buffer,
size_t size,
uint64_t initial_value) {
auto worker = for_algorithm(algorithm, initial_value);
worker->add(buffer, size);
worker->finish();
auto result = worker->get_result();
auto result_size = result->get_size();
auto result_buffer = result->get_buffer();
std::string hex_string;
hex_string.reserve(result_size * 2);
for (auto idx = 0u; idx < result_size; ++idx)
hex_string += fmt::format("{0:02x}", result_buffer[idx]);
return hex_string;
}
// ----------------------------------------------------------------------
base_c &

View File

@ -39,5 +39,7 @@ memory_cptr calculate(algorithm_e algorithm, memory_c const &buffer, uint64_t in
memory_cptr calculate(algorithm_e algorithm, void const *buffer, size_t size, uint64_t initial_value = 0);
uint64_t calculate_as_uint(algorithm_e algorithm, memory_c const &buffer, uint64_t initial_value = 0);
uint64_t calculate_as_uint(algorithm_e algorithm, void const *buffer, size_t size, uint64_t initial_value = 0);
std::string calculate_as_hex_string(algorithm_e algorithm, memory_c const &buffer, uint64_t initial_value = 0);
std::string calculate_as_hex_string(algorithm_e algorithm, void const *buffer, size_t size, uint64_t initial_value = 0);
} // namespace mtx::checksum