Skip to content

Commit

Permalink
Add "Random" for generating random strings
Browse files Browse the repository at this point in the history
  • Loading branch information
rougin committed Mar 24, 2024
1 parent f298b1f commit 02c9ce5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 78 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to `Weasley` will be documented in this file.

## [0.7.0](https://github.com/rougin/weasley/compare/v0.6.4...v0.7.0) - Unreleased

### Added
- `Random` for generating random strings in `Assorted` directory

### Changed
- Conformed HTTP middlewares from `http-interop/http-middleware` to `rougin/slytherin`'s own middleware
- Improve code quality and code formatting with `phpstan`, `php-cs-fixer`
Expand Down
48 changes: 48 additions & 0 deletions src/Assorted/Random.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace Rougin\Weasley\Assorted;

/**
* Random String
*
* @package Weasley
* @author Rougin Gutib <rougingutib@gmail.com>
*/
class Random
{
/**
* Generates a random string.
*
* @param integer $length
* @return string
*/
public static function make($length)
{
$search = array('/', '+', '=');

$string = '';

while (($len = strlen($string)) < $length)
{
$size = (int) ($length - $len);

/** @var string */
$bytes = openssl_random_pseudo_bytes($length * 2);

// Use "random_bytes" if PHP version is ~7.0 ------
if (function_exists('random_bytes'))
{
$bytes = call_user_func('random_bytes', $size);
}
// ------------------------------------------------

$bytes = base64_encode($bytes);

$text = str_replace($search, '', $bytes);

$string .= substr($text, 0, $size);
}

return $string;
}
}
47 changes: 7 additions & 40 deletions src/Packages/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@
use Rougin\Slytherin\Container\ContainerInterface;
use Rougin\Slytherin\Integration\Configuration;
use Rougin\Slytherin\Integration\IntegrationInterface;
use Rougin\Weasley\Assorted\Random;
use Rougin\Weasley\Session\FileSessionHandler;
use Rougin\Weasley\Session\Session as WeasleySession;
use Rougin\Weasley\Session\Session as Instance;

/**
* Session Package
Expand All @@ -32,14 +33,16 @@ public function define(ContainerInterface $container, Configuration $config)
/** @var string */
$name = $config->get('session.cookies', 'weasley_session');

$container->set(self::HANDLER, $handler = $this->handler($config));
$handler = $this->handler($config);

$container->set(self::HANDLER, $handler);

/** @var string|null $cookie */
$cookie = $config->get("app.http.cookies.$name", null);

if ($cookie === null)
{
$cookie = $this->random(40);
$cookie = Random::make(40);

$default = time() + 7200;

Expand All @@ -59,7 +62,7 @@ public function define(ContainerInterface $container, Configuration $config)

$handler->gc($lifetime * 60);

$session = new WeasleySession($handler, $cookie);
$session = new Instance($handler, $cookie);

return $container->set(self::SESSION, $session);
}
Expand All @@ -82,40 +85,4 @@ protected function handler(Configuration $config)

return $handlers[$driver];
}

/**
* NOTE: Should be in a single function (or class).
*
* Generates a random string.
*
* @param integer $length
* @return string
*/
protected function random($length)
{
$search = array('/', '+', '=');

$string = '';

while (($len = strlen($string)) < $length)
{
$size = (int) ($length - $len);

/** @var string */
$bytes = openssl_random_pseudo_bytes($length * 2);

if (function_exists('random_bytes'))
{
$bytes = call_user_func('random_bytes', $size);
}

$bytes = base64_encode($bytes);

$text = str_replace($search, '', $bytes);

$string .= substr($text, 0, $size);
}

return $string;
}
}
39 changes: 2 additions & 37 deletions src/Session/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Rougin\Weasley\Session;

use Rougin\Weasley\Assorted\Random;
use Rougin\Weasley\Contract\Session as Contract;

/**
Expand Down Expand Up @@ -111,7 +112,7 @@ public function regenerate($delete = false)

$serialized = serialize($this->data);

$this->id = $this->random(40);
$this->id = Random::make(40);

$this->handler->write($this->id, $serialized);

Expand All @@ -135,40 +136,4 @@ public function set($key, $value)

return $this;
}

/**
* NOTE: Should be in a single function (or class).
*
* Generates a random string.
*
* @param integer $length
* @return string
*/
protected function random($length)
{
$search = array('/', '+', '=');

$string = '';

while (($len = strlen($string)) < $length)
{
$size = (int) ($length - $len);

/** @var string */
$bytes = openssl_random_pseudo_bytes($length * 2);

if (function_exists('random_bytes'))
{
$bytes = call_user_func('random_bytes', $size);
}

$bytes = base64_encode($bytes);

$text = str_replace($search, '', $bytes);

$string .= substr($text, 0, $size);
}

return $string;
}
}
1 change: 0 additions & 1 deletion tests/Controllers/BaseControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public function testJsonMethodWithError()

/**
* Tests BaseController::toJson.
* NOTE: Must be removed in v1.0.0. Use "json" instead.
*
* @return void
*/
Expand Down

0 comments on commit 02c9ce5

Please sign in to comment.