2020-05-18 17:40:57 -07:00
|
|
|
{********************************************************}
|
|
|
|
{ }
|
|
|
|
{ Olive BBS }
|
|
|
|
{ }
|
|
|
|
{ Copyleft (ↄ) 2020 Olive BBS }
|
|
|
|
{ }
|
|
|
|
{ This file is part of Olive BBS }
|
|
|
|
{ }
|
|
|
|
{ Olive BBS is free software: you can redistribute it }
|
|
|
|
{ and/or modify it under the terms of the GNU General }
|
|
|
|
{ Public License as published by the Free Software }
|
|
|
|
{ Foundation, either version 3 of the License, or }
|
|
|
|
{ (at your option) any later version. }
|
|
|
|
{ }
|
|
|
|
{ Olive BBS is distributed in the hope that it will }
|
|
|
|
{ be useful, but WITHOUT ANY WARRANTY; without even }
|
|
|
|
{ the implied warranty of MERCHANTABILITY or FITNESS }
|
|
|
|
{ FOR A PARTICULAR PURPOSE. See the GNU General }
|
|
|
|
{ Public License for more details. }
|
|
|
|
{ }
|
|
|
|
{ You should have received a copy of the GNU General }
|
|
|
|
{ Public License along with Olive BBS. If not, see }
|
|
|
|
{ <http://www.gnu.org/licenses/>. }
|
|
|
|
{ }
|
|
|
|
{********************************************************}
|
|
|
|
{ ___ ___ ___ }
|
|
|
|
{ ( ).-. ( ) ( ) }
|
|
|
|
{ .--. | |( __)___ ___ .--. | |.-. | |.-. .--. }
|
|
|
|
{ / \| |(''"( )( / \| / \| / \ / _ \ }
|
|
|
|
{ | .-. | | | | | | | | .-. | .-. | .-. |. .' `. ; }
|
|
|
|
{ | | | | | | | | | | | | | | | | | | | || ' | | }
|
|
|
|
{ | | | | | | | | | | | |/ | | | | | | |_\_`.(___) }
|
|
|
|
{ | | | | | | | | | | | ' _.| | | | | | ( ). '. }
|
|
|
|
{ | ' | | | | | ' ' ; | .'.-| ' | | ' | || | `\ | }
|
|
|
|
{ ' `-' | | | | \ `' /' `-' ' `-' ;' `-' ; ; '._,' ' }
|
|
|
|
{ `.__.(___(___) '_.' `.__.' `.__. `.__. '.___.' }
|
|
|
|
{ }
|
|
|
|
{********************************************************}
|
|
|
|
|
|
|
|
{$mode objfpc}{$H+}
|
|
|
|
{$interfaces corba}
|
|
|
|
Unit Olive.Random;
|
|
|
|
|
|
|
|
interface
|
|
|
|
|
|
|
|
uses
|
2020-08-01 16:30:51 -07:00
|
|
|
Objects, Classes, SysUtils,
|
2020-05-18 17:40:57 -07:00
|
|
|
Olive.Random.RandomInterface,
|
|
|
|
{$IF DEFINED(LINUX)}
|
2020-08-01 16:30:51 -07:00
|
|
|
Olive.Random.Linux
|
2020-05-18 17:40:57 -07:00
|
|
|
{$ELSEIF DEFINED(WINDOWS)}
|
2020-08-01 16:30:51 -07:00
|
|
|
Olive.Random.Windows
|
2020-05-18 17:40:57 -07:00
|
|
|
{$ELSEIF DEFINED(BSD)}
|
2020-08-01 16:30:51 -07:00
|
|
|
Olive.Random.BSD
|
2020-05-18 17:40:57 -07:00
|
|
|
{$ELSE}
|
|
|
|
Olive.Random.Generic;
|
|
|
|
{$ENDIF}
|
|
|
|
;
|
|
|
|
|
|
|
|
type
|
|
|
|
PRandom = ^TRandom;
|
|
|
|
TRandom = class (RandomTrait, RandomInterface)
|
|
|
|
private
|
|
|
|
FRandomGenerator : RandomInterface;
|
|
|
|
procedure SetRandomGenerator(GeneratorClass : RandomInterface);
|
|
|
|
public
|
|
|
|
constructor Init;
|
|
|
|
destructor Destroy; override;
|
|
|
|
procedure SetDefaultGenerator;
|
|
|
|
function GetBytes(NBytes : SizeUInt) : TBytes;
|
|
|
|
function GetString(NBytes : SizeUInt) : AnsiString;
|
|
|
|
property RandomGenerator : RandomInterface read FRandomGenerator write SetRandomGenerator;
|
|
|
|
end;
|
|
|
|
|
|
|
|
implementation
|
|
|
|
|
|
|
|
|
|
|
|
constructor TRandom.Init;
|
|
|
|
begin
|
|
|
|
SetDefaultGenerator;
|
|
|
|
end;
|
|
|
|
|
|
|
|
destructor TRandom.Destroy;
|
|
|
|
begin
|
|
|
|
inherited Destroy;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TRandom.SetRandomGenerator(GeneratorClass : RandomInterface);
|
|
|
|
begin
|
|
|
|
FRandomGenerator := GeneratorClass;
|
|
|
|
end;
|
|
|
|
|
|
|
|
procedure TRandom.SetDefaultGenerator;
|
|
|
|
begin
|
|
|
|
{$IF DEFINED(LINUX)}
|
|
|
|
FRandomGenerator := LinuxRandom.Create;
|
|
|
|
{$ELSEIF DEFINED(WINDOWS)}
|
2020-08-01 16:30:51 -07:00
|
|
|
FRandomGenerator := WindowsRandom.Create;
|
2020-05-18 17:40:57 -07:00
|
|
|
{$ELSEIF DEFINED(BSD)}
|
|
|
|
FRandomGenerator := BSDRandom.Create;
|
|
|
|
{$ELSE}
|
|
|
|
FRandomGenerator := RandomGeneric.Create;
|
|
|
|
{$ENDIF}
|
|
|
|
end;
|
|
|
|
|
|
|
|
function TRandom.GetBytes(NBytes : SizeUInt) : TBytes;
|
|
|
|
begin
|
|
|
|
SetLength(Result, NBytes);
|
|
|
|
Result := FRandomGenerator.GetBytes(NBytes);
|
|
|
|
end;
|
|
|
|
function TRandom.GetString(NBytes : SizeUInt) : AnsiString;
|
|
|
|
begin
|
|
|
|
SetLength(Result, NBytes);
|
|
|
|
Result := FRandomGenerator.GetString(NBytes);
|
|
|
|
|
|
|
|
end;
|
|
|
|
|
|
|
|
end.
|