wc_rand_hash()WC 2.4.0

Generate a rand hash.

Хуков нет.

Возвращает

Строку.

Использование

wc_rand_hash( $prefix, $max_length );
$prefix(строка)
Prefix for the hash.
По умолчанию: ''
$max_length(?int)
Maximum length of the hash. Excludes the prefix.
По умолчанию: null

Список изменений

С версии 2.4.0 Введена.

Код wc_rand_hash() WC 10.7.0

function wc_rand_hash( $prefix = '', $max_length = null ) {
	try {
		$random = bin2hex( random_bytes( 20 ) );
	} catch ( Exception $e ) {
		if ( function_exists( 'wp_fast_hash' ) ) {
			$random = bin2hex( substr( wp_fast_hash( wp_rand() ), -20 ) );
		} else {
			$random = bin2hex( substr( sha1( wp_rand() ), -20 ) );
		}
	}

	if ( $max_length && $max_length > 0 ) {
		$random = substr( $random, 0, $max_length );
	}

	return $prefix . $random;
}