Links
developers   about

Examples



Following an example is shown where a file is signed (using a p12 token file or a smartcard), is verified and finally timestamped
////////////////////////////////////////////////////
////////////////////////////////////////////////////
import java.io.*;
import java.security.*;
import javax.swing.*;
import org.bouncycastle.cms.*;
import org.javasign.gui.*;
import org.javasign.*;
import org.javasign.util.*;

public class Test {

  public static void main(String[] args) {

    String base = "C:\\programmi\\javasign\\";
    File src = new File(base+"license.txt");
    File dest = new File(base+"license.txt.p7m");
	File destTs=new File(base+"license.txt.p7m.tsr");

    try {
      ///////////////////////////////////////////
      //sign with token
      //
      String algorithm=CMSSignedDataGenerator.DIGEST_SHA1;
      String ksType="PKCS12";

      String p12FileName=base+"id.p12";  //"id.p12";
      String p12Password="raffa";  //the password;

      File keystore = new File(p12FileName);

      KeyStoreLoader kl = new KeyStoreLoader(keystore,ksType,p12Password);
      kl.load();

      PrivateKey pkey=kl.getPrivateKey();
      X509Certificate cert=kl.getX509Certificate();

      P7Generator p7genToken = new P7Generator(src,dest,pkey,cert,algorithm);
      p7genToken.generate();

      /////////////////////////////////////////////
      //sign with smartcard

      String driver="incryptoki2.dll";
      String KEY_LABEL="DSPrivateKey#0";
      String PIN="123456";

      CryptokiGenerator p7genSC =
         new CryptokiGenerator(driver,src,dest,KEY_LABEL,PIN);
      p7genSC.generate();


      /////////////////////////////////////////////
      //verify

      P7Verifier pf= new P7Verifier(dest);


      pf.setCheckCA(true);

      pf.setCheckCrl(false);
      pf.setDownloadCrl(false);

      Verifications verifications =  pf.verify();

      boolean isP7m=verifications.isP7m();
      boolean isValid=verifications.isVerified();
      boolean isTsr=pf.isTS();

      /////////////////////////////////////////////////////////////////
      //timestamp
      String url="http://www.tecnes.com/javasign/timestamp";

      TsrGenerator tsrgen = new TsrGenerator(url,dst,destTs);

      tsrgen.generate();


    } catch (Exception e) {
      e.printStackTrace();
    }


  }
}