This article introduces some functions related to encryption in php. if you need them, refer to it. The encryption function in php is similar to the following: 1, md5 () 2, sha1 () 3, crc32 () 4, crypt () 5, uniqid () 1. md5 () function definition and usage md5 () function calculate the MD5 hash of a string. The md5 () function uses RSA Data Security, including the MD5 packet excerpt algorithm. If the calculation succeeds, the calculated MD5 hash is returned. if the calculation fails, false is returned. Syntax md5 (string, raw) The parameter description string is required. Specifies the string to be calculated. Rawraw Optional. Specify the hexadecimal or binary output format: TRUE-original 16-character binary format FALSE-default. 32-character hexadecimal notation: this parameter is added in PHP 5.0. Example:
Output: 8b1a9953c4611296a827abf8c47804d7 2. sha1 () function: Define and use the SHA-1 hash of the string calculated by the sha1 () function. 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. Syntax: sha1 (string, raw) The parameter description string is required. Specifies the string to be calculated. Charlist is optional. Specify the hexadecimal or binary output format: TRUE-original 20-character binary format FALSE-default. 40-character hexadecimal number Note: this parameter is added in PHP 5.0. 3. crc32 () function: defines and uses the crc32 () function to calculate the crc32 polynomial of a string. This function can be used to verify data integrity. The syntax crc32 (string) string is required. Specifies the string to be calculated. This section describes the 32-bit cyclic redundancy checksum polynomial that generates the string parameter. This is usually used to check whether the transmitted data is complete. Tip and note: Because PHP integers are signed, many crc32 verification codes return negative integers, so you need to use sprintf () or printf () to obtain the string that represents the crc32 verification code. In this example, the result of crc32 () will be output without the "% u" format character (note that the result is the same ):
";echo 'With %u: ';printf("%u",$str);?> Output: Without % u: 461707669 With % u: 461707669 Example 2: In this example, the result of crc32 () is output without the "% u" format character (note that the result is different ):
";echo 'With %u: ';printf("%u",$str);?> Output: Without % u:-1959132156 With % u: 23358351404, uniqid () function: defines and uses the uniqid () function to generate a unique ID based on the current time in microseconds. The syntax uniqid (prefix, more_entropy) prefix is optional. It is the prefix specified by ID. This parameter is useful if two scripts generate IDs in the same subtle way. 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:
Output: 4415297e3af8c5, crypt () function: defines and uses the crypt () function to return strings encrypted with DES, Blowfish, or MD5. In different operating systems, the behavior of this function is different. some operating systems support more than one algorithm type. During installation, PHP checks what algorithms are available and used. The syntax crypt (str, salt) str is required. Specifies the string to be encoded. Salt Optional. It is used to increase the number of characters to be encoded to make the encoding more secure. If the salt parameter is not provided, a random value is generated each time the function is called. Tip and comment: the decryption function does not exist. The crypt () function uses a unidirectional algorithm. For example, we will test different algorithms:
";}else{echo "Standard DES not supported.\n ";}if (CRYPT_EXT_DES == 1){echo "Extended DES: ".crypt("hello world")."\n ";}else{echo "Extended DES not supported.\n ";}if (CRYPT_MD5 == 1){echo "MD5: ".crypt("hello world")."\n ";}else{echo "MD5 not supported.\n ";}if (CRYPT_BLOWFISH == 1){echo "Blowfish: ".crypt("hello world");}else{echo "Blowfish DES not supported.";}?> The output is similar (depending on the operating system): Standard DES: $1 $ r35.Y52. $ iyiFuvM. zFGsscpU0aZ4e. extended DES not supported. MD5: $1 $ BN1.0I2. $ 8oBI/4mufxK6Tq89M12mk/Blowfish DES not supported. The exact algorithm depends on the salt parameter format and length. The following are some constants used with the crypt () function. During installation, PHP sets these constants: [CRYPT_SALT_LENGTH] [CRYPT_STD_DES] [CRYPT_EXT_DES] [CRYPT_MD5] [CRYPT_BLOWFISH] |