12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- /**
- * 手机短信接口
- */
- class Test extends Api
- {
- protected $noNeedLogin = '*';
- protected $noNeedRight = '*';
- //验签
- public function verifysign(){
- //解密签名
- $sign = $this->request->request('sign','','trim');
- if(empty($sign)){
- $this->error('缺少签名');
- }
- $sign = base64_decode($sign);
- $private_key_str = config('app_rsa.private_key');
- $private_key = "-----BEGIN RSA PRIVATE KEY-----" .PHP_EOL.
- wordwrap($private_key_str, 64, PHP_EOL, true) .
- PHP_EOL."-----END RSA PRIVATE KEY-----";
- $signgetdata = ''; //被解密出来的数据
- openssl_private_decrypt($sign, $signgetdata, $private_key); // 使用私钥解密数据
- if (!$signgetdata) {
- $this->error('签名错误');
- }
- dump($signgetdata);
- //接收到的参数
- $request_all = $this->request->request();
- unset($request_all['s']);
- unset($request_all['sign']);
- ksort($request_all);
- $request_all = http_build_query($request_all);
- $request_all .= '&signkey=F_dC923_35270PdsIIUIUTRERYTYYU';
- dump($request_all);
- //作对比
- if($request_all != $signgetdata){
- $this->error('验签错误');
- }
- echo '验签正确';
- }
- //加密
- public function jiami(){
- $request_all = $this->request->request();
- unset($request_all['s']);
- // dump($request_all);
- ksort($request_all);
- // dump($request_all);
- $request_all = http_build_query($request_all);
- $request_all .= '&signkey=F_dC923_35270PdsIIUIUTRERYTYYU';
- echo $request_all;
- $public_key_str = config('app_rsa.public_key');
- $public_key = "-----BEGIN PUBLIC KEY-----" .PHP_EOL.
- wordwrap($public_key_str, 64, PHP_EOL, true) .
- PHP_EOL."-----END PUBLIC KEY-----";
- $sign = '';
- openssl_public_encrypt($request_all,$sign,$public_key);
- $sign = base64_encode($sign);
- $this->success('获得签名',$sign);
- }
- }
|