Cheetah
PseudoRandomStringGeneratorFactory.php
Go to the documentation of this file.
1 <?php
25 
27 use InvalidArgumentException;
28 
30 {
31  private function __construct()
32  {
33  // a factory constructor should never be invoked
34  }
35 
45  public static function createPseudoRandomStringGenerator($generator)
46  {
47  if (!$generator) {
48  return self::detectDefaultPseudoRandomStringGenerator();
49  }
50 
51  if ($generator instanceof PseudoRandomStringGeneratorInterface) {
52  return $generator;
53  }
54 
55  if ('random_bytes' === $generator) {
57  }
58  if ('mcrypt' === $generator) {
60  }
61  if ('openssl' === $generator) {
63  }
64  if ('urandom' === $generator) {
66  }
67 
68  throw new InvalidArgumentException('The pseudo random string generator must be set to "random_bytes", "mcrypt", "openssl", or "urandom", or be an instance of Facebook\PseudoRandomString\PseudoRandomStringGeneratorInterface');
69  }
70 
78  private static function detectDefaultPseudoRandomStringGenerator()
79  {
80  // Check for PHP 7's CSPRNG first to keep mcrypt deprecation messages from appearing in PHP 7.1.
81  if (function_exists('random_bytes')) {
83  }
84 
85  // Since openssl_random_pseudo_bytes() can sometimes return non-cryptographically
86  // secure pseudo-random strings (in rare cases), we check for mcrypt_create_iv() next.
87  if (function_exists('mcrypt_create_iv')) {
88  return new McryptPseudoRandomStringGenerator();
89  }
90 
91  if (function_exists('openssl_random_pseudo_bytes')) {
92  return new OpenSslPseudoRandomStringGenerator();
93  }
94 
95  if (!ini_get('open_basedir') && is_readable('/dev/urandom')) {
96  return new UrandomPseudoRandomStringGenerator();
97  }
98 
99  throw new FacebookSDKException('Unable to detect a cryptographically secure pseudo-random string generator.');
100  }
101 }
Facebook\PseudoRandomString\UrandomPseudoRandomStringGenerator
Definition: UrandomPseudoRandomStringGenerator.php:29
Facebook\PseudoRandomString\PseudoRandomStringGeneratorFactory
Definition: PseudoRandomStringGeneratorFactory.php:30
Facebook\Exceptions\FacebookSDKException
Definition: FacebookSDKException.php:32
Facebook\PseudoRandomString\PseudoRandomStringGeneratorFactory\createPseudoRandomStringGenerator
static createPseudoRandomStringGenerator($generator)
Definition: PseudoRandomStringGeneratorFactory.php:45
use
GNU LESSER GENERAL PUBLIC LICENSE February Free Software Inc Franklin Fifth MA USA Everyone is permitted to copy and distribute verbatim copies of this license but changing it is not allowed[This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it By the GNU General Public Licenses are intended to guarantee your freedom to share and change free software to make sure the software is free for all its users This the Lesser General Public applies to some specially designated software packages typically libraries of the Free Software Foundation and other authors who decide to use it You can use it but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular based on the explanations below When we speak of free we are referring to freedom of use
Definition: license.txt:27
php
Facebook\PseudoRandomString\McryptPseudoRandomStringGenerator
Definition: McryptPseudoRandomStringGenerator.php:29
Facebook\PseudoRandomString\RandomBytesPseudoRandomStringGenerator
Definition: RandomBytesPseudoRandomStringGenerator.php:29
Facebook\PseudoRandomString\PseudoRandomStringGeneratorInterface
Definition: PseudoRandomStringGeneratorInterface.php:32
Facebook\PseudoRandomString\OpenSslPseudoRandomStringGenerator
Definition: OpenSslPseudoRandomStringGenerator.php:29
Facebook\PseudoRandomString
Definition: McryptPseudoRandomStringGenerator.php:24