Make WordPress Core


Ignore:
Timestamp:
02/17/2025 11:22:33 AM (3 months ago)
Author:
johnbillion
Message:

Security: Switch to using bcrypt for hashing user passwords and BLAKE2b for hashing application passwords and security keys.

Passwords and security keys that were saved in prior versions of WordPress will continue to work. Each user's password will be opportunistically rehashed and resaved when they next subsequently log in using a valid password.

The following new functions have been introduced:

  • wp_password_needs_rehash()
  • wp_fast_hash()
  • wp_verify_fast_hash()

The following new filters have been introduced:

  • password_needs_rehash
  • wp_hash_password_algorithm
  • wp_hash_password_options

Props ayeshrajans, bgermann, dd32, deadduck169, desrosj, haozi, harrym, iandunn, jammycakes, joehoyle, johnbillion, mbijon, mojorob, mslavco, my1xt, nacin, otto42, paragoninitiativeenterprises, paulkevan, rmccue, ryanhellyer, scribu, swalkinshaw, synchro, th23, timothyblynjacobs, tomdxw, westi, xknown.

Additional thanks go to the Roots team, Soatok, Calvin Alkan, and Raphael Ahrens.

Fixes #21022, #44628

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r59790 r59828  
    91159115    return in_array( $mime_type, $heic_mime_types, true );
    91169116}
     9117
     9118/**
     9119 * Returns a cryptographically secure hash of a message using a fast generic hash function.
     9120 *
     9121 * Use the wp_verify_fast_hash() function to verify the hash.
     9122 *
     9123 * This function does not salt the value prior to being hashed, therefore input to this function must originate from
     9124 * a random generator with sufficiently high entropy, preferably greater than 128 bits. This function is used internally
     9125 * in WordPress to hash security keys and application passwords which are generated with high entropy.
     9126 *
     9127 * Important:
     9128 *
     9129 *  - This function must not be used for hashing user-generated passwords. Use wp_hash_password() for that.
     9130 *  - This function must not be used for hashing other low-entropy input. Use wp_hash() for that.
     9131 *
     9132 * The BLAKE2b algorithm is used by Sodium to hash the message.
     9133 *
     9134 * @since 6.8.0
     9135 *
     9136 * @throws TypeError Thrown by Sodium if the message is not a string.
     9137 *
     9138 * @param string $message The message to hash.
     9139 * @return string The hash of the message.
     9140 */
     9141function wp_fast_hash(
     9142    #[\SensitiveParameter]
     9143    string $message
     9144): string {
     9145    return '$generic$' . sodium_bin2hex( sodium_crypto_generichash( $message ) );
     9146}
     9147
     9148/**
     9149 * Checks whether a plaintext message matches the hashed value. Used to verify values hashed via wp_fast_hash().
     9150 *
     9151 * The function uses Sodium to hash the message and compare it to the hashed value. If the hash is not a generic hash,
     9152 * the hash is treated as a phpass portable hash in order to provide backward compatibility for application passwords
     9153 * which were hashed using phpass prior to WordPress 6.8.0.
     9154 *
     9155 * @since 6.8.0
     9156 *
     9157 * @throws TypeError Thrown by Sodium if the message is not a string.
     9158 *
     9159 * @param string $message The plaintext message.
     9160 * @param string $hash    Hash of the message to check against.
     9161 * @return bool Whether the message matches the hashed message.
     9162 */
     9163function wp_verify_fast_hash(
     9164    #[\SensitiveParameter]
     9165    string $message,
     9166    string $hash
     9167): bool {
     9168    if ( ! str_starts_with( $hash, '$generic$' ) ) {
     9169        // Back-compat for old phpass hashes.
     9170        require_once ABSPATH . WPINC . '/class-phpass.php';
     9171        return ( new PasswordHash( 8, true ) )->CheckPassword( $message, $hash );
     9172    }
     9173
     9174    return hash_equals( $hash, wp_fast_hash( $message ) );
     9175}
Note: See TracChangeset for help on using the changeset viewer.