PHP版本实现谷歌实时竞价的价格加密
2024-04-10 本文已影响0人
elileo
/**
* 价格上报加密:
* @author Vincenzo_Jiang
* @param string $requestId 请求Id
* @param int $price 加密价格
* @param string $iKey
* @param string $eKey
* @return string
*/
function priceSecret(string $requestId ,int $price, string $iKey, string $eKey): string
{
$requestId = strlen($requestId) < 16 ? md5($requestId) : $requestId;
$iv = substr($requestId, 0, 16);
$pad = substr(hash_hmac("sha1", $iv, $eKey, true),0, 8);
$price = pack("J", str_pad($price, 8, 0, STR_PAD_LEFT));
$encPrice = $pad ^ $price;
$signature = substr(hash_hmac("sha1", $price.$iv, $iKey, true), 0, 4);
return str_replace(["/", "+"], ["_", "-"], base64_encode($iv . $encPrice . $signature));
}