• php 数组 暴力 JWT 密钥


    <?php
    class JwtBase {
        //头部
        private static $header=array(
            'alg'=>'HS256', //生成signature的算法
            'typ'=>'JWT'  //类型
        );
        //使用HMAC生成信息摘要时所使用的密钥
        private static $key='zhouein';
    
        /**
         * 获取jwt token
         * @return bool|string
         */
        public static function getToken(array $payload)
        {
            $arr = [
                'iss'=>'yamecent', //该JWT的签发者
                'iat'=>time(), //签发时间
                'exp'=>time()+3600*24*15, //过期时间
                'nbf'=>time(), //该时间之前不接收处理该Token
                'sub'=>'', //面向的用户
                'jti'=>md5(uniqid('JWT').time()) //该Token唯一标识
            ];
            $payload = array_merge($arr,$payload);
            if(is_array($payload))
            {
                $base64header=self::base64UrlEncode(json_encode(self::$header,JSON_UNESCAPED_UNICODE));
                $base64payload=self::base64UrlEncode(json_encode($payload,JSON_UNESCAPED_UNICODE));
                $token=$base64header.'.'.$base64payload.'.'.self::signature($base64header.'.'.$base64payload,self::$key,self::$header['alg']);
                return $token;
            }else{
                return false;
            }
        }
    
        /**
         * 验证token是否有效,默认验证exp,nbf,iat时间
         * @param string $Token 需要验证的token
         * @return bool|string
         */
        public static function verifyToken($Token,$secret)
        {
            $tokens = explode('.', $Token);
            if (count($tokens) != 3)
                return false;
    
            list($base64header, $base64payload, $sign) = $tokens;
    
            //获取jwt算法
            $base64decodeheader = json_decode(self::base64UrlDecode($base64header), JSON_OBJECT_AS_ARRAY);
            echo "验证码为:".self::signature($base64header . '.' . $base64payload, $secret, $base64decodeheader['alg']);
                ECHO "<BR>";
                echo "你输入的验证码为:".$sign;
                ECHO "<BR>";
                echo "密钥是:".$secret;
                if (empty($base64decodeheader['alg']))
                return false;
    
            //签名验证
            if (self::signature($base64header . '.' . $base64payload, $secret, $base64decodeheader['alg']) !== $sign)
                //echo "<br>密钥不正确<br>";
                return false;
            if (self::signature($base64header . '.' . $base64payload, $secret, $base64decodeheader['alg']) == $sign)
                echo "<br>密钥正确<br>";
                //return false;
    
            $payload = json_decode(self::base64UrlDecode($base64payload), JSON_OBJECT_AS_ARRAY);
    
            //签发时间大于当前服务器时间验证失败
            if (isset($payload['iat']) && $payload['iat'] > time())
                return false;
    
            //过期时间小宇当前服务器时间验证失败
            if (isset($payload['exp']) && $payload['exp'] < time())
                return false;
    
            //该nbf时间之前不接收处理该Token
            if (isset($payload['nbf']) && $payload['nbf'] > time())
                return false;
    
            return $payload;
        }
    
        /**
         * base64UrlEncode  https://jwt.io/ 中base64UrlEncode编码实现
         * @param string $input 需要编码的字符串
         * @return string
         */
        private static function base64UrlEncode($input)
        {
            return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
        }
    
        /**
         * base64UrlEncode https://jwt.io/ 中base64UrlEncode解码实现
         * @param string $input 需要解码的字符串
         * @return bool|string
         */
        private static function base64UrlDecode($input)
        {
            $remainder = strlen($input) % 4;
            if ($remainder) {
                $addlen = 4 - $remainder;
                $input .= str_repeat('=', $addlen);
            }
            return base64_decode(strtr($input, '-_', '+/'));
        }
    
        /**
         * HMACSHA256签名  https://jwt.io/ 中HMACSHA256签名实现
         * @param string $input 为base64UrlEncode(header).".".base64UrlEncode(payload)
         * @param string $key
         * @param string $alg  算法方式
         * @return mixed
         */
        private static function signature($input, $key, $alg = 'HS256')
        {
            $alg_config=array(
                'HS256'=>'sha256'
            );
            return self::base64UrlEncode(hash_hmac($alg_config[$alg], $input, $key,true));
        }
    }
    //////////////////////////////1.生成token////////////
    $token = JwtBase::getToken(['user_id'=>'0000']);
    echo $token;
    echo "<br>";
    
    /////////////////////////////2.验证//////////////////////
    $secret="zhouein";
        $data_a = JwtBase::verifyToken($token,$secret);
        var_dump($data_a);
        echo "<br>======================================<br>";
        echo "<br>======================================<br>";
    /////////////////////////////3.循环验证//////////////////////
    $zidian=array(
        'zhouein001',
        'zhouein',
        'zhouein002'
        );
    foreach($zidian as $key => $secret ){
        //echo "<br>".$key."<br>";
        //echo $secret."<br>";
        //$secret="zhouein1";
        $data_a = JwtBase::verifyToken($token,$secret);
        var_dump($data_a);
        echo "<br>============================".$key."===========================<br>";
        }
    <?php
    class JwtBase {
        //头部
        private static $header=array(
            'alg'=>'HS256', //生成signature的算法
            'typ'=>'JWT'  //类型
        );
        //使用HMAC生成信息摘要时所使用的密钥
        private static $key='zhouein';
        /**
         * 获取jwt token
         * @return bool|string
         */
        public static function getToken(array $payload){
            $arr = [
                'iss'=>'zhouein', //该JWT的签发者
                'iat'=>time(), //签发时间
                'exp'=>time()+3600*24*15, //过期时间
                'nbf'=>time(), //该时间之前不接收处理该Token
                'sub'=>'zhouein.com', //面向的用户
                'jti'=>md5(uniqid('JWT').time()) //该Token唯一标识
            ];
            $payload = array_merge($arr,$payload);
            if(is_array($payload))
            {
                $base64header=self::base64UrlEncode(json_encode(self::$header,JSON_UNESCAPED_UNICODE));
                $base64payload=self::base64UrlEncode(json_encode($payload,JSON_UNESCAPED_UNICODE));
                $token=$base64header.'.'.$base64payload.'.'.self::signature($base64header.'.'.$base64payload,self::$key,self::$header['alg']);
                return $token;
            }else{
                return false;
            }
        }
        /**
         * 验证token是否有效,默认验证exp,nbf,iat时间
         * @param string $Token 需要验证的token
         * @return bool|string
         */
        public static function verifyToken($Token,$secret){
            $tokens = explode('.', $Token);
            if (count($tokens) != 3)
                return false;
            list($base64header, $base64payload, $sign) = $tokens;
            //获取jwt算法
            $base64decodeheader = json_decode(self::base64UrlDecode($base64header), JSON_OBJECT_AS_ARRAY);
            //echo "验证码为:".self::signature($base64header . '.' . $base64payload, $secret, $base64decodeheader['alg']);
                //ECHO "<BR>";
                //echo "你输入的验证码为:".$sign;
                //ECHO "<BR>";
                //echo "密钥是:".$secret;
                if (empty($base64decodeheader['alg']))
                return false;
            //签名验证
            if (self::signature($base64header . '.' . $base64payload, $secret, $base64decodeheader['alg']) !== $sign)
                //echo "<br>密钥不正确<br>";
                return false;
            if (self::signature($base64header . '.' . $base64payload, $secret, $base64decodeheader['alg']) == $sign)
                echo "<br>密钥正确:<font size='6' color='red'>".$secret."</font><br>";
                echo "经过计算验证码为:".self::signature($base64header . '.' . $base64payload, $secret, $base64decodeheader['alg']);
                ECHO "<BR>";
                echo "你输入的验证码为:".$sign;
                ECHO "<BR>";
                //return false;
            $payload = json_decode(self::base64UrlDecode($base64payload), JSON_OBJECT_AS_ARRAY);
            //签发时间大于当前服务器时间验证失败
            if (isset($payload['iat']) && $payload['iat'] > time())
                return false;
            //过期时间小宇当前服务器时间验证失败
            if (isset($payload['exp']) && $payload['exp'] < time())
                return false;
            //该nbf时间之前不接收处理该Token
            if (isset($payload['nbf']) && $payload['nbf'] > time())
                return false;
            return $payload;
        }
        /**
         * base64UrlEncode  https://jwt.io/ 中base64UrlEncode编码实现
         * @param string $input 需要编码的字符串
         * @return string
         */
        private static function base64UrlEncode($input){
            return str_replace('=', '', strtr(base64_encode($input), '+/', '-_'));
        }
        /**
         * base64UrlEncode https://jwt.io/ 中base64UrlEncode解码实现
         * @param string $input 需要解码的字符串
         * @return bool|string
         */
        private static function base64UrlDecode($input){
            $remainder = strlen($input) % 4;
            if ($remainder) {
                $addlen = 4 - $remainder;
                $input .= str_repeat('=', $addlen);
            }
            return base64_decode(strtr($input, '-_', '+/'));
        }
        /**
         * HMACSHA256签名  https://jwt.io/ 中HMACSHA256签名实现
         * @param string $input 为base64UrlEncode(header).".".base64UrlEncode(payload)
         * @param string $key
         * @param string $alg  算法方式
         * @return mixed
         */
        private static function signature($input, $key, $alg = 'HS256'){
            $alg_config=array(
                'HS256'=>'sha256'
            );
            return self::base64UrlEncode(hash_hmac($alg_config[$alg], $input, $key,true));
        }
    }
    //////////////////////////////1.生成token////////////
    $token = JwtBase::getToken(['user_id'=>'0000']);
    echo $token;
    echo "<br>";
    /////////////////////////////2.验证//////////////////////
    //$secret="zhouein";
        //$data_a = JwtBase::verifyToken($token,$secret);
        //var_dump($data_a);
        //echo "<br>======================================<br>";
        //echo "<br>======================================<br>";
    /////////////////////////////3.循环验证//////////////////////
    $zidian=array(
        'zhouein001',
        'zhouein',
        'zhouein002',
        '%null%',
        'your-256-bit-secret'
        );
    foreach($zidian as $key => $secret ){
        //echo "<br>".$key."<br>";
        //echo $secret."<br>";
        //$secret="zhouein1";
        $token_b='eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ6aG91ZWluIiwiaWF0IjoxNjQ0NTUyNTYzLCJleHAiOjE2NDU4NDg1NjMsIm5iZiI6MTY0NDU1MjU2Mywic3ViIjoiemhvdWVpbi5jb20iLCJqdGkiOiIxZWU2YzAyMjE5NTYwYTkzNTI5YmZkYTJjNjA1Y2I4OCIsInVzZXJfaWQiOiIwMDAwIn0.FkUBz1iYmMbE9N-AvZD1fMMqeoLjh5zeWaJumAg8Wi0';
        $data_a = JwtBase::verifyToken($token_b,$secret);
        //var_dump($data_a);
        //echo "<br>============================".$key."===========================<br>";
        }
    View Code
  • 相关阅读:
    nodejs ---day01
    面向对象
    面向对象
    面向对象
    webpack
    webpack
    webpack
    模块化 (ESM) --- day02
    模块化 --- day01
    轮播图(淡入淡出切换)
  • 原文地址:https://www.cnblogs.com/zhouein/p/15882276.html
Copyright © 2020-2023  润新知