Use random_bytes() when generating OAuth nonces.

mt_rand() is not cryptographically secure. This probably doesn't matter that much in most cases because it only affects BitBucket API interactions that already happen over HTTPS, but why not use a better option when it's available?

Closes #233
This commit is contained in:
Yahnis Elsts 2018-10-23 13:23:48 +03:00
parent f11ffce720
commit ea633a91b3
1 changed files with 13 additions and 1 deletions

View File

@ -80,7 +80,19 @@ if ( !class_exists('Puc_v4p4_OAuthSignature', false) ):
*/
private function nonce() {
$mt = microtime();
$rand = mt_rand();
$rand = null;
if ( is_callable('random_bytes') ) {
try {
$rand = random_bytes(16);
} catch (Exception $ex) {
//Fall back to mt_rand (below).
}
}
if ( $rand === null ) {
$rand = mt_rand();
}
return md5($mt . '_' . $rand);
}
}