Browse Source

Use a more secure token mechanism.

Mark Scherer 9 years ago
parent
commit
b209956b8a
1 changed files with 15 additions and 1 deletions
  1. 15 1
      src/Model/Table/TokensTable.php

+ 15 - 1
src/Model/Table/TokensTable.php

@@ -196,7 +196,21 @@ class TokensTable extends Table {
 		if (empty($length)) {
 			$length = $this->defaultLength;
 		}
-		return Random::pwd($length);
+		$function = 'Random::pwd';
+		if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
+			$function = 'random_bytes';
+		} elseif (extension_loaded('openssl')) {
+			$function = 'openssl_random_pseudo_bytes';
+		} else {
+			trigger_error('Not secure', E_USER_DEPRECATED);
+			return Random::pwd($length);
+		}
+
+		$value = bin2hex($function($length / 2));
+		if (strlen($value) !== $length) {
+			$value = str_pad($value, $length, '0');
+		}
+		return $value;
 	}
 
 }