diff --git a/core/includes/common.inc b/core/includes/common.inc index a3823e7..e370090 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -4513,7 +4513,7 @@ function drupal_json_decode($var) { */ function drupal_get_private_key() { if (!($key = state()->get('system.private_key'))) { - $key = Crypt::randomStringHashed(); + $key = Crypt::randomStringHashed(55); state()->set('system.private_key', $key); } return $key; diff --git a/core/includes/install.core.inc b/core/includes/install.core.inc index 48a4d76..8e9e4a9 100644 --- a/core/includes/install.core.inc +++ b/core/includes/install.core.inc @@ -1140,7 +1140,7 @@ function install_settings_form_submit($form, &$form_state) { 'required' => TRUE, ); $settings['drupal_hash_salt'] = (object) array( - 'value' => Crypt::randomStringHashed(), + 'value' => Crypt::randomStringHashed(55), 'required' => TRUE, ); diff --git a/core/includes/install.inc b/core/includes/install.inc index 1731d0b..69b421a 100644 --- a/core/includes/install.inc +++ b/core/includes/install.inc @@ -446,7 +446,7 @@ function drupal_install_config_directories() { // Add a randomized config directory name to settings.php, unless it was // manually defined in the existing already. if (empty($config_directories)) { - $config_directories_hash = Crypt::randomStringHashed(); + $config_directories_hash = Crypt::randomStringHashed(55); $settings['config_directories'] = array( CONFIG_ACTIVE_DIRECTORY => array( 'path' => (object) array( diff --git a/core/includes/session.inc b/core/includes/session.inc index 69f0a81..5fc117d 100644 --- a/core/includes/session.inc +++ b/core/includes/session.inc @@ -359,7 +359,7 @@ function drupal_session_regenerate() { $old_insecure_session_id = $_COOKIE[$insecure_session_name]; } $params = session_get_cookie_params(); - $session_id = Crypt::hashBase64(uniqid(mt_rand(), TRUE) . Crypt::randomString()); + $session_id = Crypt::hashBase64(uniqid(mt_rand(), TRUE) . Crypt::randomBytes(55)); // If a session cookie lifetime is set, the session will expire // $params['lifetime'] seconds from the current request. If it is not set, // it will expire when the browser is closed. @@ -371,7 +371,7 @@ function drupal_session_regenerate() { if (drupal_session_started()) { $old_session_id = session_id(); } - session_id(Crypt::hashBase64(uniqid(mt_rand(), TRUE) . Crypt::randomString())); + session_id(Crypt::hashBase64(uniqid(mt_rand(), TRUE) . Crypt::randomBytes(55))); if (isset($old_session_id)) { $params = session_get_cookie_params(); diff --git a/core/lib/Drupal/Component/Utility/Crypt.php b/core/lib/Drupal/Component/Utility/Crypt.php index 5c7940d..d1e8db6 100644 --- a/core/lib/Drupal/Component/Utility/Crypt.php +++ b/core/lib/Drupal/Component/Utility/Crypt.php @@ -21,12 +21,12 @@ class Crypt { * source. * * @param int $count - * The number of characters (bytes) to return in the string. Defaults to 55. + * The number of characters (bytes) to return in the string. * * @return string * A randomly generated string. */ - public static function randomString($count = 55) { + public static function randomBytes($count) { static $random_state, $bytes; // Initialize on the first call. The contents of $_SERVER includes a mix of // user-specific and system information that varies a little with each page. @@ -108,15 +108,18 @@ public static function hashBase64($data) { /** * Genearates a random, base-64 encoded, URL-safe, sha-256 hashed string. * + * @param int $count + * The number of characters (bytes) of the string to be hashed. + * * @return * A base-64 encoded sha-256 hash, with + replaced with -, / with _ and * any = padding characters removed. * - * @see \Drupal\Component\Utility\Crypt::randomString() + * @see \Drupal\Component\Utility\Crypt::randomBytes() * @see \Drupal\Component\Utility\Crypt::hashBase64() */ - public static function randomStringHashed() { - return self::hashBase64(self::randomString()); + public static function randomStringHashed($count) { + return self::hashBase64(self::randomBytes($count)); } } diff --git a/core/lib/Drupal/Component/Uuid/Php.php b/core/lib/Drupal/Component/Uuid/Php.php index 47f332a..3841f36 100644 --- a/core/lib/Drupal/Component/Uuid/Php.php +++ b/core/lib/Drupal/Component/Uuid/Php.php @@ -22,7 +22,7 @@ class Php implements UuidInterface { * Implements Drupal\Component\Uuid\UuidInterface::generate(). */ public function generate() { - $hex = substr(hash('sha256', Crypt::randomString(16)), 0, 32); + $hex = substr(hash('sha256', Crypt::randomBytes(16)), 0, 32); // The field names refer to RFC 4122 section 4.1.2. $time_low = substr($hex, 0, 8); diff --git a/core/lib/Drupal/Core/Password/PhpassHashedPassword.php b/core/lib/Drupal/Core/Password/PhpassHashedPassword.php index 04b070f..e14ab4b 100644 --- a/core/lib/Drupal/Core/Password/PhpassHashedPassword.php +++ b/core/lib/Drupal/Core/Password/PhpassHashedPassword.php @@ -111,7 +111,7 @@ protected function generateSalt() { // We encode the final log2 iteration count in base 64. $output .= static::$ITOA64[$this->countLog2]; // 6 bytes is the standard salt for a portable phpass hash. - $output .= $this->base64Encode(Crypt::randomString(6), 6); + $output .= $this->base64Encode(Crypt::randomBytes(6), 6); return $output; } diff --git a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php index 134ce19..00df52d 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php +++ b/core/modules/system/lib/Drupal/system/Tests/Upgrade/UpgradePathTestBase.php @@ -53,7 +53,7 @@ protected function prepareD8Session() { // Generate and set a D7-compatible session cookie. $this->curlInitialize(); - $sid = Crypt::hashBase64(uniqid(mt_rand(), TRUE) . Crypt::randomString()); + $sid = Crypt::hashBase64(uniqid(mt_rand(), TRUE) . Crypt::randomBytes(55)); curl_setopt($this->curlHandle, CURLOPT_COOKIE, rawurlencode(session_name()) . '=' . rawurlencode($sid)); // Force our way into the session of the child site. diff --git a/core/modules/system/system.install b/core/modules/system/system.install index aebfb7b..948bfb6 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -532,7 +532,7 @@ function system_install() { ->save(); // Populate the cron key state variable. - $cron_key = Crypt::randomStringHashed(); + $cron_key = Crypt::randomStringHashed(55); state()->set('system.cron_key', $cron_key); } diff --git a/core/modules/user/user.pages.inc b/core/modules/user/user.pages.inc index 15500dc..c7e23c7 100644 --- a/core/modules/user/user.pages.inc +++ b/core/modules/user/user.pages.inc @@ -127,7 +127,7 @@ function user_pass_reset($form, &$form_state, $uid, $timestamp, $hashed_pass, $a watchdog('user', 'User %name used one-time login link at time %timestamp.', array('%name' => $account->name, '%timestamp' => $timestamp)); drupal_set_message(t('You have just used your one-time login link. It is no longer necessary to use this link to log in. Please change your password.')); // Let the user's password be changed without the current password check. - $token = Crypt::randomStringHashed(); + $token = Crypt::randomStringHashed(55); $_SESSION['pass_reset_' . $user->uid] = $token; drupal_goto('user/' . $user->uid . '/edit', array('query' => array('pass-reset-token' => $token))); } diff --git a/core/tests/Drupal/Tests/Component/Utility/CryptTest.php b/core/tests/Drupal/Tests/Component/Utility/CryptTest.php index c37a751..7eb10ce 100644 --- a/core/tests/Drupal/Tests/Component/Utility/CryptTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/CryptTest.php @@ -25,15 +25,15 @@ public static function getInfo() { } /** - * Tests \Drupal\Component\Utility\Crypt::randomString(). + * Tests \Drupal\Component\Utility\Crypt::randomBytes(). */ - public function testRandomString() { + public function testRandomBytes() { for ($i = 1; $i < 10; $i++) { $count = rand(10, 10000); // Check that different values are being generated. - $this->assertNotEquals(Crypt::randomString($count), Crypt::randomString($count)); + $this->assertNotEquals(Crypt::randomBytes($count), Crypt::randomBytes($count)); // Check the length. - $this->assertEquals(strlen(Crypt::randomString($count)), $count); + $this->assertEquals(strlen(Crypt::randomBytes($count)), $count); } }