Ishare encryption algorithm rules memo

Source: Internet
Author: User
Tags unique id
$salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);$fields = array(‘salt‘ => $salt,‘password‘ => $this->getPasswordEncoder()->encodePassword($password, $salt),);

1. mt_rand ();

PHP mt_rand () function PHP Math Function Definition and usage mt_rand () returns a random integer using the Mersenne Twister algorithm. The syntax mt_rand (Min, max) indicates that if the optional parameters min and Max are not provided, mt_rand () returns a pseudo-random number between 0 and rand_max. For example, if you want a random number between 5 and 15 (including 5 and 15), use mt_rand (5, 15 ). Many old libc random number generators have some uncertain and unknown features and are very slow. The rand () function of PHP uses the libc random number generator by default. The mt_rand () function is informal to replace it. This function uses known features in Mersenne Twister as a random number generator, which can generate random values at an average speed four times faster than the rand () provided by libc. Tips and notes: As of PHP 4.2.0, The srand () or mt_srand () function is no longer required for seeding the random number generator, which is now automatically completed. Note: In versions earlier than 3.0.7, Max indicates range. In these versions, we need to obtain a random number from 5 to 15, which is the same as the preceding example. The short example is mt_rand (5, 11 ). In this example, we will return some random numbers: <? Phpecho (mt_rand (); echo (mt_rand (10,100);?> Output: 315090628851328967835

2. uniqid (mt_rand (), true)

PHP uniqid () function PHP miscellaneous Function Definition and usage uniqid () function generates a unique ID based on the current time in microseconds. The parameter description prefix of the syntax uniqid (prefix, more_entropy) is optional. It is the prefix specified by ID. This parameter is useful if two scripts generate IDS exactly in the same microsecond. More_entropy is optional. Specify more entropy at the end of the returned value. If the prefix parameter is null, the returned string is 13 characters long. If the more_entropy parameter is set to true, it is a string of 23 characters. If the more_entropy parameter is set to true, an extra entropy is added at the end of the returned value (the program is generated using the combination of linear and remainder values), which improves the uniqueness of the result. The Return Value Returns a unique identifier in the form of a string. Tip and comment: because the system time is used, the ID generated by this function is not optimal. To generate an absolutely unique ID, use the MD5 () function (please refer to the string function reference ). Example <? Phpecho uniqid ();?> Output: 4415297e3af8c

 

3. sha1 (uniqid (mt_rand (), true ))

PHP sha1 () function PHP string Function Definition and usage sha1 () function calculates the SHA-1 hash of the string. The sha1 () function uses the US Secure Hash Algorithm 1. If the operation succeeds, the calculated SHA-1 hash is returned. If the operation fails, false is returned. The sha1 (string, raw) parameter description string is required. Specifies the string to be calculated. Raw is optional. Specify the hexadecimal or binary output format: True-original 20-character binary format false-default. 40 characters in hexadecimal notation: this parameter is added in PHP 5.0. Example 1 <? PHP $ STR = 'hello'; echo sha1 ($ Str);?> Output: f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0 example 2 in this example, we will output the result of sha1 () and then test it: <? PHP $ STR = 'hello'; echo sha1 ($ Str); If (sha1 ($ Str) = 'f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0 ') {echo "<br/> Hello world! "; Exit ;}?> Output: f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0hello world!

4. base_convert (sha1 (uniqid (mt_rand (), true), 16, 36)

PHP base_convert () function PHP Math Function Definition and usage the base_convert () function converts numbers between any expected hexadecimal notation. The base_convert (number, frombase, tobase) parameter description number is required. Original Value. Frombase is required. The original hexadecimal number. Tobase is required. The hexadecimal format to be converted. Returns a string containing the number in tobase format. The number is specified by frombase in hexadecimal notation. Both frombase and tobase can only be between 2 and 36 (including 2 and 36 ). Numbers higher than decimal digits are represented by the letter A-Z. For example, a represents 10, B represents 11, and Z represents 35. Example 1 converts an octal number to a decimal number: <? PHP $ Oct = "0031"; $ dec = base_convert ($ Oct, 8, 10); echo "Echo $ Oct equals to $ dec in decimal format. ";?> Output: 0031 Of The octal value is equal to 25 in decimal format. Example 2 convert the octal number to the hexadecimal number: <? PHP $ Oct = "364"; $ hex = base_convert ($ Oct, 8, 16); echo "$ $ Oct equals hex in hexadecimal format. ";?> Output: 364 of the octal value is equal to F4.

 

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.