<?php
namespace App\Controller;
use App\Form\VerificarType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController extends AbstractController
{
/**
* Página de inicio del sistema
*
* @Route("/", name="homepage", methods={"GET"})
* @return Response
*/
public function index(): Response
{
return $this->render('default/index.html.twig');
}
/**
* Verificar cadena
*
* @Route("/common/verificar", name="verificar_consulta", methods={"GET", "POST"})
* @param Request $request
* @return Response
*/
public function verificar(Request $request): Response
{
$form = $this->createForm(VerificarType::class);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$cadena64 = $form['cadena']->getData();
// Decodificación y verificación de cadena
$cadenaHash = base64_decode($cadena64);
$comparacion = explode('^', $cadenaHash);
if (empty($comparacion[1])) {
$this->addFlash('danger', 'La cadena de verificación esta corrupta.');
return $this->renderForm('default/verificar.html.twig', [
'form' => $form
]);
}
$resultado = hash_equals(hash('sha512', $comparacion[0]), $comparacion[1]);
if ($resultado) {
$this->addFlash('success', 'La cadena de verificación es correcta.');
$data = str_replace('+', ' ', $comparacion[0]);
$data = explode('||', $data);
return $this->renderForm('default/verificar.html.twig', [
'data' => $data,
'form' => $form
]);
} else {
$this->addFlash('danger', 'La cadena de verificación presenta alteraciones.');
}
}
return $this->renderForm('default/verificar.html.twig', [
'form' => $form
]);
}
}