应用场景:待签名数据以UTF-8的格式转字节流,对字节流进行MD5算法得到的签名字节流,再经过Base64转换为字符串,即生成签名。
<?php
//php之生成13位时间戳
$arr = explode(' ',microtime());
$timestamp = $arr[1].(round($arr[0]*1000));
require ('Bytes.php');
$timestamp="1566869002135";//13位时间戳
$secretKey="123456789";//秘钥
#$str = $timestamp.$secretKey;
//方式一:
$res = base64_encode(Bytes::toStr(unpack("c*", md5($str, true))));
//方式二:
$res2 = base64_encode(pack('H*', md5($str)));
echo $res;
echo "<pre>";
echo $res2;
//输出结果如下:
//DFZ16dpGQ3MQDttv2LHOmw==
//DFZ16dpGQ3MQDttv2LHOmw==
//Bytes类中toStr()方法如下
/**
* 将字节数组转化为String类型的数据
* @param $bytes 字节数组
* @param $str 目标字符串
* @return 一个String类型的数据
*/
public static function toStr($bytes)
{
$str = '';
foreach ($bytes as $ch) {
$str .= chr($ch);
}
return $str;
}
?>
PHP与JAVA相同加密结果
参考JAVA版:https://blog.csdn.net/netuser1937/article/details/100097842