2020-05-30 06:06:16 +02:00
|
|
|
#pragma once
|
|
|
|
#include <string>
|
|
|
|
#include <string_view>
|
2023-04-13 15:50:13 +02:00
|
|
|
#include <type_traits>
|
|
|
|
|
2020-10-22 19:55:33 +02:00
|
|
|
#include "fs.h"
|
2020-06-22 21:27:42 +02:00
|
|
|
|
2023-04-13 15:50:13 +02:00
|
|
|
namespace crypto {
|
|
|
|
struct hash;
|
|
|
|
}
|
2020-05-30 06:06:16 +02:00
|
|
|
|
|
|
|
namespace tools {
|
|
|
|
|
2023-04-13 15:50:13 +02:00
|
|
|
// Calculates sha256 checksum of the given data
|
|
|
|
bool sha256sum_str(std::string_view str, crypto::hash& hash);
|
2020-05-30 06:06:16 +02:00
|
|
|
|
2023-04-13 15:50:13 +02:00
|
|
|
// Calculates sha256 checksum of the given data, for non-char string_view (e.g.
|
|
|
|
// basic_string_view<unsigned char> or basic_string_view<uint8_t>).
|
|
|
|
template <
|
|
|
|
typename Char,
|
|
|
|
std::enable_if_t<sizeof(Char) == 1 && !std::is_same_v<Char, char>, int> = 0>
|
|
|
|
bool sha256sum_str(std::basic_string_view<Char> str, crypto::hash& hash) {
|
|
|
|
return sha256sum_str(
|
|
|
|
std::string_view{reinterpret_cast<const char*>(str.data()), str.size()}, hash);
|
|
|
|
}
|
2020-05-30 06:06:16 +02:00
|
|
|
|
2023-04-13 15:50:13 +02:00
|
|
|
// Calculates sha256 checksum of the given byte data given any arbitrary size-1 value pointer and
|
|
|
|
// byte length.
|
|
|
|
template <typename Char, std::enable_if_t<sizeof(Char) == 1, int> = 0>
|
|
|
|
bool sha256sum_str(const Char* data, size_t len, crypto::hash& hash) {
|
|
|
|
return sha256sum_str(std::string_view{reinterpret_cast<const char*>(data), len}, hash);
|
2020-05-30 06:06:16 +02:00
|
|
|
}
|
2023-04-13 15:50:13 +02:00
|
|
|
|
|
|
|
// Opens the given file and calculates a sha256sum of its contents
|
|
|
|
bool sha256sum_file(const fs::path& filename, crypto::hash& hash);
|
|
|
|
|
|
|
|
} // namespace tools
|