Skip to content

Commit 36c2965

Browse files
authored
Use random_bytes if available, drop SHA-512
Auth token creation order is now random_bytes -> openssl_random_pseudo_bytes -> mt_rand. SHA-512 is unnecessary as we can use bin2hex directly, but $randLength needs to be set to 64.
1 parent dc01641 commit 36c2965

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

libs/csrf/csrfprotector.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ public static function refreshToken()
354354
public static function generateAuthToken()
355355
{
356356
// todo - make this a member method / configurable
357-
$randLength = 32;
357+
$randLength = 64;
358358

359359
//if config tokenLength value is 0 or some non int
360360
if (intval(self::$config['tokenLength']) == 0) {
@@ -363,10 +363,10 @@ public static function generateAuthToken()
363363

364364
//#todo - if $length > 128 throw exception
365365

366-
if (function_exists("hash_algos")
367-
&& function_exists("openssl_random_pseudo_bytes")
368-
&& in_array("sha512", hash_algos())) {
369-
$token = hash("sha512", openssl_random_pseudo_bytes ($randLength));
366+
if (function_exists("random_bytes")) {
367+
$token = bin2hex(random_bytes($randLength));
368+
} elseif (function_exists("openssl_random_pseudo_bytes")) {
369+
$token = bin2hex(openssl_random_pseudo_bytes($randLength));
370370
} else {
371371
$token = '';
372372
for ($i = 0; $i < 128; ++$i) {

0 commit comments

Comments
 (0)