Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Ugo Sans H committed Apr 7, 2016
0 parents commit bf4b317
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/.settings
/.project
/.idea
/.buildpath
/vendor
phpunit.xml
composer.phar
composer.lock
satis.phar
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# component-random

Base definition Random string service
26 changes: 26 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "withouth/component-random",
"description": "Definition from component random string generator",
"type": "component",
"license": "MIT",
"autoload": {
"psr-4": {
"Withouth\\Component\\Random\\": "src/Withouth/Component/Random"
}
},
"authors": [
{
"name": "Ugo Sans H",
"email": "[email protected]"
}
],
"minimum-stability": "dev",
"require": {
"php" : ">=5.4"
},
"config": {
"platform": {
"php": "5.4"
}
}
}
34 changes: 34 additions & 0 deletions src/Withouth/Component/Random/Random.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace Withouth\Component\Random;

/**
* Random
*/
class Random implements RandomInterface
{
/**
* {@inheritDoc}
*/
public static function generate($length = null)
{
$bytes = false;

if (function_exists('openssl_random_pseudo_bytes') && 0 !== stripos(PHP_OS, 'win')) {
$bytes = openssl_random_pseudo_bytes(32, $strong);

if (true !== $strong) {
$bytes = false;
}
}

if (false === $bytes) {
$bytes = hash('sha256', uniqid(mt_rand(), true), true);
}

$bytes = base_convert(bin2hex($bytes), 16, 36);

return (!is_null($length)) ? substr($bytes, 0, $length) : $bytes;
}

}
17 changes: 17 additions & 0 deletions src/Withouth/Component/Random/RandomInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Withouth\Component\Random;


interface RandomInterface
{
/**
* generate random token
*
* @param integer|null $length
*
* @return string
*/
public static function generate($length = null);

}

0 comments on commit bf4b317

Please sign in to comment.