Hallo Freunde!
Habe hier mal schnell einen Passwortgenerator zusammenkodiert.
PHP
<?php
namespace FidgetSpinner2004\PasswordGenerator;
interface IRandomNumberGenerator
{
// inclusive min, exclusive max
public function getRandomNumberInRange($min, $max);
}
class RandRandomNumberGenerator implements IRandomNumberGenerator
{
public function getRandomNumberInRange($min, $max)
{
if($max <= $min)
{
return new InvalidArgumentException('$max must be larger than $min');
}
return $min + (rand() % $max);
}
}
interface IPasswordBuffer
{
public function appendString($string);
public function toString();
public function reset();
}
class StringPasswordBuffer implements IPasswordBuffer
{
private $buffer;
public function __construct()
{
$buffer = '';
}
public function appendString($string)
{
$this->buffer .= $string;
}
public function toString()
{
return $this->buffer;
}
public function reset()
{
$this->buffer = '';
}
}
interface IStringCollection
{
public function getStringByIndex($index);
public function addString($string);
public function addStringsFromArray(Array $stringArray);
public function getSize();
}
class ArrayStringCollection implements IStringCollection
{
private $strings;
public function __construct()
{
$this->strings = array();
}
public function getStringByIndex($index)
{
return $this->strings[$index];
}
public function addString($string)
{
array_push($this->strings, $string);
}
public function addStringsFromArray(Array $stringArray)
{
foreach($stringArray as $string)
{
$this->addString($string);
}
}
public function getSize()
{
return count($this->strings);
}
}
interface IPasswordSchema
{
public function getLength();
public function getStringCollection();
}
class StandardPasswordSchema implements IPasswordSchema
{
private $length;
private $stringCollection;
public function __construct($length, IStringCollection $stringCollection) {
$this->length = $length;
$this->stringCollection = $stringCollection;
}
public function getLength()
{
return $this->length;
}
public function getStringCollection()
{
return $this->stringCollection;
}
}
interface IPasswordGenerator
{
public function generatePassword(IPasswordBuffer $buffer, IRandomNumberGenerator $rng);
}
class StaticSchemaPasswordGenerator implements IPasswordGenerator
{
private $schema;
public function __construct(IPasswordSchema $schema)
{
$this->schema = $schema;
}
public function generatePassword(IPasswordBuffer $buffer, IRandomNumberGenerator $rng)
{
$chars = $this->schema->getStringCollection();
$len = $this->schema->getLength();
for($i = 0; $i < $len; $i++)
{
$randomString = $chars->getStringByIndex($rng->getRandomNumberInRange(0, $chars->getSize()));
$buffer->appendString($randomString);
}
}
public function getSchema()
{
return $this->schema;
}
}
?>
Alles anzeigen
Um ihn zu benutzen (Beispiel):
PHP
<?php
require('PasswordGenerator.php');
use FidgetSpinner2004\PasswordGenerator;
$strCollection = new PasswordGenerator\ArrayStringCollection();
$strCollection->addStringsFromArray(["a", "b", "c", "d", "e", "f", "0", "1", "2", "3"]);
$schema = new PasswordGenerator\StandardPasswordSchema(/* $length = */24, $strCollection);
$gen = new PasswordGenerator\StaticSchemaPasswordGenerator($schema);
$buffer = new PasswordGenerator\StringPasswordBuffer();
$rng = new PasswordGenerator\RandRandomNumberGenerator();
$gen->generatePassword($buffer, $rng);
echo $buffer->toString();
?>
Alles anzeigen
Ich wünsche euch viel Spaß damit
- fidgetspinner2004