Index: pruebas_web_crypto_api/testingDigitalSignatureWebCryptography.html
===================================================================
--- pruebas_web_crypto_api/testingDigitalSignatureWebCryptography.html	(revision 9bc9e0d744eedc64916a70c7037a2d16af186e14)
+++ pruebas_web_crypto_api/testingDigitalSignatureWebCryptography.html	(revision 9bc9e0d744eedc64916a70c7037a2d16af186e14)
@@ -0,0 +1,518 @@
+<!DOCTYPE html>
+<html lang="es">
+<head>
+  <meta charset="UTF-8">
+  <title>Firma electrónica con Web Crypto API</title>
+<body>
+
+  <main>
+    <form enctype="multipart/form-data" method="post" id="firmar" name="SignFormat">
+      <h1>Firmar un PDF con un pkcs12 y Murachí REST</h1>
+      <p>Seleccione el archivo que va a firmar electrónicamente</p>
+      <input id="file-sign" class="file" type="file" data-min-file-count="1" name="upload" accept=".pdf" >
+      <input id="inputCertificadoPKCS12" type="file" name="inputinputCertificadoPKCS12" />
+      <button type="button" name="buttonsubmit3" id="buttonsubmit3">Firmar</button>
+    </form>
+    <section id="resultados">
+      <div id="seccion1"></div>
+      <div id="seccion2"></div>
+      <div id="seccion3"></div>
+      <div id="seccion4"></div>
+    </section>
+  </main>
+
+  <!-- Forge 0.7.0 -->
+  <script src="./forge.min.js"></script>
+  <script src="./jquery.min.js"></script>
+  <script>
+
+  // Variables globales
+  var
+      reader = new FileReader(),
+      certificado = "",
+      password = "123456",
+      inputCertificadoPKCS12 = document.querySelector("#inputCertificadoPKCS12"),
+      buttonsubmit3 = document.querySelector("#buttonsubmit3"),
+      decodificado = null,
+      certP12bag = null,
+      certificadoHexadecimal = null,
+      privateKeyRSASSAPKCS1v1_5 = null,
+      privateKeyRSAPSS = null,
+      publicKeyRSAPSS = null,
+      importarLlave = null;
+
+
+
+  /* ----- Funciones ----- */
+    // Función para convertir el certificado de ArrayBuffer a String
+    function convertirArrayBufferAString ( buffer ) {
+      let _certificadoEnString = '';      
+      let _bytes = new Uint8Array( buffer );
+      let _longitudDeCadena = _bytes.byteLength;
+      for (let i = 0; i < _longitudDeCadena; i++) {
+        _certificadoEnString += String.fromCharCode( _bytes[ i ] );
+      }
+      return _certificadoEnString;
+    }
+
+    // Función para convertir el certificado de String a ArrayBuffer 
+    function convertirStringArrayBuffer ( data ) {
+      let _arrayBuffer = new ArrayBuffer ( data.length );
+      let writer = new Uint8Array ( _arrayBuffer );
+      for (let i = 0, len = data.length; i < len; i++) {
+        writer[i] = data.charCodeAt ( i );
+      }
+      return _arrayBuffer;
+    }
+
+    // Función para decodificar el .p12 convertido en string
+    function decodificarPKCS12 ( stringBase64 ) {
+      let pkcs12Asn1 = forge.asn1.fromDer( stringBase64 );
+      let pkcs12 = forge.pkcs12.pkcs12FromAsn1( pkcs12Asn1, false, password );
+      return pkcs12;
+    }
+
+     // Función para buscar la llave publica del PKCS#12
+    function buscarLlavePublica ( pkcs12 ) {
+      for ( var sci = 0; sci < pkcs12.safeContents.length; ++sci ) {
+        var safeContents = pkcs12.safeContents[ sci ];
+
+        for ( var sbi = 0; sbi < safeContents.safeBags.length; ++sbi ) {
+          var safeBag = safeContents.safeBags[sbi];
+          if (safeBag.cert != undefined) {
+            console.log("antes de llamar safeBag.cert.publicKey")
+            var publicKey = safeBag.cert.publicKey;
+            console.log("**** publickey ****");
+            console.log(publicKey);
+            console.log("**** publickey ****");
+            var publickeyPem = forge.pki.publicKeyToPem(publicKey);
+            console.log("**** publickeyPem ****");
+            console.log(publickeyPem);
+            console.log("**** publickeyPem ****");
+            var publickeyBytesHex = forge.util.bytesToHex(publickeyPem);
+            console.log("**** publickeyBytesHex ****");
+            console.log(publickeyBytesHex);
+            console.log("**** publickeyBytesHex ****");
+            return publicKey;
+            }
+        }
+      }
+    }
+
+    // Función para buscar la llave privada del PKCS#12
+    function buscarLlavePrivada ( pkcs12 ) {
+      for ( var sci = 0; sci < pkcs12.safeContents.length; ++sci ) {
+        var safeContents = pkcs12.safeContents[ sci ];
+
+        for ( var sbi = 0; sbi < safeContents.safeBags.length; ++sbi ) {
+          var safeBag = safeContents.safeBags[sbi];
+          //if (safeBag.cert != undefined) {
+            //console.log("antes de llamar safeBag.cert.publicKey")
+            //var publicKey = safeBag.cert.publicKey;
+            //console.log("**** publickey ****");
+            //console.log(publicKey);
+            //console.log("**** publickey ****");
+            //}
+
+          if ( safeBag.type === forge.pki.oids.keyBag ) {
+            privateKey = safeBag.key;
+          } else if ( safeBag.type === forge.pki.oids.pkcs8ShroudedKeyBag ) {
+            privateKey = safeBag.key;
+          }
+        }
+      }
+      return privateKey;
+    }
+    //var rsaPublicKey = pki.publicKeyToAsn1(publicKey);
+
+    // Función para convertir la llave publica a PKCS#8
+    function publicKeyToPkcs8 ( publicKeyP12 ) {
+      var rsaPublicKey = forge.pki.publicKeyToAsn1(publicKeyP12);
+      //var publicKeyInfo = forge.pki.wrapRsaPrivateKey(rsaPublicKey);
+      var publicKeyInfoDer = forge.asn1.toDer(rsaPublicKey).getBytes();
+      var publicKeyInfoDerBuff = convertirStringArrayBuffer(publicKeyInfoDer);
+      return publicKeyInfoDerBuff;
+    }
+
+    // Función para convertir la llave privada a PKCS#8
+    function privateKeyToPkcs8 ( privateKeyP12 ) {
+        console.log("function privateKeyToPkcs8 ( privateKeyP12 ) {");
+        console.log(privateKeyP12);
+        console.log("function privateKeyToPkcs8 ( privateKeyP12 ) {");
+      var rsaPrivateKey = forge.pki.privateKeyToAsn1(privateKeyP12);
+      var privateKeyInfo = forge.pki.wrapRsaPrivateKey(rsaPrivateKey);
+      var privateKeyInfoDer = forge.asn1.toDer(privateKeyInfo).getBytes();
+      var privateKeyInfoDerBuff = convertirStringArrayBuffer(privateKeyInfoDer);
+      return privateKeyInfoDerBuff;
+    }
+
+    // Función para convertir un Array Buffer a Hexadecimal
+    function buf2hex ( buffer ) {
+      return Array.prototype.map.call(new Uint8Array(buffer), x => ('00' + x.toString(16)).slice(-2)).join('');
+    }
+
+    // Función para importar la llave privada with RSASSA-PKCS1-v1_5
+    /*
+    function importarLlavePrivada ( llavePrivada ) {
+      var qweasd = crypto.subtle.importKey(
+        "pkcs8", //can be "jwk" (public or private), "spki" (public only), or "pkcs8" (private only)
+        llavePrivada,
+        {   //these are the algorithm options
+          name: "RSASSA-PKCS1-v1_5",
+          hash: { name: "SHA-256" }, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
+        },
+        false, //whether the key is extractable (i.e. can be used in exportKey)
+        ["sign"] //"verify" for public key import, "sign" for private key imports
+      ).then(function(publicKey){
+        //returns a publicKey (or privateKey if you are importing a private key)
+        console.log(publicKey);
+        privateKeyRSASSAPKCS1v1_5 = publicKey;
+      })
+      .catch(function(err){
+        console.error(err);
+      });
+    }
+
+
+    */
+    // Función para importar la llave privada with RSA-PSS
+    function importarLlavePrivada ( llavePrivada ) {
+        console.log("Clave privada");
+        console.log(llavePrivada);
+        console.log("Clave privada");
+      var qweasd = crypto.subtle.importKey(
+        "pkcs8", //can be "jwk" (public or private), "spki" (public only), or "pkcs8" (private only)
+        llavePrivada,
+        {   //these are the algorithm options
+          name: "RSA-PSS",
+          hash: { name: "SHA-256" }, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
+        },
+        false, //whether the key is extractable (i.e. can be used in exportKey)
+        ["sign"] //"verify" for public key import, "sign" for private key imports
+      ).then(function(publicKey){
+        //returns a publicKey (or privateKey if you are importing a private key)
+        console.log(publicKey);
+        privateKeyRSAPSS = publicKey;
+      })
+      .catch(function(err){
+        console.error(err);
+      });
+    }
+
+// Función para importar la llave publica with RSA-PSS
+    function importarLlavePublica ( llavePublica ) {
+        console.log("importarLlavePublica llavePublica");
+        console.log(llavePublica);
+        console.log("importarLlavePublica llavePublica");
+      var qweasd = crypto.subtle.importKey(
+        "spki", //can be "jwk" (public or private), "spki" (public only), or "pkcs8" (private only)
+        llavePublica,
+        {   //these are the algorithm options
+          name: "RSA-PSS",
+          hash: { name: "SHA-256" }, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
+        },
+        false, //whether the key is extractable (i.e. can be used in exportKey)
+        ["verify"] //"verify" for public key import, "sign" for private key imports
+      ).then(function(publicKey){
+        //returns a publicKey (or privateKey if you are importing a private key)}
+        console.log("llavePublica importada");
+        console.log(publicKey);
+        console.log("llavePublica importada");
+        publicKeyRSAPSS = publicKey;
+      })
+      .catch(function(err){
+        console.error(err);
+      });
+    }
+    // Función para convertir el certificado .p12 en hexadecimal
+    function convertirPKCS12aHexadecimal ( pkcs12 ) {
+      for ( var sci = 0; sci < pkcs12.safeContents.length; ++sci ) {
+        var safeContents = pkcs12.safeContents[sci];
+        for(var sbi = 0; sbi < safeContents.safeBags.length; ++sbi) {
+          var safeBag = safeContents.safeBags[sbi];
+          if(safeBag.type === forge.pki.oids.keyBag) {
+            privateKey = safeBag.key;
+          } else if(safeBag.type === forge.pki.oids.pkcs8ShroudedKeyBag) {
+            privateKey = safeBag.key;
+          } else if(safeBag.type === forge.pki.oids.certBag) {
+            if(certP12bag == null) {
+              certP12bag = safeBag.cert;
+              var certpem = forge.pki.certificateToPem(certP12bag);
+              var certBytesHex = forge.util.bytesToHex(certpem);
+            }
+          } 
+        }
+      }
+      return certBytesHex;
+    }
+
+
+    // Función para leer el .p12 luego de cargarlo
+    function leerCertificadoPKCS12 () {
+      let archivoAdjuntado = this.files[0];
+
+      reader.onload = function(e) {
+        var
+          contents = e.target.result,
+          pkcs12Der = convertirArrayBufferAString( contents ),
+          pkcs12B64 = forge.util.encode64( pkcs12Der );
+        decodificado = decodificarPKCS12( pkcs12Der );
+        certificadoHexadecimal = convertirPKCS12aHexadecimal(decodificado);
+        var
+          llavePrivada = buscarLlavePrivada(decodificado),
+          llavePublica = buscarLlavePublica(decodificado),
+          convertirAPKCS8 = privateKeyToPkcs8(llavePrivada),
+          convertirAPuKCS8 = publicKeyToPkcs8(llavePublica);
+
+          importarLlavePrivada(convertirAPKCS8);
+          importarLlavePublica(convertirAPuKCS8);
+      }
+      reader.readAsArrayBuffer(archivoAdjuntado);
+    }
+
+  /* ----- Eventos ----- */
+    // 1. Al adjuntar el archivo .p12 se ejecuta la función que lo lee y se extrae su información
+    inputCertificadoPKCS12.addEventListener('change', leerCertificadoPKCS12, false);
+  /* ----- Eventos ----- */
+
+
+
+    
+function convertStringToArrayBufferView(str)
+{
+    var bytes = new Uint8Array(str.length);
+    for (var iii = 0; iii < str.length; iii++)
+    {
+        bytes[iii] = str.charCodeAt(iii);
+    }
+
+    return bytes;
+}  
+
+var crypto = window.crypto || window.msCrypto;
+
+var promise_key = null;
+
+var private_key_object = null;
+var public_key_object = null;
+
+//var data = "QNimate";
+var data = "7a42eea172776c7a6de6d57aa9958d9654410934f5cfbf483b6135a24543f51c";
+
+var encrypted_hash = null;
+var encrypt_promise = null;
+
+var signature = null;
+
+var decrypt_promise = null;
+
+// Usando el algoritmo  RSA-PSS
+
+if(crypto.subtle)
+{
+    console.log("Antes del alert");
+    alert("Cryptography API Supported");
+
+    promise_key = crypto.subtle.generateKey({name: "RSA-PSS", modulusLength: 2048, publicExponent: new Uint8Array([0x01, 0x00, 0x01]), hash: {name: "SHA-256"}}, false, ["sign", "verify"]);
+
+    promise_key.then(function(key){
+        private_key_object = key.privateKey;
+        console.log("******// public_key_object //******");
+        console.log(public_key_object);
+        console.log("******// public_key_object //******");
+        public_key_object = key.publicKey;
+        console.log("****.. antes del encrypt_data()..****");
+        encrypt_data();
+    });
+
+    promise_key.catch = function(e){
+
+        console.log(e.message);
+    }
+   
+   
+}
+else
+{
+    alert("Cryptography API not Supported");
+}
+
+function encrypt_data()
+{
+    console.log("function encrypt_data()");
+    console.log("*** inicio private_key_object ***");
+    console.log(private_key_object);
+    console.log("*** fin private_key_object ***");
+    console.log("*** inicio privatekey ***");
+    console.log(privateKeyRSAPSS);
+    console.log("*** fin privatekey ***");
+
+    if(privateKeyRSAPSS==null) {
+        encrypt_promise = crypto.subtle.sign({name: "RSA-PSS", saltLength: 128, }, private_key_object, convertStringToArrayBufferView(data));
+
+        encrypt_promise.then(
+            function(result_signature){
+                signature = result_signature; //signature generated
+                console.log("**************************************");
+                console.log(signature);
+                console.log("**************************************");
+                console.log("****.. antes del decrypt_data()..****");
+                decrypt_data();
+            },
+            function(e){
+                console.log(e);
+            }
+        );
+    }
+    else {
+
+        encrypt_promise = crypto.subtle.sign({name: "RSA-PSS", saltLength: 128, }, privateKeyRSAPSS, convertStringToArrayBufferView(data));
+
+        encrypt_promise.then(
+            function(result_signature){
+                signature = result_signature; //signature generated
+                console.log("**************************************");
+                console.log(signature);
+                console.log("**************************************");
+                console.log("****.. antes del decrypt_data()..****");
+                decrypt_data();
+            },
+            function(e){
+                console.log(e);
+            }
+        );
+    }
+}
+
+function decrypt_data()
+{
+    if(publicKeyRSAPSS == null) {
+        console.log("function decrypt_data()");
+        console.log("******// public_key_object decrypt_data() //******");
+        console.log(public_key_object);
+        console.log("******// public_key_object decrypt_data() //******");
+        decrypt_promise = crypto.subtle.verify({name: "RSA-PSS", saltLength: 128, }, public_key_object, signature, convertStringToArrayBufferView(data));
+
+            decrypt_promise.then(
+                function(result){
+                    console.log(result);//true or false
+                },
+            function(e){
+                console.log(e.message);
+            }
+        );
+    }
+    else {
+        console.log("function decrypt_data()");
+        console.log("******// publicKeyRSAPSS decrypt_data() //******");
+        console.log(publicKeyRSAPSS);
+        console.log("******// publicKeyRSAPSS decrypt_data() //******");
+        decrypt_promise = crypto.subtle.verify({name: "RSA-PSS", saltLength: 128, }, publicKeyRSAPSS, signature, convertStringToArrayBufferView(data));
+        decrypt_promise.then(
+            function(result){
+                console.log(result);//true or false
+            },
+            function(e){
+                console.log(e.message);
+            }
+        );
+
+    }
+
+}
+/*
+// Usando el algoritmo  RSASSA-PKCS1-v1_5
+if(crypto.subtle)
+{
+    console.log("Antes del alert");
+    alert("Cryptography API Supported");
+
+    promise_key = crypto.subtle.generateKey({name: "RSASSA-PKCS1-v1_5", modulusLength: 2048, publicExponent: new Uint8Array([1, 0, 1]), hash: {name: "SHA-256"}}, false, ["sign", "verify"]);
+
+    promise_key.then(function(key){
+        private_key_object = key.privateKey;
+        public_key_object = key.publicKey;
+        console.log("****.. antes del encrypt_data()..****");
+        encrypt_data();
+    });
+
+    promise_key.catch = function(e){
+
+        console.log(e.message);
+    }
+}
+else
+{
+    alert("Cryptography API not Supported");
+}
+
+function encrypt_data()
+{
+    console.log("function encrypt_data()");
+    //encrypt_promise = crypto.subtle.sign({name: "RSASSA-PKCS1-v1_5"}, private_key_object, convertStringToArrayBufferView(data));
+    console.log("*** inicio private_key_object ***");
+    console.log(private_key_object);
+    console.log("*** fin private_key_object ***");
+    console.log("*** inicio privatekey ***");
+    console.log(privateKeyRSASSAPKCS1v1_5);
+    console.log("*** fin privatekey ***");
+
+    if(privateKeyRSASSAPKCS1v1_5 == null) {
+        encrypt_promise = crypto.subtle.sign({name: "RSASSA-PKCS1-v1_5"}, private_key_object, convertStringToArrayBufferView(data));
+
+        encrypt_promise.then(
+            function(result_signature){
+                signature = result_signature; //signature generated
+                console.log("**************************************");
+                console.log(signature);
+                console.log("**************************************");
+                console.log("****.. antes del decrypt_data()..****");
+                decrypt_data();
+            },
+            function(e){
+                console.log(e);
+            }
+        ); 
+    } else {
+         console.log("privateKey != Null");
+         encrypt_promise = crypto.subtle.sign({name: "RSASSA-PKCS1-v1_5"}, privateKeyRSASSAPKCS1v1_5, convertStringToArrayBufferView(data));
+
+        encrypt_promise.then(
+            function(result_signature){
+                signature = result_signature; //signature generated
+                console.log("**************************************");
+                console.log(signature);
+                console.log("**************************************");
+                console.log("****.. antes del decrypt_data()..****");
+                decrypt_data();
+            },
+            function(e){
+                console.log(e);
+            }
+        ); 
+    }
+}
+
+function decrypt_data()
+{
+    console.log("function decrypt_data()");
+    decrypt_promise = crypto.subtle.verify({name: "RSASSA-PKCS1-v1_5"}, public_key_object, signature, convertStringToArrayBufferView(data));
+
+    decrypt_promise.then(
+        function(result){
+            console.log(result);//true or false
+        },
+        function(e){
+            console.log(e.message);
+        }
+    );
+}
+*/
+
+  buttonsubmit3.addEventListener('click', function() {
+      encrypt_data();
+    }, true);
+
+</script>
+</body>
+</html>
