PHP random generation of unique HASH value user-defined function

Source: Internet
Author: User
This article mainly introduces the random generation of unique HASH value UDFs in PHP. This article first gives the requirements and then provides the implementation code, for more information about how to obtain random and unique HASH values, see the following online methods:

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 ();?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.