<?php
 function GenerarToken($User, $Duracion = 0,$DB=null){
 
    $key = 'bp3Qt2kMf3UG';
    $header = [
        'typ' => 'JWT',
        'alg' => 'HS256'
       ];
       $header = json_encode($header);
       $header = base64_encode($header);
       if($Duracion != 0){
     
        $payload = [      
            "User" => $User,
            "Fecha"=> AddHoras($Duracion),
              ];
       }else{
        $payload = [      
            "User" => $User,
              ];
       }
       if($DB != null){
        $payload['DB'] =  $DB;
       }
       
          $payload = json_encode($payload);
          $payload = base64_encode($payload);	
          $signature = hash_hmac('sha256',$header . '.' . $payload, $key, true);
          $signature = base64_encode($signature);
          $token = "$header.$payload.$signature";
          return  $token;
   }
   function ValidarToken($Token){
  
    $secret_key = 'bp3Qt2kMf3UG';
    $jwt_values = explode('.', $Token);
    $recieved_signature = $jwt_values[2];
    //$Decoder1=  base64_decode($jwt_values[1]);
    //return $Decoder1;
   
    $recievedHeaderAndPayload = $jwt_values[0] .'.' .$jwt_values[1];
    $resultedsignature = base64_encode(hash_hmac('sha256', $recievedHeaderAndPayload, $secret_key, true));
    if($resultedsignature != $recieved_signature) {
        $recieved_signature =  str_replace('\\',"",$recieved_signature);
        
        }
      
    if($resultedsignature == $recieved_signature) {
  
        $User =  json_decode(base64_decode($jwt_values[1]));
        if(isset($User->Fecha)){
            if(Vencido($User->Fecha )== true){
              
                return FALSE;
            }else{
                return   $User->User;
            }    
        }else{
      
            return   $User->User;
        }
      
    }else{
        
        return FALSE;
    }
   }

   function OptenerDatosToken($Token) {
    $secret_key = 'bp3Qt2kMf3UG';
    $jwt_values = explode('.', $Token);
    $recieved_signature = $jwt_values[2];
    $recievedHeaderAndPayload = $jwt_values[0] .'.' .$jwt_values[1];
    $resultedsignature = base64_encode(hash_hmac('sha256', $recievedHeaderAndPayload, $secret_key, true));
    if($resultedsignature != $recieved_signature) {
        $recieved_signature =  str_replace('\\',"",$recieved_signature);
        
        }
        if($resultedsignature == $recieved_signature) {
  
            $User =  json_decode(base64_decode($jwt_values[1]));
            if(isset($User->Fecha)){
                if(Vencido($User->Fecha )== true){
                  
                    return FALSE;
                }else{
                    if (isset($User->DB)){
                        return   $User->DB ;
                    }else{
                        return   FALSE ;
                    }
                  
                }    
            }else{
          
                if (isset($User->DB)){
                    return   $User->DB ;
                }else{
                    return   FALSE ;
                }
            }
          
        }else{
            
            return FALSE;
        }
   }
?>