How to write JAVA DigestUtils.md5Hex in PHP

  • Some time ago, the docking interface only provided java example documents. The signature algorithm authentication is used. I am a java novice, but I have some similarities in the language to understand DigestUtils in java.md5Hex is equivalent to md5 encryption in PHP, but I discovered in the process PHP's MD5 encryption is the same as DigestUtils.md5Hex in java. If it is a simple string such as 12345, the result is the same, but the result of complex string encryption is different, so the following wording is organized.

  • The method of use is: call md5Hex($newstr); newstr returns an array for the string to be processed, newstr returns an array for the string to be processed, newstr returns an array for the string to be processed charArr
    and then calls encodeHexString($charArr) to return an encrypted array

class JavaMd5Hex{



    /**
     * 16进制转string拼接
     * @param array $bytes [description]
     * @return [type] [description]
     * @author Lerko
     * @dateTime 2018-01-25T10:18:31+0800
     */
    public function encodeHexString(array $bytes)
    {
        $LOWER = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c',        $length = count($bytes);
        $charArr = [];
        foreach ($bytes as $value) {
            $value = intval($value);
            $charArr[] = $LOWER[$this->uright(0xF0 & $value, 4)];
            $charArr[] = $LOWER[0x0F &$value]; 
    /** php unsigned right shift */
    }
        }
        return implode("", $charArr);

    public function uright($a, $n) 
    { 
        $c = 2147483647 >> ($n-1); 
        return $c & ($a >> $n); 
    } 

    /** 
     * Simulate DigestUtils.md5 
     * @param [ string] $string encrypted character 
     * @return [array] encrypted byte array 
     * @author Lerko 
     * @dateTime 2018-01-25T09:28:33+0800 
     */ 
    public function md5Hex($string) 
    { 
        return unpack("c *", md5($string, true)); 
    }}12345678910111213141516171819202122232425262728293031323334353637383940414243

Guess you like

Origin blog.csdn.net/dongle705/article/details/112969231