Home  >  Article  >  Backend Development  >  How to generate random string in PHP? Use hash function

How to generate random string in PHP? Use hash function

青灯夜游
青灯夜游Original
2018-12-25 17:53:245016browse

How does PHP generate random strings? This article will introduce to you how PHP uses the rand() function and hash function to generate random strings. Let’s start with the specific content. I hope it will be helpful to you.

How to generate random string in PHP? Use hash function

There are some functions in PHP, such as md5(), sha1() and hash(), which can be used to hash strings according to certain algorithms (Hash )deal with. A hash function can take a string as an argument and return a hashed string.

First, let’s take a look at how the rand() function and the hash function generate a random string through a simple hash function.

<?php 
header("content-type:text/html;charset=utf-8"); 
echo "PHP生成随机字符串:<br><br>";
$str=rand(); 
$md5=md5($str); 
$sha1=sha1($str);
$hash=hash(&#39;gost&#39;,$str);

echo "md5()函数:<br>"; 
echo $md5; 
echo "<br><br>"; 

echo "sha1()函数<br>"; 
echo $sha1; 
echo "<br><br>"; 

echo "hash()函数<br>"; 
echo $hash; 
echo "<br><br>"; 
?>

Rendering:

How to generate random string in PHP? Use hash function

When we continuously refresh the page, the output string is different, dynamic effect:

How to generate random string in PHP? Use hash function

Let’s introduce the php functions used.

rand() function: can randomly generate an integer. It can accept two parameters (min, max) at the same time to limit the random range.

md5() function: Processes the MD5 algorithm on the string parameter and returns an MD5 hash. It has two parameters, namely

$string parameter: required value, string that needs to be calculated.

$raw parameter: optional value, defines the output format, possible values ​​are: TRUE--output original 16-character binary format; FALSE--output 32-character hexadecimal number, default value.

sha1() function: Performs the American Secure Hash algorithm 1 on the string parameter and returns a SHA-1 hash.

It also has two parameters (refer to the md5() function, the TRUE value of the $raw parameter outputs the original 20-character binary format, and the FALSE value outputs the 40-character hexadecimal number)

hash() function: Performs special algorithm processing on the string parameter and returns a hash string.

It requires up to three parameters, as follows:

$algo parameter: required value, defines the hashing algorithm that needs to be used. PHP has a total of 46 registered hash algorithms, among which "sha1", "sha256", "md5", "haval160,4" are the most popular algorithms.

$string parameter: required value, defines the string that needs to be hashed.

$getRawOutput parameter: optional parameter, defines the output format. A TRUE value indicates that the function returns a hash value in raw binary format; a FALSE value indicates that the function returns a hash value in a sequence of lowercase hexadecimal characters.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

The above is the detailed content of How to generate random string in PHP? Use hash function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn