From 0433791bc686661877aac3d18b5d4a918e06f49b Mon Sep 17 00:00:00 2001 From: Alex Bilbie Date: Wed, 6 Aug 2014 09:29:20 +0100 Subject: [PATCH] Accidentally merged wrong version of file --- src/Util/KeyAlgorithm/DefaultAlgorithm.php | 33 ++++++++-------------- 1 file changed, 11 insertions(+), 22 deletions(-) diff --git a/src/Util/KeyAlgorithm/DefaultAlgorithm.php b/src/Util/KeyAlgorithm/DefaultAlgorithm.php index ec691c7e..fc07b0cb 100644 --- a/src/Util/KeyAlgorithm/DefaultAlgorithm.php +++ b/src/Util/KeyAlgorithm/DefaultAlgorithm.php @@ -1,6 +1,6 @@ @@ -13,34 +13,23 @@ namespace League\OAuth2\Server\Util\KeyAlgorithm; class DefaultAlgorithm implements KeyAlgorithmInterface { - protected static $algorithm; - /** * {@inheritdoc} */ 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); - /** - * @param KeyAlgorithmInterface $algorithm - */ - public static function setAlgorithm(KeyAlgorithmInterface $algorithm) - { - self::$algorithm = $algorithm; - } - - /** - * @return KeyAlgorithmInterface - */ - public static function getAlgorithm() - { - if (!self::$algorithm) { - - self::$algorithm = new DefaultAlgorithm(); + // We want to stop execution if the key fails because, well, that is bad. + if ($bytes === false || $strong === false) { + // @codeCoverageIgnoreStart + throw new \Exception('Error Generating Key'); + // @codeCoverageIgnoreEnd } - return self::$algorithm; + return substr(str_replace(array('/', '+', '='), '', base64_encode($bytes)), 0, $len); } }