Renegade -> Olive BBS

This commit is contained in:
R. Eric Wheeler 2020-05-18 17:40:57 -07:00
parent 4f09e7deb6
commit d0ee467262
12 changed files with 601 additions and 574 deletions

107
Olive.Random.BSDRandom.pas Normal file
View File

@ -0,0 +1,107 @@
{********************************************************}
{ }
{ 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+}
Unit Olive.Random.BSDRandom;
interface
uses
CTypes,
Objects,
Classes,
SysUtils,
Olive.Random.RandomInterface,
Olive.Random.URandom;
type
PCChar = ^CChar;
PBSDRandom = ^BSDRandom;
BSDRandom = class (RandomTrait, RandomInterface)
private
function GetSystemBytes(var RandomByteBuffer : TBytes; NBytes : SizeUint) : CInt;
public
function GetBytes(NBytes : SizeUInt) : TBytes;
function GetString(NBytes : SizeUInt) : AnsiString;
end;
procedure arc4random_buf(var Buffer; NBytes : csize_t);
cdecl;external 'c' name 'arc4random_buf';
implementation
function BSDRandom.GetSystemBytes(var RandomByteBuffer : TBytes; NBytes : SizeUint) : CInt;
var
CharBuffer : array of pcuint8;
begin
SetLength(CharBuffer, NBytes);
arc4random_buf(CharBuffer[0], NBytes);
if Length(CharBuffer) <> NBytes then
begin
RandomByteBuffer[0] := 0;
Result := -1;
end else
begin
Move(CharBuffer[Low(CharBuffer)], RandomByteBuffer[0], NBytes);
Result := High(RandomByteBuffer);
end;
end;
function BSDRandom.GetBytes(NBytes : SizeUInt) : TBytes;
var
RandomBuffer : AnsiString;
begin
SetLength(RandomBuffer, NBytes);
SetLength(Result, NBytes);
RandomBuffer := GetString(NBytes);
Move(RandomBuffer[1], Result[0], NBytes);
end;
function BSDRandom.GetString(NBytes : SizeUInt) : AnsiString;
var
RandomBuffer : TBytes;
B : SizeInt;
begin
SetLength(RandomBuffer, (NBytes*2));
SetLength(Result, NBytes);
B := GetSystemBytes(RandomBuffer, (NBytes*2));
Move(RandomBuffer[0], Result[1], NBytes);
end;
end.

81
Olive.Random.Generic.pas Normal file
View File

@ -0,0 +1,81 @@
{********************************************************}
{ }
{ 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+}
Unit Olive.Random.Generic;
interface
uses
Objects,
Classes,
SysUtils,
Olive.Random.RandomInterface;
type
PRandomGeneric = ^RandomGeneric;
RandomGeneric = class (RandomTrait, RandomInterface)
public
function GetBytes(NBytes : SizeUInt) : TBytes;
function GetString(NBytes : SizeUInt) : AnsiString;
end;
implementation
function RandomGeneric.GetBytes(NBytes : SizeUInt) : TBytes;
var
RandomBuffer : AnsiString;
begin
SetLength(Result, NBytes);
RandomBuffer := GetString(NBytes);
Move(RandomBuffer[1], Result[0], NBytes);
end;
function RandomGeneric.GetString(NBytes : SizeUInt) : AnsiString;
var
ByteBuffer : TBytes;
begin
SetLength(Result, NBytes);
SetLength(ByteBuffer, (NBytes*2));
ByteBuffer := MTRandomBytes((NBytes*2));
Move(ByteBuffer[0], Result[1], NBytes);
end;
end.

View File

