File size: 10,422 Bytes
2492322 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | // Copyright (c) 2023, QuantStack and Mamba Contributors
//
// Distributed under the terms of the BSD 3-Clause License.
//
// The full license is in the file LICENSE, distributed with this software.
#ifndef MAMBA_UTIL_CRYPTOGRAPHY_HPP
#define MAMBA_UTIL_CRYPTOGRAPHY_HPP
#include <algorithm>
#include <array>
#include <cstddef>
#include <fstream>
#include <memory>
#include <string>
#include <vector>
#include "mamba/util/encoding.hpp"
using EVP_MD_CTX = struct evp_md_ctx_st; // OpenSSL impl
namespace mamba::util
{
/**
* Provide high-level hashing functions over a digest hashing algorithm.
*/
template <typename Digester>
class DigestHasher
{
public:
using digester_type = Digester;
inline static constexpr std::size_t bytes_size = digester_type::bytes_size;
inline static constexpr std::size_t hex_size = 2 * bytes_size;
inline static constexpr std::size_t digest_size = digester_type::digest_size;
using bytes_array = std::array<std::byte, bytes_size>;
using hex_array = std::array<char, hex_size>;
// TODO(C++20): use std::span<std::byte>
struct blob_type
{
const std::byte* data;
std::size_t size;
};
/**
* Hash a blob of data and write the hashed bytes to the provided output.
*/
void blob_bytes_to(blob_type blob, std::byte* out);
/**
* Hash a blob of data and return the hashed bytes as an array.
*/
[[nodiscard]] auto blob_bytes(blob_type blob) -> bytes_array;
/**
* Hash a blob of data and write the hashed bytes with hexadecimal encoding to the output.
*/
void blob_hex_to(blob_type blob, char* out);
/**
* Hash a blob of data and return the hashed bytes with hexadecimal encoding as an array.
*/
[[nodiscard]] auto blob_hex(blob_type blob) -> hex_array;
/**
* Hash a blob of data and return the hashed bytes with hexadecimal encoding as a string.
*/
[[nodiscard]] auto blob_hex_str(blob_type blob) -> std::string;
/**
* Hash a string and write the hashed bytes to the provided output.
*/
void str_bytes_to(std::string_view data, std::byte* out);
/**
* Hash a string and return the hashed bytes as an array.
*/
[[nodiscard]] auto str_bytes(std::string_view data) -> bytes_array;
/**
* Hash a string and write the hashed bytes with hexadecimal encoding to the output.
*/
void str_hex_to(std::string_view data, char* out);
/**
* Hash a string and return the hashed bytes with hexadecimal encoding as an array.
*/
[[nodiscard]] auto str_hex(std::string_view data) -> hex_array;
/**
* Hash a string and return the hashed bytes with hexadecimal encoding as a string.
*/
[[nodiscard]] auto str_hex_str(std::string_view data) -> std::string;
/**
* Incrementally hash a file and write the hashed bytes to the provided output.
*/
void file_bytes_to(std::ifstream& file, std::byte* out);
/**
* Incrementally hash a file and return the hashed bytes as an array.
*/
[[nodiscard]] auto file_bytes(std::ifstream& file) -> bytes_array;
/**
* Incrementally hash a file and write the hashed bytes with hexadecimal encoding to the
* output.
*/
void file_hex_to(std::ifstream& infile, char* out);
/**
* Incrementally hash a file and return the hashed bytes with hexadecimal encoding as an
* array.
*/
[[nodiscard]] auto file_hex(std::ifstream& file) -> hex_array;
/**
* Incrementally hash a file and return the hashed bytes with hexadecimal encoding as a
* string.
*/
[[nodiscard]] auto file_hex_str(std::ifstream& file) -> std::string;
private:
std::vector<std::byte> m_digest_buffer = {};
digester_type m_digester = {};
};
namespace detail
{
class EVPDigester
{
public:
enum struct Algorithm
{
sha256,
md5
};
EVPDigester(Algorithm algo);
void digest_start();
void digest_update(const std::byte* buffer, std::size_t count);
void digest_finalize_to(std::byte* hash);
private:
struct EVPContextDeleter
{
void operator()(::EVP_MD_CTX* ptr) const;
};
std::unique_ptr<::EVP_MD_CTX, EVPContextDeleter> m_ctx;
Algorithm m_algorithm;
};
}
class Sha256Digester : private detail::EVPDigester
{
public:
inline static constexpr std::size_t bytes_size = 32;
inline static constexpr std::size_t digest_size = 32768;
using detail::EVPDigester::digest_start;
using detail::EVPDigester::digest_update;
using detail::EVPDigester::digest_finalize_to;
Sha256Digester()
: EVPDigester(detail::EVPDigester::Algorithm::sha256)
{
}
};
using Sha256Hasher = DigestHasher<Sha256Digester>;
class Md5Digester : private detail::EVPDigester
{
public:
inline static constexpr std::size_t bytes_size = 16;
inline static constexpr std::size_t digest_size = 32768;
using detail::EVPDigester::digest_start;
using detail::EVPDigester::digest_update;
using detail::EVPDigester::digest_finalize_to;
Md5Digester()
: EVPDigester(detail::EVPDigester::Algorithm::md5)
{
}
};
using Md5Hasher = DigestHasher<Md5Digester>;
/************************************
* Implementation of DigestHasher *
************************************/
template <typename D>
void DigestHasher<D>::blob_bytes_to(blob_type blob, std::byte* out)
{
m_digester.digest_start();
auto [iter, remaining] = blob;
while (remaining > 0)
{
const auto taken = std::min(remaining, digest_size);
m_digester.digest_update(const_cast<std::byte*>(iter), taken);
remaining -= taken;
iter += taken;
}
return m_digester.digest_finalize_to(out);
}
template <typename D>
auto DigestHasher<D>::blob_bytes(blob_type blob) -> bytes_array
{
auto out = bytes_array{};
blob_bytes_to(blob, out.data());
return out;
}
template <typename D>
void DigestHasher<D>::blob_hex_to(blob_type blob, char* out)
{
// Reusing the output array to write the temporary bytes
static_assert(hex_size >= 2 * bytes_size);
static_assert(sizeof(std::byte) == sizeof(char));
auto bytes_first = reinterpret_cast<std::byte*>(out) + bytes_size;
auto bytes_last = bytes_first + bytes_size;
blob_bytes_to(blob, bytes_first);
bytes_to_hex_to(bytes_first, bytes_last, out);
}
template <typename D>
auto DigestHasher<D>::blob_hex(blob_type blob) -> hex_array
{
auto out = hex_array{};
blob_hex_to(blob, out.data());
return out;
}
template <typename D>
auto DigestHasher<D>::blob_hex_str(blob_type blob) -> std::string
{
auto out = std::string(hex_size, 'x'); // An invalid character
blob_hex_to(blob, out.data());
return out;
}
template <typename D>
void DigestHasher<D>::str_bytes_to(std::string_view data, std::byte* out)
{
blob_bytes_to({ reinterpret_cast<const std::byte*>(data.data()), data.size() }, out);
}
template <typename D>
auto DigestHasher<D>::str_bytes(std::string_view data) -> bytes_array
{
return blob_bytes({ reinterpret_cast<const std::byte*>(data.data()), data.size() });
}
template <typename D>
void DigestHasher<D>::str_hex_to(std::string_view data, char* out)
{
blob_hex_to({ reinterpret_cast<const std::byte*>(data.data()), data.size() }, out);
}
template <typename D>
auto DigestHasher<D>::str_hex(std::string_view data) -> hex_array
{
return blob_hex({ reinterpret_cast<const std::byte*>(data.data()), data.size() });
}
template <typename D>
auto DigestHasher<D>::str_hex_str(std::string_view data) -> std::string
{
return blob_hex_str({ reinterpret_cast<const std::byte*>(data.data()), data.size() });
}
template <typename D>
void DigestHasher<D>::file_bytes_to(std::ifstream& infile, std::byte* out)
{
m_digest_buffer.assign(digest_size, std::byte(0));
m_digester.digest_start();
while (infile)
{
infile.read(reinterpret_cast<char*>(m_digest_buffer.data()), digest_size);
const auto count = static_cast<std::size_t>(infile.gcount());
if (!count)
{
break;
}
m_digester.digest_update(m_digest_buffer.data(), count);
}
return m_digester.digest_finalize_to(out);
}
template <typename D>
auto DigestHasher<D>::file_bytes(std::ifstream& infile) -> bytes_array
{
auto out = bytes_array{};
file_bytes_to(infile, out.data());
return out;
}
template <typename D>
void DigestHasher<D>::file_hex_to(std::ifstream& infile, char* out)
{
// Reusing the output array to write the temporary bytes
static_assert(hex_size >= 2 * bytes_size);
static_assert(sizeof(std::byte) == sizeof(char));
auto bytes_first = reinterpret_cast<std::byte*>(out) + bytes_size;
auto bytes_last = bytes_first + bytes_size;
file_bytes_to(infile, bytes_first);
bytes_to_hex_to(bytes_first, bytes_last, out);
}
template <typename D>
auto DigestHasher<D>::file_hex(std::ifstream& infile) -> hex_array
{
auto out = hex_array{};
file_hex_to(infile, out.data());
return out;
}
template <typename D>
auto DigestHasher<D>::file_hex_str(std::ifstream& infile) -> std::string
{
auto out = std::string(hex_size, 'x'); // An invalid character
file_hex_to(infile, out.data());
return out;
}
}
#endif
|