Olive.Random/Olive.Random.BSDRandom.pas

108 lines
3.8 KiB
ObjectPascal
Raw Normal View History

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+}
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.