wp_salt

Get salt to add to hashes.

Description

(string) wp_salt( (string) $scheme = 'auth' ); 

Salts are created using secret keys. Secret keys are located in two places: in the database and in the wp-config.php file. The secret key in the database is randomly generated and will be appended to the secret keys in wp-config.php.

The secret keys in wp-config.php should be updated to strong, random keys to maximize security. Below is an example of how the secret key constants are defined. Do not paste this example directly into wp-config.php. Instead, have a just for you.

Salting passwords helps against tools which has stored hashed values of common dictionary strings. The added values makes it harder to crack.

Returns (string)

Salt value

Parameters (1)

0. $scheme — Optional. (string) => 'auth'
Authentication scheme (auth, secure_auth, logged_in, nonce)

Usage

  1. if ( !function_exists( 'wp_salt' ) ) { 
  2. require_once ABSPATH . WPINC . '/pluggable.php'; 
  3.  
  4. // Authentication scheme (auth, secure_auth, logged_in, nonce) 
  5. $scheme = 'auth'; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = wp_salt($scheme); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/pluggable.php  
  1. function wp_salt( $scheme = 'auth' ) { 
  2. static $cached_salts = array(); 
  3. if ( isset( $cached_salts[ $scheme ] ) ) { 
  4. /** 
  5. * Filters the WordPress salt. 
  6. * @since 2.5.0 
  7. * @param string $cached_salt Cached salt for the given scheme. 
  8. * @param string $scheme Authentication scheme. Values include 'auth',  
  9. * 'secure_auth', 'logged_in', and 'nonce'. 
  10. */ 
  11. return apply_filters( 'salt', $cached_salts[ $scheme ], $scheme ); 
  12.  
  13. static $duplicated_keys; 
  14. if ( null === $duplicated_keys ) { 
  15. $duplicated_keys = array( 'put your unique phrase here' => true ); 
  16. foreach ( array( 'AUTH', 'SECURE_AUTH', 'LOGGED_IN', 'NONCE', 'SECRET' ) as $first ) { 
  17. foreach ( array( 'KEY', 'SALT' ) as $second ) { 
  18. if ( ! defined( "{$first}_{$second}" ) ) { 
  19. continue; 
  20. $value = constant( "{$first}_{$second}" ); 
  21. $duplicated_keys[ $value ] = isset( $duplicated_keys[ $value ] ); 
  22.  
  23. $values = array( 
  24. 'key' => '',  
  25. 'salt' => '' 
  26. ); 
  27. if ( defined( 'SECRET_KEY' ) && SECRET_KEY && empty( $duplicated_keys[ SECRET_KEY ] ) ) { 
  28. $values['key'] = SECRET_KEY; 
  29. if ( 'auth' == $scheme && defined( 'SECRET_SALT' ) && SECRET_SALT && empty( $duplicated_keys[ SECRET_SALT ] ) ) { 
  30. $values['salt'] = SECRET_SALT; 
  31.  
  32. if ( in_array( $scheme, array( 'auth', 'secure_auth', 'logged_in', 'nonce' ) ) ) { 
  33. foreach ( array( 'key', 'salt' ) as $type ) { 
  34. $const = strtoupper( "{$scheme}_{$type}" ); 
  35. if ( defined( $const ) && constant( $const ) && empty( $duplicated_keys[ constant( $const ) ] ) ) { 
  36. $values[ $type ] = constant( $const ); 
  37. } elseif ( ! $values[ $type ] ) { 
  38. $values[ $type ] = get_site_option( "{$scheme}_{$type}" ); 
  39. if ( ! $values[ $type ] ) { 
  40. $values[ $type ] = wp_generate_password( 64, true, true ); 
  41. update_site_option( "{$scheme}_{$type}", $values[ $type ] ); 
  42. } else { 
  43. if ( ! $values['key'] ) { 
  44. $values['key'] = get_site_option( 'secret_key' ); 
  45. if ( ! $values['key'] ) { 
  46. $values['key'] = wp_generate_password( 64, true, true ); 
  47. update_site_option( 'secret_key', $values['key'] ); 
  48. $values['salt'] = hash_hmac( 'md5', $scheme, $values['key'] ); 
  49.  
  50. $cached_salts[ $scheme ] = $values['key'] . $values['salt']; 
  51.  
  52. /** This filter is documented in wp-includes/pluggable.php */ 
  53. return apply_filters( 'salt', $cached_salts[ $scheme ], $scheme );