AuroraRuntime/Include/Aurora/RNG/RNG.hpp
2021-06-27 22:25:29 +01:00

53 lines
1.5 KiB
C++

/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: RNG.hpp
Date: 2021-6-10
Author: Reece
***/
#pragma once
namespace Aurora::RNG
{
AUKN_SYM void ReadSecureRNG(void *in, AuUInt length);
template<typename T, int N>
void Read(T(&array)[N])
{
ReadSecureRNG(array, N * sizeof(T));
}
template<typename T>
void Read(T *array, AuUInt length)
{
ReadSecureRNG(array, length * sizeof(T));
}
enum class RngStringCharacters
{
eAlphaCharacters,
eAlphaNumericCharacters,
eExtendedEntropy,
MAX
};
static void RngString(char *string, AuUInt length, RngStringCharacters type = RngStringCharacters::eAlphaCharacters)
{
static std::pair<const char *, int> rngSequence[static_cast<int>(RngStringCharacters::MAX)] =
{
{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 52},
{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", 62},
{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890=-+!$%^*.[];:", 75}
};
ReadSecureRNG(string, length);
const auto &pair = rngSequence[static_cast<int>(type)];
for (auto i = 0; i < length; i++)
{
auto idx = static_cast<float>(reinterpret_cast<AuUInt8 *>(string)[i]) / 255.f * static_cast<float>(pair.second);
string[i] = pair.first[static_cast<int>(idx)];
}
}
}