@ -1,40 +1,44 @@
{*******************************************************} {********************************************************}
{ } { }
{ Renegade BBS } { Olive BBS }
{ } { }
{ Copyright (c) 1990-2013 The Renegade Dev Team } { Copyleft (ↄ) 2020 Olive BBS }
{ Copyleft (ↄ) 2016 Renegade BBS } { }
{ } { This file is part of Olive BBS }
{ This file is part of Renegade BBS } { }
{ } { Olive BBS is free software: you can redistribute it }
{ Renegade is free software: you can redistribute it } { and/or modify it under the terms of the GNU General }
{ and/or modify it under the terms of the GNU General } { Public License as published by the Free Software }
{ Public License as published by the Free Software } { Foundation, either version 3 of the License, or }
{ Foundation, either version 3 of the License, or } { (at your option) any later version. }
{ (at your option) any later version. } { }
{ } { Olive BBS is distributed in the hope that it will }
{ Renegade is distributed in the hope that it will be } { be useful, but WITHOUT ANY WARRANTY; without even }
{ useful, but WITHOUT ANY WARRANTY; without even the } { the implied warranty of MERCHANTABILITY or FITNESS }
{ implied warranty of MERCHANTABILITY or FITNESS FOR } { FOR A PARTICULAR PURPOSE. See the GNU General }
{ A PARTICULAR PURPOSE. See the GNU General Public } { Public License for more details. }
{ License for more details. } { }
{ } { You should have received a copy of the GNU General }
{ You should have received a copy of the GNU General } { Public License along with Olive BBS. If not, see }
{ Public License along with Renegade. If not, see } { <http://www.gnu.org/licenses/>. }
{ <http://www.gnu.org/licenses/>. } { }
{ } {********************************************************}
{*******************************************************} { ___ ___ ___ }
{ _______ __ } { ( ).-. ( ) ( ) }
{ | _ .-----.-----.-----.-----.---.-.--| .-----. } { .--. | |( __)___ ___ .--. | |.-. | |.-. .--. }
{ |. l | -__| | -__| _ | _ | _ | -__| } { / \| |(''"( )( / \| / \| / \ / _ \ }
{ |. _ |_____|__|__|_____|___ |___._|_____|_____| } { | .-. | | | | | | | | .-. | .-. | .-. |. .' `. ; }
{ |: | | |_____| } { | | | | | | | | | | | | | | | | | | | || ' | | }
{ |::.|:. | } { | | | | | | | | | | | |/ | | | | | | |_\_`.(___) }
{ `--- ---' } { | | | | | | | | | | | ' _.| | | | | | ( ). '. }
{*******************************************************} { | ' | | | | | ' ' ; | .'.-| ' | | ' | || | `\ | }
{ ' `-' | | | | \ `' /' `-' ' `-' ;' `-' ; ; '._,' ' }
{ `.__.(___(___) '_.' `.__.' `.__. `.__. '.___.' }
{ }
{********************************************************}
{$mode objfpc}{$H+} {$mode objfpc}{$H+}
Unit Renegade.Random.LinuxRandom; Unit Olive.Random.LinuxRandom;
interface interface
@ -43,7 +47,7 @@ uses
Objects, Objects,
Classes, Classes,
SysUtils, SysUtils,
Renegade.Random.RandomInterface; Olive.Random.RandomInterface;
const const
{$IF DEFINED(CPU64)} SYS_getrandom = 318; {$IF DEFINED(CPU64)} SYS_getrandom = 318;

View File

@ -0,0 +1,90 @@
{********************************************************}
{ }
{ 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}
{$codepage UTF8}
Unit Olive.Random.RandomInterface;
interface
uses
Classes,
SysUtils;
type
RandomInterface = interface
['{0750E585-C1D2-4C1F-A8A4-4EDC41847396}']
function GetBytes(NBytes : SizeUInt) : TBytes;
function GetString(NBytes : SizeUInt) : AnsiString;
end;
RandomTrait = class (TObject)
public
constructor Create;
destructor Destroy; override;
function MTRandomBytes(NBytes : SizeUInt) : TBytes; virtual;
end;
implementation
constructor RandomTrait.Create;
begin
inherited Create;
end;
destructor RandomTrait.Destroy;
begin
inherited Destroy;
end;
function RandomTrait.MTRandomBytes(NBytes : SizeUInt) : TBytes;
var
i : SizeUint;
begin
Randomize;
SetLength(Result, (NBytes*2));
for i := 0 to (NBytes*2) do
begin
Result[i] := Random(MaxInt) mod 256;
end;
SetLength(Result, NBytes);
end;
end.

View File

@ -1,40 +1,44 @@
{*******************************************************} {********************************************************}
{ } { }
{ Renegade BBS } { Olive BBS }
{ } { }
{ Copyright (c) 1990-2013 The Renegade Dev Team } { Copyleft (ↄ) 2020 Olive BBS }
{ Copyleft (ↄ) 2016 Renegade BBS } { }
{ } { This file is part of Olive BBS }
{ This file is part of Renegade BBS } { }
{ } { Olive BBS is free software: you can redistribute it }
{ Renegade is free software: you can redistribute it } { and/or modify it under the terms of the GNU General }
{ and/or modify it under the terms of the GNU General } { Public License as published by the Free Software }
{ Public License as published by the Free Software } { Foundation, either version 3 of the License, or }
{ Foundation, either version 3 of the License, or } { (at your option) any later version. }
{ (at your option) any later version. } { }
{ } { Olive BBS is distributed in the hope that it will }
{ Renegade is distributed in the hope that it will be } { be useful, but WITHOUT ANY WARRANTY; without even }
{ useful, but WITHOUT ANY WARRANTY; without even the } { the implied warranty of MERCHANTABILITY or FITNESS }
{ implied warranty of MERCHANTABILITY or FITNESS FOR } { FOR A PARTICULAR PURPOSE. See the GNU General }
{ A PARTICULAR PURPOSE. See the GNU General Public } { Public License for more details. }
{ License for more details. } { }
{ } { You should have received a copy of the GNU General }
{ You should have received a copy of the GNU General } { Public License along with Olive BBS. If not, see }
{ Public License along with Renegade. If not, see } { <http://www.gnu.org/licenses/>. }
{ <http://www.gnu.org/licenses/>. } { }
{ } {********************************************************}
{*******************************************************} { ___ ___ ___ }
{ _______ __ } { ( ).-. ( ) ( ) }
{ | _ .-----.-----.-----.-----.---.-.--| .-----. } { .--. | |( __)___ ___ .--. | |.-. | |.-. .--. }
{ |. l | -__| | -__| _ | _ | _ | -__| } { / \| |(''"( )( / \| / \| / \ / _ \ }
{ |. _ |_____|__|__|_____|___ |___._|_____|_____| } { | .-. | | | | | | | | .-. | .-. | .-. |. .' `. ; }
{ |: | | |_____| } { | | | | | | | | | | | | | | | | | | | || ' | | }
{ |::.|:. | } { | | | | | | | | | | | |/ | | | | | | |_\_`.(___) }
{ `--- ---' } { | | | | | | | | | | | ' _.| | | | | | ( ). '. }
{*******************************************************} { | ' | | | | | ' ' ; | .'.-| ' | | ' | || | `\ | }
{ ' `-' | | | | \ `' /' `-' ' `-' ;' `-' ; ; '._,' ' }
{ `.__.(___(___) '_.' `.__.' `.__. `.__. '.___.' }
{ }
{********************************************************}
{$mode objfpc}{$H+} {$mode objfpc}{$H+}
Unit Renegade.Random.URandom; Unit Olive.Random.URandom;
interface interface
@ -43,7 +47,7 @@ uses
Objects, Objects,
Classes, Classes,
SysUtils, SysUtils,
Renegade.Random.RandomInterface; Olive.Random.RandomInterface;
const const
{$IF DEFINED(CPU64)} SYS_getrandom = 318; {$IF DEFINED(CPU64)} SYS_getrandom = 318;

123
Olive.Random.WinRandom.pas Normal file
View File

@ -0,0 +1,123 @@
{********************************************************}
{ }
{ 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+}
Unit Olive.Random.WinRandom;
interface
uses
Objects,
Classes,
SysUtils,
Windows,
Olive.Random.RandomInterface;
const
CRYPT_VERIFYCONTEXT = $F0000000;
CRYPT_MACHINE_KEYSET = 32;
PROV_RSA_FULL = 1;
CRYPT_NEWKEYSET = 8;
type
HCRYPTPROV = ULONG_PTR;
PWinRandom = ^WinRandom;
WinRandom = class (RandomTrait, RandomInterface)
public
function GetBytes(NBytes : SizeUInt) : TBytes;
function GetString(NBytes : SizeUInt) : AnsiString;
end;
function CryptAcquireContextW(var phProv: HCRYPTPROV; pszContainer: LPCTSTR;
pszProvider: LPCTSTR; dwProvType: DWORD; dwFlags: DWORD): BOOL;stdcall; external 'advapi32' name 'CryptAcquireContextW';
function CryptGenRandom(hProv: HCRYPTPROV; dwLen: DWORD;
var pbBuffer: BYTE): BOOL; stdcall; external 'advapi32' name 'CryptGenRandom';
implementation
function WinRandom.GetBytes(NBytes : SizeUInt) : TBytes;
var
RandomBuffer : AnsiString;
begin
SetLength(Result, NBytes);
SetLength(RandomBuffer, NBytes);
RandomBuffer := GetString(NBytes);
Move(RandomBuffer[1], Result[0], NBytes);
end;
function WinRandom.GetString(NBytes : SizeUInt) : AnsiString;
var
RandomBuffer : ^BYTE;
hCryptProv : ^ULONG_PTR;
WinCrypt : Boolean;
ReturnString : AnsiString;
i, Bytes : SizeInt;
ReturnBytes : TBytes;
begin
GetMem(RandomBuffer, (NBytes * 2));
CryptAcquireContextW(hCryptProv^, nil, nil, PROV_RSA_FULL,
CRYPT_NEWKEYSET or CRYPT_MACHINE_KEYSET or CRYPT_VERIFYCONTEXT );
WinCrypt := CryptGenRandom(hCryptProv^, (NBytes * 2), RandomBuffer^);
SetLength(ReturnString, (NBytes*2));
if WinCrypt then
begin
for i := 1 to (NBytes*2) do
begin
ReturnString[i] := Chr(RandomBuffer[i]);
end;
end else
begin
SetLength(ReturnBytes, (NBytes*2));
ReturnBytes := MTRandomBytes((NBytes*2));
end;
SetLength(Result, NBytes);
FreeMem(RandomBuffer, (NBytes * 2));
if WinCrypt then
begin
Move(ReturnString[1], Result[1], NBytes);
end else
begin
Move(ReturnBytes[0], Result[1], NBytes);
end;
end;
end.

120
Olive.Random.pas Normal file
View File

@ -0,0 +1,120 @@
{********************************************************}
{ }
{ 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
Objects,
Classes,
SysUtils,
Olive.Random.RandomInterface,
{$IF DEFINED(LINUX)}
Olive.Random.LinuxRandom
{$ELSEIF DEFINED(WINDOWS)}
Olive.Random.WinRandom
{$ELSEIF DEFINED(BSD)}
Olive.Random.BSDRandom
{$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)}
FRandomGenerator := WinRandom.Create;
{$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.

View File

@ -1,104 +0,0 @@
{*******************************************************}
{ }
{ Renegade BBS }
{ }
{ Copyright (c) 1990-2013 The Renegade Dev Team }
{ Copyleft (ↄ) 2016 Renegade BBS }
{ }
{ This file is part of Renegade BBS }
{ }
{ Renegade 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. }
{ }
{ Renegade 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 Renegade. If not, see }
{ <http://www.gnu.org/licenses/>. }
{ }
{*******************************************************}
{ _______ __ }
{ | _ .-----.-----.-----.-----.---.-.--| .-----. }
{ |. l | -__| | -__| _ | _ | _ | -__| }
{ |. _ |_____|__|__|_____|___ |___._|_____|_____| }
{ |: | | |_____| }
{ |::.|:. | }
{ `--- ---' }
{*******************************************************}
{ ???: Todo - use arc4random_buf }
{$mode objfpc}{$H+}
Unit Renegade.Random.BSDRandom;
interface
uses
CTypes,
Objects,
Classes,
SysUtils,
Renegade.Random.RandomInterface,
Renegade.Random.URandom;
type
PCChar = ^CChar;
PBSDRandom = ^BSDRandom;
BSDRandom = class (RandomTrait, RandomInterface)
private
function GetSystemBytes(var RandomByteBuffer : TBytes; NBytes : SizeUint) : CInt;
public
function GetBytes(NBytes : SizeUInt) : TBytes;
function GetString(NBytes : SizeUInt) : AnsiString;
end;
procedure arc4random_buf(var Buffer; NBytes : csize_t);
cdecl;external 'c' name 'arc4random_buf';
implementation
function BSDRandom.GetSystemBytes(var RandomByteBuffer : TBytes; NBytes : SizeUint) : CInt;
var
CharBuffer : array of pcuint8;
begin
SetLength(CharBuffer, NBytes);
arc4random_buf(CharBuffer[0], NBytes);
if Length(CharBuffer) <> NBytes then
begin
RandomByteBuffer[0] := 0;
Result := -1;
end else
begin
Move(CharBuffer[Low(CharBuffer)], RandomByteBuffer[0], NBytes);
Result := High(RandomByteBuffer);
end;
end;
function BSDRandom.GetBytes(NBytes : SizeUInt) : TBytes;
var
RandomBuffer : AnsiString;
begin
SetLength(RandomBuffer, NBytes);
SetLength(Result, NBytes);
RandomBuffer := GetString(NBytes);
Move(RandomBuffer[1], Result[0], NBytes);
end;
function BSDRandom.GetString(NBytes : SizeUInt) : AnsiString;
var
RandomBuffer : TBytes;
B : SizeInt;
begin
SetLength(RandomBuffer, (NBytes*2));
SetLength(Result, NBytes);
B := GetSystemBytes(RandomBuffer, (NBytes*2));
Move(RandomBuffer[0], Result[1], NBytes);
end;
end.

View File

@ -1,77 +0,0 @@
{*******************************************************}
{ }
{ Renegade BBS }
{ }
{ Copyright (c) 1990-2013 The Renegade Dev Team }
{ Copyleft (ↄ) 2016 Renegade BBS }
{ }
{ This file is part of Renegade BBS }
{ }
{ Renegade 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. }
{ }
{ Renegade 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 Renegade. If not, see }
{ <http://www.gnu.org/licenses/>. }
{ }
{*******************************************************}
{ _______ __ }
{ | _ .-----.-----.-----.-----.---.-.--| .-----. }
{ |. l | -__| | -__| _ | _ | _ | -__| }
{ |. _ |_____|__|__|_____|___ |___._|_____|_____| }
{ |: | | |_____| }
{ |::.|:. | }
{ `--- ---' }
{*******************************************************}
{$mode objfpc}{$H+}
Unit Renegade.Random.Generic;
interface
uses
Objects,
Classes,
SysUtils,
Renegade.Random.RandomInterface;
type
PRandomGeneric = ^RandomGeneric;
RandomGeneric = class (RandomTrait, RandomInterface)
public
function GetBytes(NBytes : SizeUInt) : TBytes;
function GetString(NBytes : SizeUInt) : AnsiString;
end;
implementation
function RandomGeneric.GetBytes(NBytes : SizeUInt) : TBytes;
var
RandomBuffer : AnsiString;
begin
SetLength(Result, NBytes);
RandomBuffer := GetString(NBytes);
Move(RandomBuffer[1], Result[0], NBytes);
end;
function RandomGeneric.GetString(NBytes : SizeUInt) : AnsiString;
var
ByteBuffer : TBytes;
begin
SetLength(Result, NBytes);
SetLength(ByteBuffer, (NBytes*2));
ByteBuffer := MTRandomBytes((NBytes*2));
Move(ByteBuffer[0], Result[1], NBytes);
end;
end.

View File

@ -1,86 +0,0 @@
{*******************************************************}
{ }
{ Renegade BBS }
{ }
{ Copyright (c) 1990-2013 The Renegade Dev Team }
{ Copyleft (ↄ) 2016 Renegade BBS }
{ }
{ This file is part of Renegade BBS }
{ }
{ Renegade 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. }
{ }
{ Renegade 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 Renegade. If not, see }
{ <http://www.gnu.org/licenses/>. }
{ }
{*******************************************************}
{ _______ __ }
{ | _ .-----.-----.-----.-----.---.-.--| .-----. }
{ |. l | -__| | -__| _ | _ | _ | -__| }
{ |. _ |_____|__|__|_____|___ |___._|_____|_____| }
{ |: | | |_____| }
{ |::.|:. | }
{ `--- ---' }
{*******************************************************}
{$mode objfpc}{$H+}
{$interfaces corba}
{$codepage UTF8}
Unit Renegade.Random.RandomInterface;
interface
uses
Classes,
SysUtils;
type
RandomInterface = interface
['{0750E585-C1D2-4C1F-A8A4-4EDC41847396}']
function GetBytes(NBytes : SizeUInt) : TBytes;
function GetString(NBytes : SizeUInt) : AnsiString;
end;
RandomTrait = class (TObject)
public
constructor Create;
destructor Destroy; override;
function MTRandomBytes(NBytes : SizeUInt) : TBytes; virtual;
end;
implementation
constructor RandomTrait.Create;
begin
inherited Create;
end;
destructor RandomTrait.Destroy;
begin
inherited Destroy;
end;
function RandomTrait.MTRandomBytes(NBytes : SizeUInt) : TBytes;
var
i : SizeUint;
begin
Randomize;
SetLength(Result, (NBytes*2));
for i := 0 to (NBytes*2) do
begin
Result[i] := Random(MaxInt) mod 256;
end;
SetLength(Result, NBytes);
end;
end.

View File

@ -1,119 +0,0 @@
{*******************************************************}
{ }
{ Renegade BBS }
{ }
{ Copyright (c) 1990-2013 The Renegade Dev Team }
{ Copyleft (ↄ) 2016 Renegade BBS }
{ }
{ This file is part of Renegade BBS }
{ }
{ Renegade 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. }
{ }
{ Renegade 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 Renegade. If not, see }
{ <http://www.gnu.org/licenses/>. }
{ }
{*******************************************************}
{ _______ __ }
{ | _ .-----.-----.-----.-----.---.-.--| .-----. }
{ |. l | -__| | -__| _ | _ | _ | -__| }
{ |. _ |_____|__|__|_____|___ |___._|_____|_____| }
{ |: | | |_____| }
{ |::.|:. | }
{ `--- ---' }
{*******************************************************}
{$mode objfpc}{$H+}
Unit Renegade.Random.WinRandom;
interface
uses
Objects,
Classes,
SysUtils,
Windows,
Renegade.Random.RandomInterface;
const
CRYPT_VERIFYCONTEXT = $F0000000;
CRYPT_MACHINE_KEYSET = 32;
PROV_RSA_FULL = 1;
CRYPT_NEWKEYSET = 8;
type
HCRYPTPROV = ULONG_PTR;
PWinRandom = ^WinRandom;
WinRandom = class (RandomTrait, RandomInterface)
public
function GetBytes(NBytes : SizeUInt) : TBytes;
function GetString(NBytes : SizeUInt) : AnsiString;
end;
function CryptAcquireContextW(var phProv: HCRYPTPROV; pszContainer: LPCTSTR;
pszProvider: LPCTSTR; dwProvType: DWORD; dwFlags: DWORD): BOOL;stdcall; external 'advapi32' name 'CryptAcquireContextW';
function CryptGenRandom(hProv: HCRYPTPROV; dwLen: DWORD;
var pbBuffer: BYTE): BOOL; stdcall; external 'advapi32' name 'CryptGenRandom';
implementation
function WinRandom.GetBytes(NBytes : SizeUInt) : TBytes;
var
RandomBuffer : AnsiString;
begin
SetLength(Result, NBytes);
SetLength(RandomBuffer, NBytes);
RandomBuffer := GetString(NBytes);
Move(RandomBuffer[1], Result[0], NBytes);
end;
function WinRandom.GetString(NBytes : SizeUInt) : AnsiString;
var
RandomBuffer : ^BYTE;
hCryptProv : ^ULONG_PTR;
WinCrypt : Boolean;
ReturnString : AnsiString;
i, Bytes : SizeInt;
ReturnBytes : TBytes;
begin
GetMem(RandomBuffer, (NBytes * 2));
CryptAcquireContextW(hCryptProv^, nil, nil, PROV_RSA_FULL,
CRYPT_NEWKEYSET or CRYPT_MACHINE_KEYSET or CRYPT_VERIFYCONTEXT );
WinCrypt := CryptGenRandom(hCryptProv^, (NBytes * 2), RandomBuffer^);
SetLength(ReturnString, (NBytes*2));
if WinCrypt then
begin
for i := 1 to (NBytes*2) do
begin
ReturnString[i] := Chr(RandomBuffer[i]);
end;
end else
begin
SetLength(ReturnBytes, (NBytes*2));
ReturnBytes := MTRandomBytes((NBytes*2));
end;
SetLength(Result, NBytes);
FreeMem(RandomBuffer, (NBytes * 2));
if WinCrypt then
begin
Move(ReturnString[1], Result[1], NBytes);
end else
begin
Move(ReturnBytes[0], Result[1], NBytes);
end;
end;
end.

View File

@ -1,116 +0,0 @@
{*******************************************************}
{ }
{ Renegade BBS }
{ }
{ Copyright (c) 1990-2013 The Renegade Dev Team }
{ Copyleft (ↄ) 2016 Renegade BBS }
{ }
{ This file is part of Renegade BBS }
{ }
{ Renegade 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. }
{ }
{ Renegade 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 Renegade. If not, see }
{ <http://www.gnu.org/licenses/>. }
{ }
{*******************************************************}
{ _______ __ }
{ | _ .-----.-----.-----.-----.---.-.--| .-----. }
{ |. l | -__| | -__| _ | _ | _ | -__| }
{ |. _ |_____|__|__|_____|___ |___._|_____|_____| }
{ |: | | |_____| }
{ |::.|:. | }
{ `--- ---' }
{*******************************************************}
{$mode objfpc}{$H+}
{$interfaces corba}
Unit Renegade.Random;
interface
uses
Objects,
Classes,
SysUtils,
Renegade.Random.RandomInterface,
{$IF DEFINED(LINUX)}
Renegade.Random.LinuxRandom
{$ELSEIF DEFINED(WINDOWS)}
Renegade.Random.WinRandom
{$ELSEIF DEFINED(BSD)}
Renegade.Random.BSDRandom
{$ELSE}
Renegade.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)}
FRandomGenerator := WinRandom.Create;
{$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.