php The solution to the 16-bit garbled code in md5: 1. Convert the 16-byte original binary format code to hexadecimal; 2. Take "substr(md5($str),8,16) ;" interception method solves the problem of garbled characters.
The operating environment of this article: Windows 7 system, PHP version 7.1, DELL G3 computer
php uses the md5 function to generate a 16-bit md5 value and returns garbled characters The reasons and solutions
have always been using 32-bit md5 codes. Recently, there was a demand to use 16-bit md5 codes. I checked the official PHP manual and used the following method to generate md5 codes, but the result was returned Garbled characters.
$code = md5($str,true);//$code是乱码
Looked at the manual carefully again. The description of the second parameter in the manual is: If the optional raw_output is set to TRUE, then the MD5 message digest will be a raw binary with a length of 16 bytes. format returned.
The default return value definition of the md5 function is: return the hash value in the form of a 32-character hexadecimal number.
In other words, we usually use md5 to return a hexadecimal number format with a length of 32 bytes. If the second parameter is set to true, a raw binary format with a length of 16 bytes is returned. Although it is a bit convoluted, it is obvious that the two return formats are completely different, which leads to the generation of garbled characters.
There are two solutions. One is to convert the 16-byte original binary format code into hexadecimal; the other is to intercept it, because the 8th to 32-bit md5 code of a value The 24-bit and 16-bit md5 strings are the same.
Attached is the second solution code:
$code = substr(md5($str),8,16);//$code是16位的md5码。
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to solve the problem of garbled characters when php md5 generates 16 bits. For more information, please follow other related articles on the PHP Chinese website!