diff --git a/tsa2.php b/tsa2.php new file mode 100644 index 0000000000000000000000000000000000000000..27ac307878fdf5f5d5d822badcd93acd1cbd53e2 --- /dev/null +++ b/tsa2.php @@ -0,0 +1,99 @@ +#!/usr/bin/env php +<?php +# +# tsa2.php en un script en php para Sellar y/o Verificar un archivo en la BFA. +# basado en tsa2.sh de Robert Martin-Legene +# Autor: Rafael Bidegain, rbidegain@loteriadelaciudad.gob.ar +# +# Licencia: GPLv2-only, 21 de mayo de 2021 +# + +function existeArchivo($file) { + return is_file($file); +} + + +function getSha($file) { + return hash_file('sha256', $file); +} + +function firmar($hash) { + $ch = curl_init(); + + curl_setopt($ch, CURLOPT_URL, "https://tsa2.buenosaires.gob.ar:443/stamp"); + #curl_setopt($ch, CURLOPT_URL, "http://tsa.ultimamilla.com.ar/stamp"); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($ch, CURLOPT_POST, 1); + curl_setopt($ch, CURLOPT_POSTFIELDS, "{\"hashes\":[\"$hash\"]}"); + + $headers = array(); + $headers[] = 'Content-Type: application/json'; + curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); + + $result = curl_exec($ch); + if (curl_errno($ch)) { + echo 'Error:' . curl_error($ch); + } + curl_close($ch); +} + +function verificar($hash){ +$ch = curl_init(); + +curl_setopt($ch, CURLOPT_URL, "https://tsa2.buenosaires.gob.ar:443/verify/$hash"); +#curl_setopt($ch, CURLOPT_URL, "http://tsa.ultimamilla.com.ar/verify/$hash"); +curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + +$result = curl_exec($ch); +if (curl_errno($ch)) { + echo 'Error:' . curl_error($ch); +} +else { + print "$result\n"; +} + +curl_close($ch); + +} + +function formaDeUso(){ + print "forma de uso:\n"; + print "./tsa2.php stamp|verify archivo\n"; +} + +#--------------------------------------------------------------------- +# Comienzo +#--------------------------------------------------------------------- +if ($argc != 3) { + print "ERROR. Se requieren dos argumentos\n\n"; + formaDeUso(); + exit(1); +} + +$archivo = $argv[2]; +if (existeArchivo($archivo) ) { + $sha = getSha($archivo); + #print "$sha\n"; + + switch ($argv[1]) { + case "stamp": + firmar($sha); + break; + case "verify": + verificar($sha); + break; + default: + print "ERROR. La acción a realizar debe ser stamp o verify\n"; + print "\"$argv[1]\" no es una acción contemplada\n\n"; + + formaDeUso(); + exit(1); + } +} +else { + print "No existe el archivo $archivo\n\n"; + formaDeUso(); + exit(1); +} + +?>