mirror of
https://github.com/elyby/oauth2-server.git
synced 2024-12-02 11:40:47 +05:30
Merge pull request #273 from sarciszewski/patch-1
Make Util/KeyAlgorithm/DefaultAlgorithm guarantee $len bytes of output even in edge cases.
This commit is contained in:
commit
0ce7ecb45a
@ -18,18 +18,18 @@ class DefaultAlgorithm implements KeyAlgorithmInterface
|
||||
*/
|
||||
public function generate($len = 40)
|
||||
{
|
||||
// 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.
|
||||
if ($bytes === false || $strong === false) {
|
||||
// @codeCoverageIgnoreStart
|
||||
throw new \Exception('Error Generating Key');
|
||||
// @codeCoverageIgnoreEnd
|
||||
}
|
||||
|
||||
return substr(str_replace(['/', '+', '='], '', base64_encode($bytes)), 0, $len);
|
||||
$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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user