mirror of
https://github.com/doublecmd/doublecmd.git
synced 2026-06-21 09:58:13 +00:00
UPD: Use RtlGenRandom function
This commit is contained in:
parent
7e91f44ec3
commit
5ccc853845
1 changed files with 16 additions and 14 deletions
|
|
@ -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.
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue