7#include <BufferStream.h>
11std::vector<std::byte>
VFONT::encrypt(std::span<const std::byte> data, uint8_t saltLength) {
12 std::vector<std::byte> out;
13 BufferStream stream{out};
15 static std::random_device random_device{};
16 static std::mt19937 generator{random_device()};
17 std::uniform_int_distribution<> distribution{0, (std::numeric_limits<uint8_t>::max())};
19 std::vector<std::byte> salt;
20 salt.resize(saltLength);
22 uint8_t magic =
MAGIC;
23 for (
int i = 0; i < saltLength; i++) {
24 salt[i] =
static_cast<std::byte
>(distribution(generator));
25 magic ^=
static_cast<uint8_t
>(salt[i]) +
MAGIC;
28 for (
auto byte : data) {
29 const uint8_t encrypted =
static_cast<uint8_t
>(byte) ^ magic;
31 magic = encrypted +
MAGIC;
35 .write<uint8_t>(saltLength + 1)
38 out.resize(stream.size());
43 BufferStreamReadOnly reader{data.data(), data.size()};
48 reader.seek(
SIGNATURE.length() +
sizeof(uint8_t), std::ios::end);
50 const auto bytes = reader.read<uint8_t>();
52 std::vector<std::byte> out;
53 out.resize(reader.size() -
SIGNATURE.length() - bytes);
55 reader.seek(-
static_cast<int>(bytes), std::ios::cur);
56 uint8_t magic =
MAGIC;
57 for (
int i = 0; i < bytes - 1; i++) {
58 magic ^= reader.read<uint8_t>() +
MAGIC;
62 for (
auto& outByte : out) {
63 const auto byte = reader.read<uint8_t>();
64 outByte =
static_cast<std::byte
>(
byte ^ magic);
constexpr std::string_view SIGNATURE
std::vector< std::byte > encrypt(std::span< const std::byte > data, uint8_t saltLength=2)
std::vector< std::byte > decrypt(std::span< const std::byte > data)