/**
* Función que se conecta al servicio web de verificación de la firma electrónica captura el evento submit y toma el
* formulario para firmar.
*/
$(function(){
$("#firmar").on("submit", function(e) {
e.preventDefault();
var f = $(this);
/** FIRMA DE PDF
* chequea si el botón radio PDF esta seleccionado y entra a la sección en caso contrario va al otro if
* (botón radio BDOC)
*/
if(document.SignFormat.optradio[0].checked) {
var formData = new FormData();
formData.append("upload", $("#file-sign")[0].files[0]);
console.log($("#file-sign")[0].files[0]);
$.ajax({
url: "https://murachi.cenditel.gob.ve/Murachi/0.1/archivos",
type: "post",
dataType: "json",
data: formData,
cache: false,
contentType: false,
xhrFields: { withCredentials: true },
processData: false,
headers: {"Authorization":"Basic YWRtaW46YWRtaW4="},
success: function(response) {
var html = manejoJsonPDF(JSON.stringify(response));
document.getElementById("respuesta").innerHTML = html;
// identificador del archivo en el servidor
var fileId = response.fileId.toString();
// seleccionar certificado del firmante
var cert;
window.hwcrypto.getCertificate({lang: "en"}).then(
function(response) {
var cert = response;
console.log(cert);
console.log(cert.hex);
var parameters = JSON.stringify({
"fileId":fileId,
"certificate":cert.hex,
"reason":"Certificado",
"location":"CENDITEL",
"contact":"582746574336",
"signatureVisible":"true"
});
// ajax para obtener la resena del pdf
$.ajax({
type: 'POST',
contentType: 'application/json',
url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/pdfs",
dataType: "json",
data: parameters,
xhrFields: { withCredentials: true },
headers: {"Authorization":"Basic YWRtaW46YWRtaW4="},
success: function(data, textStatus, jqXHR) {
var json_x = data;
console.log("***** hash data ******");
console.log(data);
console.log("***** hash data ******")
var hash = json_x['hash'];
console.log("****** json_x[hash] ******");
console.log(hash);
console.log("****** json_x[hash] *****");
var hashtype = "SHA-256";
var lang = "eng";
window.hwcrypto.sign(cert, {type: hashtype, hex: hash}, {lang: lang}).then(
function(signature) {
var firmaEnArray8 = new Uint8Array(signature.value);
var firma = buf2hex(firmaEnArray8);
console.log("******* Signature.hex *******");
console.log(signature);
console.log(signature.value);
console.log(firmaEnArray8);
console.log(firma);
console.log(signature.hex);
console.log("******* Signature.hex *******");
$.ajax({
type: 'POST',
contentType: 'application/json',
url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/pdfs/resenas",
dataType: 'json',
data: JSON.stringify({"signature":signature.hex}),
xhrFields: { withCredentials: true },
headers: {"Authorization":"Basic YWRtaW46YWRtaW4="},
success: function(data, textStatus, jqXHR){
alert('Archivo firmado correctamente: ' + data['signedFileId']);
var linkToDownload = "descargar archivo firmado";
document.getElementById("respuesta").innerHTML = linkToDownload;
},
error: function(jqXHR, textStatus, errorThrown) {
alert('error en pdfs/resenas: ' + textStatus);
$("#respuesta").html("error en pdfs/resenas: " + textStatus);
}
});
},
function(err) {
log_text("sign() failed: " + err);
var error;
log_text("sign() failed: " + err);
if(err == "Error: user_cancel") {
alert("sign() failed: El usuario cancelo la operación");
error = "El usuario cancelo la operación";
}
else if(err == "Error: no_certificates") {
alert("sign() failed: No hay certificado disponible");
error = "No hay certificado disponible";
}
else if(err == "Error: no_implementation") {
alert("sign() failed: No hay soporte para el manejo del certificado");
error = "No hay soporte para el manejo del certificado";
}
$("#respuesta").html("sign() failed: " + error);
}
);
},
error: function(jqXHR, textStatus, errorThrown) {
alert('ajax error function: ' + jqXHR.responseText);
$("#respuesta").html("error function: " + jqXHR.responseText);
}
});
},
function(err) {
log_text("getCertificate() failed: " + err);
var error;
if(err == "Error: user_cancel") {
alert("getCertificate() failed: El usuario cancelo la operación" );
error = "El usuario cancelo la operación";
}
else if(err == "Error: no_certificates") {
alert("getCertificate() failed: No hay certificado disponible") ;
error = "No hay certificado disponible";
}
else if(err == "Error: no_implementation") {
alert("getCertificate() failed: No hay soporte para el manejo del certificado");
error = "No hay soporte para el manejo del certificado";
}
$("#respuesta").html("getCertificate() failed: " + error);
}
);
},
error: function(response) {
//Que se ejecuta cuando finalice la petición de con error
$("#respuesta").html('Error...!!!');
alert("ocurrio un error")
}
});
}
/** FIRMA DE BDOC
* chequea si el botón radio BDOC esta seleccionado y entra
*/
if(document.SignFormat.optradio[1].checked) {
var formData = new FormData();
var fileInput = document.getElementById("file-sign");
var list = fileInput.files;
for (var i=0; i < list.length; i++) {
formData.append("upload", $("#file-sign")[0].files[i]);
}
$.ajax({
url: "https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/bdocs/cargas",
type: "post",
dataType: "json",
data: formData,
cache: false,
contentType: false,
xhrFields: { withCredentials: true },
processData: false,
headers: {"Authorization":"Basic YWRtaW46YWRtaW4="},
success: function(response) {
var fileId = response.containerId.toString();
var cert; // seleccionar certificado del firmante
window.hwcrypto.getCertificate({lang: "en"}).then(
function(response) {
var cert = response;
var parameters = JSON.stringify({
"fileId":fileId,
"certificate":cert.hex,
"city":"Merida",
"state":"Merida",
"postalCode":"5101",
"country":"Venezuela",
"role":"Analista",
"addSignature":true
});
// ajax para obtener la resena del bdoc
$.ajax({
type: 'POST',
contentType: 'application/json',
url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/bdocs/firmas/pre",
dataType: "json",
data: parameters,
headers: {"Authorization":"Basic YWRtaW46YWRtaW4="},
xhrFields: { withCredentials: true },
success: function(data, textStatus, jqXHR) {
var json_x = data;
var hash = json_x['hash'];
var hashtype = "SHA-256";
var lang = "eng";
window.hwcrypto.sign(cert, {type: hashtype, hex: hash}, {lang: lang}).then(
function(signature) {
$.ajax({
type: 'POST',
contentType: 'application/json',
url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/bdocs/firmas/post",
dataType: 'json',
data: JSON.stringify({"signature":signature.hex, "containerId":fileId}),
headers: {"Authorization":"Basic YWRtaW46YWRtaW4="},
xhrFields: { withCredentials: true },
success: function(data, textStatus, jqXHR) {
alert('Archivo firmado correctamente: ' + data['signedFileId']);
var linkToDownload = "descargar archivo firmado";
document.getElementById("respuesta").innerHTML = linkToDownload;
},
error: function(jqXHR, textStatus, errorThrown){
alert('error en /bdocs/resenas: ' + textStatus);
$("#respuesta").html("error en /bdocs/resenas: " + textStatus);
}
});
},
function(err) {
log_text("sign() failed: " + err);
var error;
if(err == "Error: user_cancel") {
alert("sign() failed: El usuario cancelo la operación");
error = "El usuario cancelo la operación";
}
else if(err == "Error: no_certificates") {
alert("sign() failed: No hay certificado disponible");
error = "No hay certificado disponible";
}
else if(err == "Error: no_implementation") {
alert("sign() failed: No hay soporte para el manejo del certificado");
error = "No hay soporte para el manejo del certificado";
}
$("#respuesta").html("sign() failed: " + error);
}
);
},
error: function(jqXHR, textStatus, errorThrown) {
alert('ajax error function: ' + jqXHR.responseText);
$("#respuesta").html("error function: " + jqXHR.responseText);
}
});
},
function(err) {
log_text("getCertificate() failed: " + err);
var error;
if (err == "Error: user_cancel") {
alert("getCertificate() failed: El usuario cancelo la operación");
error = "El usuario cancelo la operación";
}
else if(err == "Error: no_certificates") {
alert("getCertificate() failed: No hay certificado disponible");
error = "No hay certificado disponible";
}
else if (err == "Error: no_implementation") {
alert("getCertificate() failed: No hay soporte para el manejo del certificado");
error = "No hay soporte para el manejo del certificado";
}
$("#respuesta").html("getCertificate() failed: " + error);
}
);
},
error: function(xhr, status, error) {
// Se ejecuta cuando finalice la petición de con error
$("#respuesta").html('Error...!!!');
alert(xhr.responseText);
alert(error);
alert("ocurrio un error en cargas");
}
});
}
});
});