騰訊app任務集市簽名 php實現代碼

來源:互聯網
上載者:User

標籤:style   class   blog   code   color   get   

 1 <?php 2  3 $appKey = "xxxxxxxxxxx";//密鑰 4 $paths = ‘GET&%2Fjob.php&‘; //假如回調檔案為根目錄下/job.php 5 $result = $_GET; 6 ksort($result);//排序 7 $step = $_GET[‘step‘];//第幾步 8 $str = ""; 9 10 $md5pkey = $_GET[‘pkey‘];//pkey11 $md5Str = md5($_GET[‘openid‘].$appKey.$_GET[‘ts‘]);//驗證pkey簽名12 13 foreach($result as $key=>$val){14   if($key != "sig"){15     $array_str = str_split($val);16     foreach($array_str as $key2=>$val2){17       if(!ereg("^([a-zA-Z0-9!*()])",$val2)){18         $array_str[$key2] = "%".strtoupper(bin2hex($val2));19       }20      }21     $val = implode(‘‘,$array_str);22     if($str == ""){23       $str = $key."=".$val;24     }else{25         $str = $str."&".$key."=".$val;26     }27   }28 }29 $str = urlencode($str);30 $sig2 = base64_encode(custom_hmac(‘sha1‘, $paths.$str, $appKey));31 32 33 if($sig2 == $_GET[‘sig‘] && $md5Str == $md5pkey){34 //驗證通過35    if($step == 1){36         //處理代碼37         $data = array("ret"=>0,"msg"=>"OK","zoneid"=>"1");38         echo mjson($data);39    }elseif($step == 2){40         //處理代碼 41         $data = array("ret"=>0,"msg"=>"OK2","zoneid"=>"1");42         echo mjson($data);43     }elseif($step == 3){44         //處理代碼 45         $data = array("ret"=>0,"msg"=>"OK3","zoneid"=>"1");46         echo mjson($data);47     }48 }else{49         //驗證不通過50         $data = array("ret"=>200,"msg"=>‘參數錯誤‘,"zoneid"=>"1");51         echo mjson($data);52 }53 54 function mjson($data)55 {56     if (!headers_sent()) header("Content-Type: application/json; charset=utf-8");57     echo json_encode($data);58 } 59 function custom_hmac($algo, $data, $key, $raw_output = true)60 {61     $key = $key."&";62     $algo = strtolower($algo);63     $pack = ‘H‘.strlen($algo(‘test‘));64     $size = 64;65     $opad = str_repeat(chr(0x5C), $size);66     $ipad = str_repeat(chr(0x36), $size);67 68     if (strlen($key) > $size) {69         $key = str_pad(pack($pack, $algo($key)), $size, chr(0x00));70     } else {71         $key = str_pad($key, $size, chr(0x00));72     }73 74     for ($i = 0; $i < strlen($key) - 1; $i++) {75         $opad[$i] = $opad[$i] ^ $key[$i];76         $ipad[$i] = $ipad[$i] ^ $key[$i];77     }78 79     $output = $algo($opad.pack($pack, $algo($ipad.$data)));80 81     return ($raw_output) ? pack($pack, $output) : $output;82 } 83 ?>

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.