PHP randomly generated unique hash value Custom Function _php Tips

Source: Internet
Author: User
Tags md5 sha1

There are a number of ways to get random, unique hash values on the web, but much the same:

1, first get the random unique string
2, Carry on MD5 or SHA1 calculate hash value

A project to use the hash value, went online to find, 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 not easy to guess solution. 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 the random can only enhance security, 5 times enough
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)

<?php
function 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.