Skip to content
Snippets Groups Projects
Commit a3e8d2d5 authored by Rafael Bidegain's avatar Rafael Bidegain
Browse files

tsa2.pl es un script en perl fork de tsa2.sh, sella y verifica ejecutandolo desde una consola.

parent c092b520
Branches master
No related tags found
No related merge requests found
tsa2.pl 0 → 100644
#!/usr/bin/env perl
#
# tsa2.pl en un script en perl 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
#
use Digest::file qw(digest_file_hex);
use LWP::UserAgent;
use HTTP::Request ();
sub existeArchivo() {
$file = @_[0];
return (-e $file);
}
sub getSha(){
$file = @_[0];
return digest_file_hex($file, "SHA-256");
}
sub formaDeUso() {
print "forma de uso:\n";
print "./tsa2.pl stamp|verify archivo\n";
}
sub sellar(){
my $hash = @_[0];
my $uri = "https://tsa2.buenosaires.gob.ar:443/stamp";
my $json = '{"hashes":["' . $hash . '"]}';
my $req = HTTP::Request->new( 'POST', $uri );
$req->header( 'Content-Type' => 'application/json' );
$req->content( $json );
my $lwp = LWP::UserAgent->new;
$lwp->request( $req );
}
sub verificar(){
my $hash = @_[0];
my $URL = "https://tsa2.buenosaires.gob.ar:443/verify/$hash";
my $ua = LWP::UserAgent->new(ssl_opts => { verify_hostname => 1 });
my $header = HTTP::Request->new(GET => $URL);
my $request = HTTP::Request->new('GET', $URL, $header);
my $response = $ua->request($request);
if ($response->is_success){
#print "URL:$URL\nHeaders:\n";
#print $response->headers_as_string;
print $response->content,"\n";
}elsif ($response->is_error){
print "Error:$URL\n";
print $response->error_as_HTML, "\n";
}
}
#---------------------------------------------------------------------
# Comienzo
#---------------------------------------------------------------------
#print "argumentos: $#ARGV \n";
#print "$ARGV[0] \n";
#print "$ARGV[1] \n";
#print "\n------------------------\n";
if ($#ARGV != 1) {
print "ERROR. Se requieren dos argumentos\n\n";
&formaDeUso();
exit(1);
}
my $archivo = $ARGV[1];
if (&existeArchivo($archivo) ){
$sha = &getSha($archivo);
print "$sha\n";
if ($ARGV[0] eq "stamp") {
&sellar($sha);
#print "sellado\n";
}
else {
if ($ARGV[0] eq"verify") {
&verificar($sha);
#print "verificado\n";
}
else {
print "ERROR. La acción a realizar debe ser stamp o verify\n";
print "\"$ARGV[0]\" no es una acción contemplada\n\n";
formaDeUso();
exit(1);
}
}
}
else {
print "ERROR. No existe el archivo $archivo\n\n";
&formaDeUso();
exit(1);
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment