Skip to content

Instantly share code, notes, and snippets.

@voskobovich
Last active March 20, 2018 12:26
Show Gist options
  • Save voskobovich/611e82306bab9e4223f53147456d32fc to your computer and use it in GitHub Desktop.
Save voskobovich/611e82306bab9e4223f53147456d32fc to your computer and use it in GitHub Desktop.
Randomise Tools
<?php
/**
* To run the code, use the following link:
* @link http://sandbox.onlinephpfunctions.com/code/c0c3b9e0f3df2aa3f1251594dc09282e5aabf5fc
*
* Please do not use this in PRODUCTION systems!
*/
/**
* Interface RandomNumberInterface
*/
interface RandomNumberInterface
{
/**
* @param bool $moreEntropy Increases the uniqueness of the generated value
* @return mixed
*/
public function getValue(bool $moreEntropy = false);
}
/**
* Interface RandomStringInterface
*/
interface RandomStringInterface
{
/**
* @param int $length Length of the generated string
* @param bool $moreEntropy Increases the uniqueness of the generated value
* @return mixed
*/
public function getValue(int $length = 1, bool $moreEntropy = false);
}
/**
* Interface HashableInterface
*/
interface CryptableInterface
{
/**
* @param bool $value Increases the uniqueness of the generated value
*/
public function setMoreEntropy(bool $value);
/**
* @param mixed $value Raw string
* @return string
*/
public function encrypt($value): string;
/**
* @param string $value Hashed string
* @return mixed
*/
public function decrypt(string $value);
}
/**
* Class RandomSevenBuilder
*/
class RandomSevenBuilder implements RandomNumberInterface
{
private const RANDOM_INT = 7;
private const RANDOM_INT_MIN = self::RANDOM_INT;
private const RANDOM_INT_MAX = self::RANDOM_INT;
/**
* Generate random number
* @param bool $moreEntropy
* @return int
*/
public function getValue(bool $moreEntropy = false): int
{
if (false === $moreEntropy) {
return self::RANDOM_INT;
}
try {
return random_int(self::RANDOM_INT_MIN, self::RANDOM_INT_MAX);
} catch (Exception $e) {
return self::RANDOM_INT;
}
}
}
/**
* Class RandomHBuilder
*/
class RandomHBuilder implements RandomStringInterface
{
private const LIBRARY_CHARACTER = 'H';
private const LIBRARY_CHARACTER_FIRST = 'A';
private const LIBRARY_CHARACTER_LAST = 'Z';
/**
* @var RandomNumberInterface
*/
private $randomNumber;
/**
* RandomHBuilder constructor.
* @param RandomNumberInterface $randomNumber
*/
public function __construct(RandomNumberInterface $randomNumber)
{
$this->randomNumber = $randomNumber;
}
/**
* Get character library
* @return array
*/
protected function getCharacterLibrary(): array
{
return \range(
self::LIBRARY_CHARACTER_FIRST,
self::LIBRARY_CHARACTER_LAST
);
}
/**
* Generate random character
* @param int $length Length of the generated string
* @param bool $moreEntropy Increases the uniqueness of the generated value
* @return string
*/
public function getValue(int $length = 1, bool $moreEntropy = false): string
{
if (false === $moreEntropy) {
return self::LIBRARY_CHARACTER;
}
$pos = $this->randomNumber->getValue($moreEntropy);
$library = $this->getCharacterLibrary();
if (false === empty($library[$pos])) {
return $library[$pos];
}
return self::LIBRARY_CHARACTER;
}
}
/**
* Class CryptBuilder
*/
class CryptBuilder implements CryptableInterface
{
private $moreEntropy = false;
private $randomNumber;
private $randomString;
/**
* HashBuilder constructor.
* @param RandomNumberInterface $randomNumber
* @param RandomStringInterface $randomString
*/
public function __construct(RandomNumberInterface $randomNumber, RandomStringInterface $randomString)
{
$this->randomNumber = $randomNumber;
$this->randomString = $randomString;
}
/**
* @param bool $value Increases the uniqueness of the generated value
* @return CryptBuilder
*/
public function setMoreEntropy(bool $value): CryptBuilder
{
$this->moreEntropy = $value;
return $this;
}
/**
* @param string $value Raw string
* @return string
* @throws \InvalidArgumentException
*/
public function encrypt($value): string
{
if (false === is_string($value)) {
throw new \InvalidArgumentException('The value must be a string');
}
$result = $this->randomNumber->getValue($this->moreEntropy);
$result .= $value;
$result .= $this->randomString->getValue($this->moreEntropy);
return $result;
}
/**
* @param string $value Hashed string
* @return string
*/
public function decrypt(string $value): ?string
{
return str_replace(
[
$this->randomNumber->getValue($this->moreEntropy),
$this->randomString->getValue($this->moreEntropy)
],
'',
$value
);
}
}
$moreEntropy = false;
$randomNumber = new \RandomSevenBuilder();
//echo $randomNumber->getValue($moreEntropy) . PHP_EOL;
$randomString = new \RandomHBuilder($randomNumber);
//echo $randomString->getValue($moreEntropy) . PHP_EOL;
$rawString = 'raw data';
$cryptBuilder = new \CryptBuilder($randomNumber, $randomString);
$cryptedString = $cryptBuilder->encrypt($rawString);
//echo $cryptedString . PHP_EOL;
//echo $cryptBuilder->decrypt($cryptedString) . PHP_EOL;
/**
* To run the code, use the following link:
* @link http://sandbox.onlinephpfunctions.com/code/c0c3b9e0f3df2aa3f1251594dc09282e5aabf5fc
*
* Please do not use this in PRODUCTION systems!
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment