×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: ActionScript
Posted by: Dorian Cuentas
Added: Jan 16, 2017 4:47 PM
Views: 12
Tags: no tags
Falla firma pdf
  1. /*
  2.  * ##############################################################################
  3.  * This is a unlicensed code
  4.  * you could find a beautiful piece of code or a disaster that will make you
  5.  * wish a hippocampus dysfunction, no guarantees, use it at your discretion.
  6.  * ns616dc@gmail.com
  7.  * ##############################################################################
  8.  */
  9. package com.iofesac.notsel.desktop.dsignaturebasicsigner.signer;
  10.  
  11. import com.dsignature.config.ConfigManager;
  12. import com.dsignature.crypto.SignatureOptions;
  13. import com.dsignature.generation.RCSignatureProvider;
  14. import com.dsignature.license.DSignatureLicenseManager;
  15. import java.io.ByteArrayOutputStream;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.security.KeyStore;
  19. import java.security.KeyStoreException;
  20. import java.security.NoSuchAlgorithmException;
  21. import java.security.PrivateKey;
  22. import java.security.UnrecoverableKeyException;
  23. import java.security.cert.CertificateException;
  24. import java.security.cert.X509Certificate;
  25. import java.util.logging.Level;
  26. import java.util.logging.Logger;
  27. import org.apache.commons.io.IOUtils;
  28. import org.junit.After;
  29. import org.junit.AfterClass;
  30. import org.junit.Assert;
  31. import org.junit.Before;
  32. import org.junit.BeforeClass;
  33. import org.junit.Test;
  34.  
  35. /**
  36.  *
  37.  * @author dorian <ns616dc(at)gmail.com>
  38.  */
  39. public class SignerTest {
  40.  
  41.     public static final String HTTPTTSA = "http://tss.accv.es:8318/tsa";
  42.     public static final String POLITICA_FIRMAS = "http://ks.kprtech.com/policies/politica_firmas_xades_t_offline_v0.2.pdf";
  43.  
  44.     public SignerTest() {
  45.     }
  46.  
  47.     @BeforeClass
  48.     public static void setUpClass() {
  49.     }
  50.  
  51.     @AfterClass
  52.     public static void tearDownClass() {
  53.     }
  54.  
  55.     @Before
  56.     public void setUp() {
  57.     }
  58.  
  59.     @After
  60.     public void tearDown() {
  61.     }
  62.  
  63.     /**
  64.      * Test of signFile method, of class Signer.
  65.      */
  66.     @Test
  67.     public void testSignFile() {
  68.  
  69.         try {
  70.  
  71.             final InputStream licAsResource = this.getClass().getResourceAsStream("/iofe.lic");
  72.             DSignatureLicenseManager.getDSignatureLicenseManager().installLicense(
  73.                     IOUtils.toByteArray(licAsResource));
  74.             DSignatureLicenseManager.getDSignatureLicenseManager().verifyLicense();
  75.             ConfigManager.init();
  76.  
  77.             KeyStore keyStore = KeyStore.getInstance("JKS");
  78.             keyStore.load(this.getClass().getResourceAsStream("./leonardnimoy.jks"), "123456".toCharArray());
  79.  
  80.             final PrivateKey privateKey = (PrivateKey) keyStore.getKey("leonardnimoy", "123456".toCharArray());
  81.             // Build signature provider
  82.             RCSignatureProvider signatureProvider = new RCSignatureProvider.Builder()
  83.                     .setSignerCertificate((X509Certificate) keyStore.getCertificate("leonardnimoy"))
  84.                     .setPrivateKey(privateKey)
  85.                     .setSignaturePolicy(POLITICA_FIRMAS)
  86.                     .setTSAUrl(HTTPTTSA)
  87.                     .build();
  88.  
  89.             String digidocFileName = "pdf.pdf";
  90.             InputStream unsignedData = this.getClass().getResourceAsStream("./pdf.pdf");
  91.             SignatureOptions signatureOptions = new SignatureOptions();
  92.             signatureOptions.setLocationCity("Lima");
  93.             signatureOptions.setLocationCountry("Peru");
  94.             signatureOptions.setDataToSign(unsignedData);
  95.             signatureOptions.setCommitmentOID("1.2.840.113549.1.9.16.6.2");
  96.             signatureOptions.setDigidocFilename(digidocFileName);
  97.  
  98.             ByteArrayOutputStream resolvedOutBuffer = new ByteArrayOutputStream();
  99.             signatureOptions.setSignatureFormat("PAdES");
  100.             signatureOptions.setOutputStream(resolvedOutBuffer);
  101.             signatureProvider.generateSignature(signatureOptions);
  102.             resolvedOutBuffer.flush();
  103.             Assert.assertNotNull(resolvedOutBuffer.toByteArray());
  104.             Assert.assertTrue(resolvedOutBuffer.toByteArray().length > 0);
  105.         } catch (KeyStoreException | IOException | NoSuchAlgorithmException | CertificateException | UnrecoverableKeyException ex) {
  106.             Logger.getLogger(SignerTest.class.getName()).log(Level.SEVERE, null, ex);
  107.         }
  108.     }
  109. }
  110.