AuroraRuntime/Include/Aurora/RNG/RNG.hpp

86 lines
2.2 KiB
C++
Raw Normal View History

2021-06-27 21:25:29 +00:00
/***
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
File: RNG.hpp
2021-09-06 10:58:08 +00:00
Date: 2021-7-14
2021-06-27 21:25:29 +00:00
Author: Reece
***/
#pragma once
2021-09-06 10:58:08 +00:00
#include "ERngStringCharacters.hpp"
#include "IRandomDevice.hpp"
2021-06-27 21:25:29 +00:00
namespace Aurora::RNG
{
2021-09-06 10:58:08 +00:00
AUKN_SYM void ReadSecureRNG(void *in, AuUInt32 length);
AUKN_SYM void ReadFastRNG (void *in, AuUInt32 length);
2021-09-06 10:58:08 +00:00
AUKN_SYM AuString ReadString(AuUInt32 length, ERngStringCharacters type = ERngStringCharacters::eAlphaCharacters);
AUKN_SYM void RngString(char *string, AuUInt32 length, ERngStringCharacters type = ERngStringCharacters::eAlphaCharacters);
AUKN_SYM AuUInt8 RngByte();
AUKN_SYM bool RngBoolean();
AUKN_SYM AuUInt32 RngU32();
AUKN_SYM AuUInt32 RngU32(AuUInt32 min, AuUInt32 max);
AUKN_SYM AuUInt64 RngU64();
AUKN_SYM AuInt32 RngInt(AuInt32 min, AuInt32 max);
AUKN_SYM double RngDecimal();
AUKN_SYM float RngNumber(float min, float max);
AUKN_SYM AuUInt32 RngIndex(AuUInt32 count /* = max + 1*/);
// Note: it is conceivable that someone may want the following templates for some cryptographic purpose
template<bool fast = true, typename T, int N>
2021-09-06 10:58:08 +00:00
static inline void RngArray(T(&array)[N])
2021-06-27 21:25:29 +00:00
{
if constexpr (fast)
{
ReadFastRNG(array, N * sizeof(T));
}
else
{
ReadSecureRNG(array, N * sizeof(T));
}
2021-06-27 21:25:29 +00:00
}
template<bool fast = true, typename T>
2021-09-06 10:58:08 +00:00
static inline void RngArray(T *array, AuUInt32 length)
2021-06-27 21:25:29 +00:00
{
if constexpr (fast)
{
ReadFastRNG(array, length * sizeof(T));
}
else
{
ReadSecureRNG(array, length * sizeof(T));
}
2021-06-27 21:25:29 +00:00
}
2021-09-06 10:58:08 +00:00
template<bool fast = true, typename T>
static inline T RngTmpl()
2021-06-27 21:25:29 +00:00
{
2021-09-06 10:58:08 +00:00
T ret {};
if constexpr (fast)
2021-06-27 21:25:29 +00:00
{
2021-09-06 10:58:08 +00:00
ReadFastRNG(&ret, sizeof(T));
}
else
2021-06-27 21:25:29 +00:00
{
2021-09-06 10:58:08 +00:00
ReadSecureRNG(&ret, sizeof(T));
2021-06-27 21:25:29 +00:00
}
2021-09-06 10:58:08 +00:00
return ret;
2021-07-13 12:13:21 +00:00
}
template<typename T>
2021-09-06 10:58:08 +00:00
static inline T &RngArray(T *items, AuUInt32 count)
{
2021-09-06 10:58:08 +00:00
return items[RngIndex(count)];
}
template<typename T>
2021-07-13 12:13:21 +00:00
static inline T &RngVector(const AuList<T> &items)
{
return RngArray(items.data(), items.size());
}
2021-06-27 21:25:29 +00:00
}