unit RandomDLL;
{
Dynamically load my little DLL file RANDOM.DLL / RANDOM-X86.DLL in Embarcadero Delphi and use it to generate random Bytes. It's using Microsoft Crypto API. The returned string is hex encoded. The DLL file is Unicode and is available in both x64 and x86 variants. Download the DLL files here:
https://www.andreas-software.com/RANDOM.DLL.ZIP
https://www.andreas-software.com/RANDOM-X86.DLL.ZIP
}
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes;
function GenerateRandomHexBytes(IntBytes: NativeInt): string;
implementation
type
TGenerateRandomHexBytesFunction = function(IntBytes: NativeInt; OutputBuffer: Pointer): NativeInt; stdcall;
function GenerateRandomHexBytes(IntBytes: NativeInt): string;
var DLLFileName: string;
var DLLFunctionName: string;
var DLLHandle: NativeUInt;
var GenerateRandomHexBytesFunction: TGenerateRandomHexBytesFunction;
begin
Result := '';
if IntBytes > 0 then begin
DLLFileName := 'RANDOM';
{$IFDEF WIN32}
DLLFileName := DLLFileName + '-X86';
{$ENDIF}
DLLFileName := DLLFileName + '.DLL';
DLLFunctionName := 'GenerateRandomHexBytes';
DLLHandle := LoadLibrary(PWideChar(DLLFileName));
if DLLHandle <> 0 then begin
@GenerateRandomHexBytesFunction := GetProcAddress(DLLHandle, PWideChar(DLLFunctionName));
if Assigned(GenerateRandomHexBytesFunction) = True then begin
SetLength(Result, IntBytes * 2);
if GenerateRandomHexBytesFunction(IntBytes, Pointer(Result)) = ((IntBytes * 2) * 2) then begin
// OK.
end
else begin
Result := '';
end;
end;
FreeLibrary(DLLHandle);
end;
end;
end;
end.
The source code is related to KSADLL.PAS.
The source code is used in AS-File Crypt, AS-Text Crypt and others.