20 #ifndef CS_CRYPTO_UTIL_HEX_H
21 #define CS_CRYPTO_UTIL_HEX_H
23 #include <util/conversions/byte.h>
24 #include <util/tools/crypto_traits.h>
30 namespace cs_crypto::util {
37 constexpr hex_byte to_hex_char(
const unsigned char value)
noexcept
39 constexpr char const chars[] =
"0123456789abcdef";
40 return {chars[value >> 4], chars[value & 0x0f]};
43 constexpr hex_byte to_hex_char(
const std::byte value)
noexcept
45 return to_hex_char(std::to_integer<
unsigned char>(value));
48 template <
typename Iter,
typename Sentinel>
49 std::string hex(Iter iter,
const Sentinel last)
53 const auto distance = std::distance(iter, last);
54 result.reserve(distance * 2);
56 while (iter != last) {
57 auto [high, low] = to_hex_char(*iter);
58 result.push_back(high);
59 result.push_back(low);
60 iter = std::next(iter);
66 template <
typename Range,
typename = std::enable_if_t<cs_crypto::traits::is_iterable_v<Range>>>
67 std::string hex(Range &&input)
69 return hex(std::begin(input), std::end(input));