diff --git a/tsa2.rb b/tsa2.rb new file mode 100644 index 0000000000000000000000000000000000000000..d690438e12d80ec6a5bbf16a75308404eea38261 --- /dev/null +++ b/tsa2.rb @@ -0,0 +1,92 @@ +#!/usr/bin/env ruby +# +# tsa2.rb en un script en ruby 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, 15 de mayo de 2021 +# + +require 'digest' +require 'net/http' +require 'uri' +require 'json' + + +def existeArchivo(file) + return File.exist?(file) +end + +def getSha(file) + return Digest::SHA256.hexdigest( File.read(file) ) +end + +def sellar(sha) +# +# convertido a ruby usando: https://jhawthorn.github.io/curl-to-ruby/ +# + uri = URI.parse("https://tsa2.buenosaires.gob.ar:443/stamp") + request = Net::HTTP::Post.new(uri) + request.content_type = "application/json" + request.body = JSON.dump({ "hashes" => ["#{sha}" ] }) + + req_options = { + use_ssl: uri.scheme == "https", + } + + response = Net::HTTP.start(uri.hostname, uri.port, req_options) do |http| + http.request(request) + end +end + +def verificar(sha) + uri = URI("https://tsa2.buenosaires.gob.ar:443/verify/#{sha}"); + +# prueba con la ip de ultima milla +# uri = URI("http://201.190.184.52/verify/#{sha}"); + + response = Net::HTTP.get_response(URI(uri)) + print "#{response}\n" + print "#{response.body}\n" +end + +def formaDeUso + print "forma de uso:\n" + print "./tsa2.rb stamp|verify archivo\n" +end + + +#--------------------------------------------------------------------- +# Comienzo +#--------------------------------------------------------------------- +if ARGV.length != 2 then + print "ERROR. Se requieren dos argumentos\n\n" + formaDeUso + exit(1) +end + + +archivo = ARGV[1] +if existeArchivo(archivo) + sha = getSha(archivo) + #print "#{sha}\n" + case ARGV[0] + when "stamp" + sellar(sha) + #print "sellado\n" + when "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) + end +else + print "ERROR. No existe el archivo #{archivo}\n\n" + formaDeUso + exit(1) +end +exit(0)