Reece Wilson
2db80bbefd
[+] IRandomDevice::NextFillBuffer
[*] Regression in tests using rng blobs: FillRange is no longer the routine we need to call on buffers. (184fecb8
)
115 lines
3.0 KiB
C++
115 lines
3.0 KiB
C++
/***
|
|
Copyright (C) 2021 J Reece Wilson (a/k/a "Reece"). All rights reserved.
|
|
|
|
File: RNG.hpp
|
|
Date: 2021-7-14
|
|
Author: Reece
|
|
***/
|
|
#pragma once
|
|
|
|
#include "ERngStringCharacters.hpp"
|
|
#include "RandomDef.hpp"
|
|
#include "IRandomDevice.hpp"
|
|
|
|
namespace Aurora::RNG
|
|
{
|
|
AUKN_SYM void ReadSecureRNG(void *in, AuUInt32 length);
|
|
AUKN_SYM void ReadFastRNG (void *in, AuUInt32 length);
|
|
|
|
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>
|
|
static auline void RngFillArray(T(&array)[N])
|
|
{
|
|
if constexpr (fast)
|
|
{
|
|
ReadFastRNG(array, N * sizeof(T));
|
|
}
|
|
else
|
|
{
|
|
ReadSecureRNG(array, N * sizeof(T));
|
|
}
|
|
}
|
|
|
|
template<bool fast = true, typename T>
|
|
static auline void RngFillArray(T *array, AuUInt32 length)
|
|
{
|
|
if constexpr (fast)
|
|
{
|
|
ReadFastRNG(array, length * sizeof(T));
|
|
}
|
|
else
|
|
{
|
|
ReadSecureRNG(array, length * sizeof(T));
|
|
}
|
|
}
|
|
|
|
template<bool fast = true, typename T>
|
|
static auline void RngFillRange(const T &container)
|
|
{
|
|
RngFillArray<fast>(container.begin(), container.end() - container.begin());
|
|
}
|
|
|
|
static auline void RngFillBuffer(Memory::ByteBuffer &buffer)
|
|
{
|
|
auto view = buffer.GetNextLinearWrite();
|
|
RngFillRange(view);
|
|
buffer.writePtr += view.length;
|
|
}
|
|
|
|
/**
|
|
* @brief
|
|
* @deprecated
|
|
*/
|
|
static auline void RngFillRange(Memory::ByteBuffer &buffer)
|
|
{
|
|
RngFillBuffer(buffer);
|
|
}
|
|
|
|
template<bool fast = true, typename T>
|
|
static auline T RngTmpl()
|
|
{
|
|
T ret {};
|
|
if constexpr (fast)
|
|
{
|
|
ReadFastRNG(&ret, sizeof(T));
|
|
}
|
|
else
|
|
{
|
|
ReadSecureRNG(&ret, sizeof(T));
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
template<typename T>
|
|
static auline T &RngArray(T *items, AuUInt32 count)
|
|
{
|
|
return items[RngIndex(count)];
|
|
}
|
|
|
|
template<typename T>
|
|
static auline T &RngVector(const AuList<T> &items)
|
|
{
|
|
return RngArray(items.data(), items.size());
|
|
}
|
|
|
|
template<typename T>
|
|
static auline T &RngRange(const T &items)
|
|
{
|
|
return RngArray(items.begin(), items.end() - items.begin());
|
|
}
|
|
} |