CryptoByLeo/Src/URandom/BCrypt.mqh

83 lines
3.1 KiB
MQL5

2026-09-01 12:16:15 -05:00
//+------------------------------------------------------------------+
//| BCrypt.mqh |
//| Copyright 2026, Niquel Mendoza |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2026, Niquel Mendoza"
#property link "https://www.mql5.com"
#property strict
#ifndef CRYPTOBYLEO_SRC_URANDOM_BCRYPT_MQH
#define CRYPTOBYLEO_SRC_URANDOM_BCRYPT_MQH
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
2026-09-01 15:15:16 -05:00
#include "..\\BCrypt\\Main.mqh"
#include <TSN\\Tables\\Bits.mqh>
2026-09-01 12:16:15 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
namespace TSN
{
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
class CURandom
{
public:
CURandom(void) {}
~CURandom(void) {}
//----
static __forceinline void RandomCryptoBytes(uchar& out[], int oult, int o_s = 0);
2026-09-01 15:15:16 -05:00
static __forceinline void RandomUlongArr(ulong& out[], int oult, int o_s = 0);
2026-09-01 12:25:55 -05:00
static void RandomUlongArrRange(ulong& out[], int oult, ulong last, int o_s = 0);
2026-09-01 12:16:15 -05:00
};
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
static __forceinline void CURandom::RandomCryptoBytes(uchar &out[], int oult, int o_s = 0)
{
2026-09-01 15:15:16 -05:00
BCryptRandomWOffset(out, o_s, oult);
2026-09-01 12:16:15 -05:00
}
2026-09-01 15:15:16 -05:00
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
static __forceinline void CURandom::RandomUlongArr(ulong& out[], int oult, int o_s = 0)
{
BCryptRandomWOffset(out, sizeof(ulong) * o_s, sizeof(ulong) * oult);
}
//+------------------------------------------------------------------+
static void CURandom::RandomUlongArrRange(ulong& out[], int oult, ulong last, int o_s = 0)
{
// completos
BCryptRandomWOffset(out, sizeof(ulong) * o_s, sizeof(ulong) * oult);
//resto
o_s += (oult - 1);
//---
// genermaos una mascarac que cobra el rango de [0 - last]
ulong mask = last;
mask |= mask >> 1;
mask |= mask >> 2;
mask |= mask >> 4;
mask |= mask >> 8;
mask |= mask >> 16;
mask |= mask >> 32;
//---
ulong candidate = out[o_s] & mask;
while(candidate > last)
{
BCryptRandomWOffset(out, sizeof(ulong) * o_s, sizeof(ulong));
candidate = out[o_s] & mask;
}
out[o_s] = candidate;
}
2026-09-01 12:16:15 -05:00
}
//+------------------------------------------------------------------+
#endif // CRYPTOBYLEO_SRC_URANDOM_BCRYPT_MQH