[PHP高可用后端]②④--Sign机制解剖

2017-11-15  本文已影响25人  子木同
image.png image.png image.png

Aes.php

<?php
/**
 * Created by PhpStorm.
 * User: tong
 * Date: 2017/11/15
 * Time: 15:48
 */

namespace app\common\lib;

class Aes
{
    private $key = null;

    /**
     * Aes constructor.
     */
    function __construct()
    {
        $this->key = config('app.aeskey');
    }

    /**
     * 加密 客户端工程师也需要相应的加密模式和填充方式
     * @param string $input
     * @return string
     */
    public function encryt($input = '')
    {
        $size = mcrypt_get_block_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_ECB);
        $input = $this->pkcs5_pad($input, $size);
        $td = mcrypt_module_open(MCRYPT_RIJNDAEL_128, '', MCRYPT_MODE_ECB, '');
        $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
        mcrypt_generic_init($td, $this->key, $iv);

        $data = mcrypt_generic($td, $input);
        mcrypt_generic_deinit($td);
        mcrypt_module_close($td);
        $data = base64_encode($data);

        return $data;

    }

    /**
     * 填充方式 pkcs5
     * @param string $text 原始字符串
     * @param string $blocksize 加密长度
     * @return  string
     */
    private function pkcs5_pad($text, $blocksize)
    {
        $pad = $blocksize - (strlen($text) % $blocksize);
        return $text . str_repeat(chr($pad), $pad);
    }

    /**
     * 解密
     * @param string $sStr 解密的字符串
     * @return string bool|string  解密的key
     * @return string
     */
    public function decrypt($sStr)
    {
        $decrypted = mcrypt_decrypt(MCRYPT_RIJNDAEL_128,
            $this->key, base64_decode($sStr), MCRYPT_MODE_ECB);
        $dec_s = strlen($decrypted);
        $padding = ord($decrypted[$dec_s - 1]);
        $decrypted = substr($decrypted, 0, -$padding);

        return $decrypted;
    }

}

app.php

<?php
/**
 * Created by PhpStorm.
 * User: tong
 * Date: 2017/11/9
 * Time: 17:32
 */
return [
    'admin_password_pre' => '_#sing_ty',
    'aeskey' => 'sgg45747ss223455',//aes密钥,服务端和客户端必须保持一致
];

IAuth.php

<?php
/**
 * Created by PhpStorm.
 * User: tong
 * Date: 2017/11/9
 * Time: 17:35
 */

namespace app\common\lib;

class IAuth
{

    public static function setPassword($data)
    {
        return md5($data . config('app.admin_password_pre'));
    }

    /**
     * 生成每次请求的sign
     * @param array $data
     * @return string
     */
    public static function setSign($data = [])
    {
        //1.按字段排序
        ksort($data);
        //2.拼接字符串数据 &
        $string = http_build_query($data);
        //3.通过aes加密
        $string = (new Aes())->encryt($string);
        return $string;
    }

}

Common.php

<?php
/**
 * Created by PhpStorm.
 * User: tong
 * Date: 2017/11/15
 * Time: 15:33
 */

namespace app\api\controller;

use app\common\lib\Aes;
use app\common\lib\IAuth;
use think\Controller;

class Common extends Controller
{
    /**
     * 初始化的方法
     */
    protected function _initialize()
    {
        $this->checkRequestAuth();
    }

    /**
     * 检查每次app请求的数据是否合法
     */
    public function checkRequestAuth()
    {
        //首先需要获取headers
        $headers = request()->header();
        $this->testAes();
        /**
         * array (size=16)
         * 'host' => string 'singwa.com' (length=10)
         * 'connection' => string 'keep-alive' (length=10)
         * 'content-length' => string '19' (length=2)
         * 'origin' => string 'chrome-extension://fhbjgbiflinjbdggehcddcbncdddomop' (length=51)
         * 'model' => string 'sanxing5.6' (length=10)
         * 'user-agent' => string 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36' (length=115)
         * 'content-type' => string 'application/x-www-form-urlencoded' (length=33)
         * 'did' => string '231456' (length=6)
         * 'app_type' => string 'android' (length=7)
         * 'cache-control' => string 'no-cache' (length=8)
         * 'postman-token' => string '851c0def-ae2f-baa3-a2fd-c772f1d9f939' (length=36)
         * 'sign' => string 'sdjskjdskj' (length=10)
         * 'version' => string '1' (length=1)
         * 'accept' => string  (length=3)
         * 'accept-encoding' => string 'gzip, deflate' (length=13)
         * 'accept-language' => string 'zh-CN,zh;q=0.8' (length=14)
         */
        //halt($headers);

        //sign 加密需要 客户端工程师 解密:服务端工程师

    }

    public function testAes()
    {
        //$str = "id=1&ms=45&username=singwa";
        //6dDiaoQrSC2tPepBYWGFh8ri8FNeKXBwRFKbn3hv8qA=
        //echo (new Aes())->encryt($str);

        //$str = "6dDiaoQrSC2tPepBYWGFh8ri8FNeKXBwRFKbn3hv8qA=";
        //id=1&ms=45&username=singwa
        //echo (new Aes())->decrypt($str);

        $data = [
            'did'=>'12345dg',
            'version'=>1,
        ];

        //sRCvj52mZ8G+u2OdHYwmysvczmCw+RrAYWiEaXFI/5A=
        //echo IAuth::setSign($data);

        $str="sRCvj52mZ8G+u2OdHYwmysvczmCw+RrAYWiEaXFI/5A=";
        echo (new Aes())->decrypt($str);//did=12345dg&version=1

        exit;
    }
}

Test.php

<?php
/**
 * Created by PhpStorm.
 * User: tong
 * Date: 2017/11/15
 * Time: 10:23
 */

namespace app\api\controller;

use app\common\lib\exception\ApiException;

class Test extends Common
{

    public function index()
    {
        return [
            'sgsg',
            'sgsgs',
        ];
    }

    public function update($id = 0)
    {
//        return $id;//http://singwa.com/test/100

        halt(input('put.'));//body提交

    }

    /**
     * post 新增
     * @return mixed
     */
    public function save()
    {
        $data = input('post.');
        if ($data['mt'] != 1) {
            //exception('您提交的数据不合法',403);
            throw new ApiException('您提交的数据不合法~~~', 403);
        }
        //获取到提交数据 插入库
        //给客户端APP =》 接口数据

        //201 创建成功
        return show(1, 'OK', input('post.'), 201);
    }
}
image.png
上一篇下一篇

猜你喜欢

热点阅读