UPD: Use RtlGenRandom function

This commit is contained in:
Alexander Koblov 2017-08-05 09:46:29 +00:00
commit 5ccc853845

View file

@ -1,6 +1,6 @@
unit uRandom;
{$mode objfpc}{$H+}
{$mode delphi}
interface
@ -12,27 +12,29 @@ procedure Random(ABlock: PByte; ACount: Integer);
implementation
{$IF DEFINED(MSWINDOWS)}
uses JwaWinCrypt;
uses
Windows;
var
RtlGenRandom: function(RandomBuffer: PByte; RandomBufferLength: ULONG): LongBool; stdcall;
{$ENDIF}
procedure Random(ABlock: PByte; ACount: Integer);
var
I: Integer;
{$IF DEFINED(MSWINDOWS)}
Result: Boolean;
phProv: HCRYPTPROV = 0;
{$ENDIF}
Result: Boolean = False;
begin
{$IF DEFINED(MSWINDOWS)}
Result:= CryptAcquireContext(phProv, nil, nil, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
if Result then begin
Result:= CryptGenRandom(phProv, ACount, ABlock);
CryptReleaseContext(phProv, 0);
end;
if not Result then
{$ENDIF}
for I:= 0 to ACount - 1 do ABlock[I]:= Byte(System.Random(256));
Result:= Assigned(RtlGenRandom);
if Result then Result:= RtlGenRandom(ABlock, ACount);
{$ENDIF}
if not Result then for I:= 0 to ACount - 1 do ABlock[I]:= Byte(System.Random(256));
end;
initialization
{$IF DEFINED(MSWINDOWS)}
@RtlGenRandom:= GetProcAddress(GetModuleHandle('advapi32.dll'), 'SystemFunction036');
{$ENDIF}
end.