Files
oauth2-server/src/Util/KeyAlgorithm/DefaultAlgorithm.php
Graham Campbell a1726903b5 CS fixes
2014-12-10 13:10:35 +00:00

37 lines
1.0 KiB
PHP

<?php
/**
* OAuth 2.0 Secure key interface
*
* @package league/oauth2-server
* @author Alex Bilbie <hello@alexbilbie.com>
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
* @link https://github.com/thephpleague/oauth2-server
*/
namespace League\OAuth2\Server\Util\KeyAlgorithm;
class DefaultAlgorithm implements KeyAlgorithmInterface
{
/**
* {@inheritdoc}
*/
public function generate($len = 40)
{
$stripped = '';
do {
$bytes = openssl_random_pseudo_bytes($len, $strong);
// 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
}
$stripped .= str_replace(['/', '+', '='], '', base64_encode($bytes));
} while (strlen($stripped) < $len);
return substr($stripped, 0, $len);
}
}