PHP randomly generates a unique hash value custom function, Phphash Custom Function _php Tutorial

Source: Internet
Author: User
Tags sha1

PHP randomly generates a unique hash value custom function, Phphash custom function


There are many ways to get random, unique hash values on the Web, but they are similar:

1. Get a random, unique string first
2, MD5 or SHA1 to calculate the hash value

A project to use the hash value, went to search the Internet, but found that PHP has a function can directly generate a unique string--uniqid (), by using this function, coupled with their own generated random number (to prevent being cracked), more unique and difficult to guess. The main issues to consider are as follows:

1. Stochastic efficiency and randomness: Rand and Mt_rand function selection, preferred mt_rand, high efficiency, good randomness;
2, Random times: Choose 5 times, originally Unniqid is the only, plus random can just enhance security, 5 times is sufficient
3, MD5 or SHA1: can generate a unique hash value, SHA1 occupy resources may be high, but very little, if you consider the database storage of lowercase, 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 ();? >

http://www.bkjia.com/PHPjc/987256.html www.bkjia.com true http://www.bkjia.com/PHPjc/987256.html techarticle PHP randomly generates a unique hash value custom function, Phphash custom function Online There are many ways to get random unique hash value, but the same thing: 1, first get the random unique string ...

  • 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.