Accidentally merged wrong version of file

This commit is contained in:
Alex Bilbie
2014-08-06 09:29:20 +01:00
parent 79f15f3855
commit 0433791bc6

View File

@@ -1,6 +1,6 @@
<?php <?php
/** /**
* OAuth 2.0 Secure key generator * OAuth 2.0 Secure key interface
* *
* @package league/oauth2-server * @package league/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com> * @author Alex Bilbie <hello@alexbilbie.com>
@@ -13,34 +13,23 @@ namespace League\OAuth2\Server\Util\KeyAlgorithm;
class DefaultAlgorithm implements KeyAlgorithmInterface class DefaultAlgorithm implements KeyAlgorithmInterface
{ {
protected static $algorithm;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public function generate($len = 40) public function generate($len = 40)
{ {
return self::getAlgorithm()->make($len); // We generate twice as many bytes here because we want to ensure we have
} // enough after we base64 encode it to get the length we need because we
// take out the "/", "+", and "=" characters.
$bytes = openssl_random_pseudo_bytes($len * 2, $strong);
/** // We want to stop execution if the key fails because, well, that is bad.
* @param KeyAlgorithmInterface $algorithm if ($bytes === false || $strong === false) {
*/ // @codeCoverageIgnoreStart
public static function setAlgorithm(KeyAlgorithmInterface $algorithm) throw new \Exception('Error Generating Key');
{ // @codeCoverageIgnoreEnd
self::$algorithm = $algorithm;
}
/**
* @return KeyAlgorithmInterface
*/
public static function getAlgorithm()
{
if (!self::$algorithm) {
self::$algorithm = new DefaultAlgorithm();
} }
return self::$algorithm; return substr(str_replace(array('/', '+', '='), '', base64_encode($bytes)), 0, $len);
} }
} }