This article mainly introduces PHP's random generation of unique HASH value user-defined functions. This article first gives the requirements, and then provides the implementation code. For more information, see
This article mainly introduces PHP's random generation of unique HASH value user-defined functions. This article first gives the requirements, and then provides the implementation code. For more information, see
There are many ways to obtain random and unique HASH values on the Internet, but they are similar:
1. Obtain random and unique strings first
2. Calculate the HASH value using MD5 or sha1.
When a project uses the hash value, it finds it online, but finds that a function in PHP can directly generate a unique string-uniqid (). By using this function, in addition, the random number generated by the user (to prevent cracking) is more unique and difficult to guess. The main considerations are as follows:
1. Random efficiency and randomness: the choice of the rand and mt_rand functions is the first choice. mt_rand features high efficiency and good randomness;
2. Random Number of times: Select 5 times. Originally, the unniqid is unique. In addition, the random number can only enhance security, and the number of times is sufficient.
3. md5 or sha1: can generate a unique hash value. sha1 may occupy a high resource, but it is very small. If you consider the lower case of database storage, you can use md5 (32-bit length)
<? Phpfunction get_hash () {$ chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789! @ # $ % ^ & * () +-'; $ Random = $ chars [mt_rand (0, 73)]. $ chars [mt_rand (0, 73)]. $ chars [mt_rand (0, 73)]. $ chars [mt_rand (0, 73)]. $ chars [mt_rand (0, 73)]; // Random 5 times $ content = uniqid (). $ random; // similar to 5443e09c27bf4aB4uT return sha1 ($ content);} echo get_hash ();?>