获取盐添加到哈希。

原型

wp_salt( string $scheme = 'auth' )

描述

盐是使用密钥创建的。密钥位于两个位置:数据库和wp-config.php文件中。数据库中的密钥是随机生成的,并将附加到wp-config.php中的密钥。

参数

$scheme (string) (Optional) 身份验证方案(auth,secure_auth,logged_in,nonce)

返回值

(string)  盐值

源文件

路径:wp-includes/pluggable.php

<?php
...
function wp_salt( $scheme = 'auth' ) {
	static $cached_salts = array();
	if ( isset( $cached_salts[ $scheme ] ) ) {
		/**
		 * Filters the WordPress salt.
		 *
		 * @since 2.5.0
		 *
		 * @param string $cached_salt Cached salt for the given scheme.
		 * @param string $scheme      Authentication scheme. Values include 'auth',
		 *                            'secure_auth', 'logged_in', and 'nonce'.
		 */
		return apply_filters( 'salt', $cached_salts[ $scheme ], $scheme );
	}
	static $duplicated_keys;
	if ( null === $duplicated_keys ) {
		$duplicated_keys = array( 'put your unique phrase here' => true );
		foreach ( array( 'AUTH', 'SECURE_AUTH', 'LOGGED_IN', 'NONCE', 'SECRET' ) as $first ) {
			foreach ( array( 'KEY', 'SALT' ) as $second ) {
				if ( ! defined( "{$first}_{$second}" ) ) {
					continue;
				}
				$value = constant( "{$first}_{$second}" );
				$duplicated_keys[ $value ] = isset( $duplicated_keys[ $value ] );
			}
		}
	}
	$values = array(
		'key' => '',
		'salt' => ''
	);
	if ( defined( 'SECRET_KEY' ) && SECRET_KEY && empty( $duplicated_keys[ SECRET_KEY ] ) ) {
		$values['key'] = SECRET_KEY;
	}
	if ( 'auth' == $scheme && defined( 'SECRET_SALT' ) && SECRET_SALT && empty( $duplicated_keys[ SECRET_SALT ] ) ) {
		$values['salt'] = SECRET_SALT;
	}
	if ( in_array( $scheme, array( 'auth', 'secure_auth', 'logged_in', 'nonce' ) ) ) {
		foreach ( array( 'key', 'salt' ) as $type ) {
			$const = strtoupper( "{$scheme}_{$type}" );
			if ( defined( $const ) && constant( $const ) && empty( $duplicated_keys[ constant( $const ) ] ) ) {
				$values[ $type ] = constant( $const );
			} elseif ( ! $values[ $type ] ) {
				$values[ $type ] = get_site_option( "{$scheme}_{$type}" );
				if ( ! $values[ $type ] ) {
					$values[ $type ] = wp_generate_password( 64, true, true );
					update_site_option( "{$scheme}_{$type}", $values[ $type ] );
				}
			}
		}
	} else {
		if ( ! $values['key'] ) {
			$values['key'] = get_site_option( 'secret_key' );
			if ( ! $values['key'] ) {
				$values['key'] = wp_generate_password( 64, true, true );
				update_site_option( 'secret_key', $values['key'] );
			}
		}
		$values['salt'] = hash_hmac( 'md5', $scheme, $values['key'] );
	}
	$cached_salts[ $scheme ] = $values['key'] . $values['salt'];
	/** This filter is documented in wp-includes/pluggable.php */
	return apply_filters( 'salt', $cached_salts[ $scheme ], $scheme );
}
...
?>

其他

英文文档:https://developer.wordpress.org/reference/functions/wp_salt/