Last active
August 29, 2015 14:02
-
-
Save jamesBan/8e99d494490ec33fe280 to your computer and use it in GitHub Desktop.
reset api check Signature
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| class Demo | |
| { | |
| /** | |
| * @var string | |
| */ | |
| public static $ACCESS_KEY = 'KUN6xYZlOAtid2MjHm90-6VFY2M7HC90ijDH4uOR'; | |
| /** | |
| * @var string | |
| */ | |
| public static $SECRET_KEY = 'D-K57TE5hPe3krexftxLWFKmL2xbQEKA-mtkrUfB'; | |
| /** | |
| * | |
| */ | |
| public function actionTest() | |
| { | |
| $result = Yii::app()->request->getParam('token', ''); | |
| if ($result) { | |
| $result = explode(':', $result); | |
| if(count($result) < 3){ | |
| echo 'invalidate request'; | |
| exit; | |
| } | |
| list($accessKey, $sign, $data) = $result; | |
| if ($this->compareSign($sign, $data)) { | |
| $data = self::deCodeData($data); | |
| if ($data['deadline'] < time()) { | |
| exit('timeout'); | |
| } | |
| unset($data['deadline']); | |
| print_r($data); | |
| } | |
| } | |
| } | |
| /** | |
| * | |
| */ | |
| public function actionReq() | |
| { | |
| $data = array( | |
| 'name' => 'zhangsan', | |
| 'age' => '25' | |
| ); | |
| echo $this->makeRequest($data); | |
| exit; | |
| } | |
| /** | |
| * @param $data | |
| * @return string | |
| */ | |
| public static function makeRequest($data) | |
| { | |
| $find = array('+', '/'); | |
| $replace = array('-', '_'); | |
| $time = time() + 3600; | |
| $data['deadline'] = $time; | |
| $data = self::enCodeData($data); | |
| $sign = hash_hmac('sha1', $data, self::$SECRET_KEY, true); | |
| return self::$ACCESS_KEY . ':' . str_replace($find, $replace, base64_encode($sign)) . ':' . $data; | |
| } | |
| /** | |
| * | |
| * 校验签名 | |
| * | |
| * @param string $sign | |
| * @param string $data | |
| * @return bollen | |
| */ | |
| public static function compareSign($sign, $data) | |
| { | |
| $replace = array('+', '/'); | |
| $find = array('-', '_'); | |
| $sign = str_replace($find, $replace, $sign); | |
| $sign = base64_decode($sign); | |
| return hash_hmac('sha1', $data, self::$SECRET_KEY, true) == $sign; | |
| } | |
| /** | |
| * @param $data | |
| * @return mixed | |
| */ | |
| public static function deCodeData($data) | |
| { | |
| $replace = array('+', '/'); | |
| $find = array('-', '_'); | |
| $data = str_replace($find, $replace, $data); | |
| $data = base64_decode($data); | |
| return json_decode($data, 1); | |
| } | |
| /** | |
| * @param $data | |
| * @return mixed|string | |
| */ | |
| public static function enCodeData($data) | |
| { | |
| $find = array('+', '/'); | |
| $replace = array('-', '_'); | |
| $data = json_encode($data); | |
| $data = str_replace($find, $replace, base64_encode($data)); | |
| return $data; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment