Skip to content

Commit aec0d69

Browse files
authored
Merge pull request #75 from oittaa/patch-1
Use random_bytes if available, drop SHA-512 Merging, thanks for PR. Apologies for late merge, was on vacation.
2 parents 6a51f1c + 2f3b171 commit aec0d69

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ before_script:
3131

3232
script:
3333
- mkdir -p build/logs
34+
- if [ $(phpenv version-name) = 'hhvm' ]; then echo 'xdebug.enable=1' >> /etc/hhvm/php.ini; fi
3435
- phpunit --stderr --coverage-clover build/logs/clover.xml
3536

3637
after_script:
@@ -42,4 +43,4 @@ after_success:
4243
cache:
4344
directories:
4445
- vendor
45-
- $HOME/.cache/composer
46+
- $HOME/.cache/composer

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) {

test/csrfprotector_test.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,10 +356,12 @@ public function testGenerateAuthToken()
356356

357357
$this->assertFalse($token1 == $token2);
358358
$this->assertEquals(strlen($token1), 20);
359+
$this->assertRegExp('/^[a-z0-9]{20}$/', $token1);
359360

360361
csrfprotector::$config['tokenLength'] = 128;
361362
$token = csrfprotector::generateAuthToken();
362363
$this->assertEquals(strlen($token), 128);
364+
$this->assertRegExp('/^[a-z0-9]{128}$/', $token);
363365
}
364366

365367
/**

0 commit comments

Comments
 (0)