PHP版本解密openrtb中的价格
Decrypt Price Confirmations
原文地址 https://developers.google.com/ad-exchange/rtb/response-guide/decrypt-price?hl=zh-cn%23decryption_scheme
<?php function urlsafe_b64decode($string) { $data = str_replace(array('-','_'),array('+','/'),$string); $mod4 = strlen($data) % 4; if ($mod4) { $data .= substr('====', $mod4); } return base64_decode($data); } function decode($final_message, $e_key, $i_key) { $enc_price = urlsafe_b64decode($final_message); $iv = substr($enc_price, 0, 16); $p = substr($enc_price, 16, 8); $sig = substr($enc_price, 24, 4); $price_pad = hash_hmac("sha1", $iv, $e_key, true); $price = $p ^ $price_pad; $conf_sig = hash_hmac("sha1", $price . $iv, $i_key, true); if ($conf_sig != $sig) { return false; } return $price; } $e_key = "encrypriceencodingwhenintegratin"; $i_key = "integpriceencodingwhenintegratin"; foreach(["cHJpY2VlbmNvZGluZ3doZTzUyMb2dirzYcjALQ", "cHJpY2VlbmNvZGluZ3doZTjUy8b2dirzYXcL0Q", "cHJpY2VlbmNvZGluZ3doZTzKyOjDQx_zjiVOmw", "cHJpY2VlbmNvZGluZ3doZTrN1vbBdirz1G52-Q"] as $msg) { echo decode($msg, $e_key, $i_key) . PHP_EOL; }