Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleaned up Code #111

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions PHPGangsta/GoogleAuthenticator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,40 @@ public function createSecret($secretLength = 16)
if ($secretLength < 16 || $secretLength > 128) {
throw new Exception('Bad secret length');
}
//$secret = '';
//$rnd = false;
//if (function_exists('random_bytes')) {
// $rnd = random_bytes($secretLength);
// } elseif (function_exists('mcrypt_create_iv')) {
// $rnd = mcrypt_create_iv($secretLength, MCRYPT_DEV_URANDOM);
// } elseif (function_exists('openssl_random_pseudo_bytes')) {
// $rnd = openssl_random_pseudo_bytes($secretLength, $cryptoStrong);
// if (!$cryptoStrong) {
// $rnd = false;
// }
//}
$secret = '';
$rnd = false;
if (function_exists('random_bytes')) {
$rnd = random_bytes($secretLength);
} elseif (function_exists('mcrypt_create_iv')) {
$rnd = mcrypt_create_iv($secretLength, MCRYPT_DEV_URANDOM);
} elseif (function_exists('openssl_random_pseudo_bytes')) {
$rnd = openssl_random_pseudo_bytes($secretLength, $cryptoStrong);
if (!$cryptoStrong) {
$rnd = false;
$rnd = generateRandomBytes($secretLength);

function generateRandomBytes($length) {
if (function_exists('random_bytes')) {
return random_bytes($length);
} elseif (function_exists('mcrypt_create_iv')) {
return mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
} elseif (function_exists('openssl_random_pseudo_bytes')) {
$cryptoStrong = false;
$bytes = openssl_random_pseudo_bytes($length, $cryptoStrong);
return $cryptoStrong ? $bytes : false;
} else {
return false; // No secure random function available
}
}
// Cleaned up the code to make it easier to read.
// Added a default case in the generateRanbomBytes function to return false.
// Combined the openssl_random_pseudo_bytes check and assignment into one statement.



if ($rnd !== false) {
for ($i = 0; $i < $secretLength; ++$i) {
$secret .= $validChars[ord($rnd[$i]) & 31];
Expand Down