<?php


class FuncionesComunes
{
    public static function generarToken()
    {
        $palabra="";
        $pwchars = "abcdefhjmnpqrstuvwxyz23456789ABCDEFGHJKLMNPQRSTUVWYXZ";
        $passwordlength = 10;
        for ( $i = 0; $i < $passwordlength; $i++)
        {
            $palabra.= substr($pwchars, rand(0,52),1);
        }
        return  $palabra;
    }

    public static function encrypt($string, $key="") {
        $key="F1S@c;Ac@;2021;";
        $result = '';
        for($i=0; $i<strlen($string); $i++) {
            $char = substr($string, $i, 1);
            $keychar = substr($key, ($i % strlen($key))-1, 1);
            $char = chr(ord($char)+ord($keychar));
            $result.=$char;
        }
        return base64_encode($result);
    }

    public static function decrypt($string, $key="") {
        $key="F1S@c;Ac@;2021;";
        $result = '';
        $string = urlencode($string);
        $string = str_replace('+','%2B',$string);
        $string = str_replace('=','%3D',$string);
        $string = str_replace('/','%2F',$string);
        $string = base64_decode(urldecode($string));
        for($i=0; $i<strlen($string); $i++) {
            $char = substr($string, $i, 1);
            $keychar = substr($key, ($i % strlen($key))-1, 1);
            $char = chr(ord($char)-ord($keychar));
            $result.=$char;
        }
        return $result;
    }
}