[+] Added stub for fast rng
[*] Fixed RNG strings may contain a null byte [+] Added RngByte, RngArray, RngBoolean
This commit is contained in:
parent
28a840f71d
commit
1ff63f3479
@ -10,17 +10,32 @@
|
||||
namespace Aurora::RNG
|
||||
{
|
||||
AUKN_SYM void ReadSecureRNG(void *in, AuUInt length);
|
||||
|
||||
template<typename T, int N>
|
||||
AUKN_SYM void ReadFastRNG(void *in, AuUInt length);
|
||||
|
||||
template<bool fast = true, typename T, int N>
|
||||
void Read(T(&array)[N])
|
||||
{
|
||||
ReadSecureRNG(array, N * sizeof(T));
|
||||
if constexpr (fast)
|
||||
{
|
||||
ReadFastRNG(array, N * sizeof(T));
|
||||
}
|
||||
else
|
||||
{
|
||||
ReadSecureRNG(array, N * sizeof(T));
|
||||
}
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
template<bool fast = true, typename T>
|
||||
void Read(T *array, AuUInt length)
|
||||
{
|
||||
ReadSecureRNG(array, length * sizeof(T));
|
||||
if constexpr (fast)
|
||||
{
|
||||
ReadFastRNG(array, length * sizeof(T));
|
||||
}
|
||||
else
|
||||
{
|
||||
ReadSecureRNG(array, length * sizeof(T));
|
||||
}
|
||||
}
|
||||
|
||||
enum class RngStringCharacters
|
||||
@ -28,12 +43,12 @@ namespace Aurora::RNG
|
||||
eAlphaCharacters,
|
||||
eAlphaNumericCharacters,
|
||||
eExtendedEntropy,
|
||||
MAX
|
||||
eCount
|
||||
};
|
||||
|
||||
static void RngString(char *string, AuUInt length, RngStringCharacters type = RngStringCharacters::eAlphaCharacters)
|
||||
{
|
||||
static std::pair<const char *, int> rngSequence[static_cast<int>(RngStringCharacters::MAX)] =
|
||||
static std::pair<const char *, int> rngSequence[static_cast<int>(RngStringCharacters::eCount)] =
|
||||
{
|
||||
{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ", 52},
|
||||
{"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", 62},
|
||||
@ -46,8 +61,34 @@ namespace Aurora::RNG
|
||||
|
||||
for (auto i = 0; i < length; i++)
|
||||
{
|
||||
auto idx = static_cast<float>(reinterpret_cast<AuUInt8 *>(string)[i]) / 255.f * static_cast<float>(pair.second);
|
||||
auto idx = std::floor(static_cast<float>(reinterpret_cast<AuUInt8 *>(string)[i]) / 255.f * static_cast<float>(pair.second - 1));
|
||||
string[i] = pair.first[static_cast<int>(idx)];
|
||||
}
|
||||
}
|
||||
|
||||
static bool RngBoolean()
|
||||
{
|
||||
AuUInt8 x[1];
|
||||
Read<true>(x);
|
||||
return x[0] > 127;
|
||||
}
|
||||
|
||||
static AuUInt8 RngByte()
|
||||
{
|
||||
AuUInt8 x[1];
|
||||
Read<true>(x);
|
||||
return x[0];
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static T &RngArray(T *items, AuUInt count)
|
||||
{
|
||||
return items[static_cast<int>(std::floor(static_cast<float>(RngByte()) / 255.f * static_cast<float>(count - 1))];
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static T &RngVector(const AuList<T> &items)
|
||||
{
|
||||
return RngArray(items.data(), items.size());
|
||||
}
|
||||
}
|
@ -173,6 +173,12 @@ namespace Aurora::RNG
|
||||
SysAssertExp(bytes == length, "Couldn't consume {} RNG bytes", length);
|
||||
}
|
||||
|
||||
AUKN_SYM void ReadFastRNG(void *in, AuUInt length)
|
||||
{
|
||||
// TODO: implement me
|
||||
return ReadSecureRNG(in, length);
|
||||
}
|
||||
|
||||
void Init()
|
||||
{
|
||||
InitRandPlatform();
|
||||
|
Loading…
Reference in New Issue
Block a user