function FIEL() { this.login = false; this.destino = null; this.original = null; this.valor = null; this.rfc = ""; this.certificado = null; this.firmas = []; this.firms = []; this.archivos = []; } FIEL.prototype.showAuthentication = function() { this.initApplet(); //Ocupado para mostrar applet }; FIEL.prototype.setRfc = function(rfc) { this.rfc = rfc; }; /* Primer metodo que se llama al inicializar el applet Metodo llamado por el applet para enviar los valores del objeto fiel(JS) a las clases JAVA del applet(document.AppFEA) */ FIEL.prototype.initApplet = function() { /*document.AppFEA.setCadena(this.original); if (!this.login) { document.AppFEA.setRFC(this.rfc); }*/ var i = 0; for (i = 0; i < this.firmas.length; i++) { var x = ""; eval("x =" + this.firmas[i].origen + ".value"); if (x != "") { this.addFirm(this.firmas[i].origen, eval(this.firmas[i].origen + ".value"),this.firmas[i].destino); } } }; /* Llama a la funcion que muestra el applet e inicializa el certificado */ FIEL.prototype.showSigner = function(certificado) { this.showAuthentication(); this.certificado = certificado; }; /* Una vez que el applet regresa con las firmas se asignan con este metodo */ function setSello(campoDestino, sello, campoOrigen, original) { eval(campoDestino + ".value= sello;"); eval(campoOrigen + ".value= original;"); } /* Agrega de firmas de una en una */ FIEL.prototype.addFirma = function(origen, destino) { this.firmas[this.firmas.length] = new Firma(origen, destino); }; function Firma(origen, destino) { this.origen = origen; this.destino = destino; } FIEL.prototype.addFirm = function(origen, valor, destino) { this.firms[this.firms.length] = new Firm(origen, valor, destino); }; function Firm(origen, valor, destino) { this.origen = origen; this.valor = valor; this.destino = destino; } /* Si el applet regresa correctamente la FIEL regresa un true y se hace el submit */ FIEL.prototype.sign = function(datas) { var val=false; datas.push(this.rfc); $.ajax({ type : 'POST', contentType : 'application/x-www-form-urlencoded; charset=utf-8', url : context+"verificarfirmas=", async : false, data :{ 'JSONfirmas' : "{firmas : " + JSON.stringify(this.firms) + "}", 'data.datas' : datas }, success : function( result ){ if (result != undefined && result != null){ result = eval(result); if (result != undefined && result != null){ val=true; for(var i=0;i'+mensajeError+'

'); $('#errorFiel').css('display','block'); $('#errorFiel').fadeOut(5000); } } else { inhabilitarBoton(false); BANDERA_ERROR_FIRMA = true; mensajeError = 'Ocurri\u00F3 un error al firmar la cadena: \n' + PKI.SAT.FielUtil.obtenMensajeError(error_code); if($('#errorFiel').val() != undefined){ $('#errorFiel').html('
'+mensajeError+'

'); $('#errorFiel').css('display','block'); $('#errorFiel').fadeOut(5000); }else{ jqAlert('Ocurri\u00F3 un error al firmar la cadena: \n' + PKI.SAT.FielUtil.obtenMensajeError(error_code)); } } } ); } else if (compatibilidad === false) { jqAlert('Navegador NO es compatible para realizar firmado'); } else { jqAlert('Ocurri\u00F3 un error al validar el navegador: \n' + PKI.SAT.FielUtil.obtenMensajeError(compatibilidad)); } } function procesarFirmaFormulario(event) { if(BANDERA_ERROR_FIRMA) { event.preventDefault(); }else{ TokenPEM.init(); document.getElementById("privateKeyPassword").value = ''; document.getElementById("certificate").value = null; document.getElementById("privateKey").value = null; } } function inhabilitarBoton(valor){ //$( "#btnSubmit" ).prop( "disabled", valor ); //setTimeout(function(){$('#'+boton).prop( "disabled", valor )}, 10); } function isBlank(str) { return (!str || /^\s*$/.test(str)); } function terminarFirmado(tipoFirma){ var datas = []; datas.push(TokenPEM.config.data1); datas.push(TokenPEM.config.data2); datas.push(TokenPEM.config.data3); datas.push(TokenPEM.config.data4); $.ajax({ type : 'POST', url : context+"verificar=", async : false, data :{ 'data.datas' : datas }, success : function( result ){ var error=true; if (result != undefined && result != null){ result = eval(result); if (result != undefined && result != null && result.rfc != undefined && result.rfc != null){ error=false; if(tipoFirma == 1){ document.login.elements["fiel.rfc"].value = result.rfc; document.login.elements["fiel.firma"].value = result.firma; document.login.elements["fiel.original"].value = result.original; document.login.elements["fiel.serie"].value = result.serie; document.login.elements["fiel.certificado"].value = result.certificado; document.login.submit(); }else if(tipoFirma == 0){ document.firmado.elements["firma.certificado"].value = result.certificado; terminaFirmado(datas); }else{ registrarFiel(result); } TokenPEM.init(); } } if (error==true){ jqAlert('Ocurri\u00F3 un error al firmar'); } }, cache : false }); } function actualizarEntrada(idElement){ $.ajaxFileUpload({url : context+idElement+"=",secureuri : false, async : false,fileElementId : idElement,success : function(data) {},error : function(data, status, e) {jqAlert('Error al cargar el archivo');}}); } function getLlaveDesdeCertificado(fileLlavePrivada) { //////////////////////////////////////// var readerPrivateKey = new FileReader(); readerPrivateKey.onload = function(e) { //Convierte a un arreglo de enteros sin signo var bytes = new Uint8Array(readerPrivateKey.result); var binary = ""; for (var i = 0; i < bytes.byteLength; i++) { //Convierte un numero Unicode a su correspondiente caracter binary += String.fromCharCode(bytes[i]); } //Se convierte binario a hex var hex = rstrtohex(binary); TokenPEM.initData2(hex); };//fin reader.onload readerPrivateKey.readAsArrayBuffer(fileLlavePrivada.files[0]); /////////////////////////////////////// } function getLlavePrivada(event){ //////////////////////////////////////// var privateKey = event.target; var readerPrivateKey = new FileReader(); readerPrivateKey.onload = function(e) { //Convierte a un arreglo de enteros sin signo var bytes = new Uint8Array(readerPrivateKey.result); var binary = ""; for (var i = 0; i < bytes.byteLength; i++) { //Convierte un numero Unicode a su correspondiente caracter binary += String.fromCharCode(bytes[i]); } //Se convierte binario a hex var hex = rstrtohex(binary); TokenPEM.init(); TokenPEM.initData2(hex); };//fin reader.onload readerPrivateKey.readAsArrayBuffer(privateKey.files[0]); /////////////////////////////////////// } var TokenPEM = { config : { data1 : '', data2 : '', data3 : '', data4 : '' }, init : function() { this.config.data1 = ''; this.config.data2 = ''; this.config.data3 = ''; this.config.data4 = ''; }, initData1 : function(data) { this.config.data1 = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(data)); }, initData2 : function(data) { this.config.data2 = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(data)); }, initData3 : function(data) { this.config.data3 = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(data)); }, initData4 : function(data) { this.config.data4 = CryptoJS.enc.Base64.stringify(CryptoJS.enc.Utf8.parse(data.replace(/ /g,""))); }, }; $( document ).ready(function() { if(document.getElementById('privateKey') != null && document.getElementById('certificate') != null){ document.getElementById('privateKey').value = ''; document.getElementById('certificate').value = ''; document.getElementById('privateKey').addEventListener('change', getLlavePrivada, false); } });