W
e
l
c
o
m
e
: )

PHP实现JAVA的md5算法,php验签对接java

/**
     * 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', 'd', 'e', 'f'];
        $length  = count($bytes);
        $charArr = [];
        foreach ($bytes as $value) {
            $value     = intval($value);
            $charArr[] = $LOWER[$this->uright(0xF0 & $value, 4)];
            $charArr[] = $LOWER[0x0F & $value];
        }
        return implode("", $charArr);
    }

    /** php 无符号右移 */
    public function uright($a, $n)
    {
        $c = 2147483647 >> ($n - 1);
        return $c & ($a >> $n);
    }

    /**
     * 模拟DigestUtils.md5
     * @param    [string]                   $string 加密字符
     * @return   [array]                           加密之后的byte数组
     * @author   Lerko
     * @dateTime 2018-01-25T09:28:33+0800
     */
    public function md5Hex($string)
    {
        return unpack("c*", md5($string, true));
    }

  使用方法

$md5 = $this->encodeHexString($this->md5Hex($sign_str))

  

posted @ 2022-08-02 11:53  fastKapi  阅读(781)  评论(0编辑  收藏  举报