title: Generating Digital Signature using BouncyCastle in Java
body: <p>I am generating digital signature for the file by using following code in java. And I am writing digital signature to database. Is this digital signature is time dependent? Because every time I am getting different digital signature for the same file.<br>
      Do I get different digital signature at different time for the same file?</p>
  
  <pre><code>package generatesign;
  import java.io.File;
  import java.nio.file.Files; 
  import java.security.KeyStore;
  import java.security.PrivateKey;
  import java.security.PublicKey;
  import java.security.Security;
  import java.security.Signature;
  import java.security.cert.X509Certificate;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.PreparedStatement;
  import java.util.ArrayList;
  import java.util.Enumeration;
  import java.util.List;
  
  import org.bouncycastle.cert.jcajce.JcaCertStore;
  import org.bouncycastle.cms.CMSProcessableByteArray;
  import org.bouncycastle.cms.CMSSignedData;
  import org.bouncycastle.cms.CMSSignedDataGenerator;
  
  import org.bouncycastle.cms.CMSTypedData;
  import org.bouncycastle.cms.jcajce.JcaSignerInfoGeneratorBuilder;
  import org.bouncycastle.jce.provider.BouncyCastleProvider;
  import org.bouncycastle.operator.ContentSigner;
  import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
  import org.bouncycastle.operator.jcajce.JcaDigestCalculatorProviderBuilder;
  import org.bouncycastle.util.Store;
  
  import sun.misc.BASE64Encoder;
  
  public class SaveFileSign {
      static final String KEYSTORE_ALIAS = "GURU ESWARA RAO (1)";
      static final String KEYSTORE_PWD = "abcd@123";
      static final String DIGEST_SHA1 = "SHA1WithRSA";
      static final String BC_PROVIDER = "BC";
  
  @SuppressWarnings({ "unchecked", "rawtypes" })
  public static void main(String[] args) throws Exception {
      try {
          KeyStore ks;
          byte[] fileContent = Files.readAllBytes(new 
          File("F:\\myfile.xlsx").toPath());
          Security.addProvider(new BouncyCastleProvider());
          ks = KeyStore.getInstance("WINDOWS-MY", "SunMSCAPI");
          ks.load(null, KEYSTORE_PWD.toCharArray());
          Enumeration&lt;String&gt; aliases = ks.aliases();
          while (aliases.hasMoreElements()) {
              String alias = (String) aliases.nextElement();
  
              PrivateKey privkey = (PrivateKey) ks.getKey(KEYSTORE_ALIAS, 
              KEYSTORE_PWD.toCharArray());
              System.out.println("Private Key****" + privkey + "**");
              X509Certificate x509Certificate = (X509Certificate) 
              ks.getCertificate(alias);
              PublicKey publicKey = x509Certificate.getPublicKey();
              System.out.println("Public Key of " + KEYSTORE_ALIAS + "==" + 
              publicKey + "==");
              Signature signature = Signature.getInstance(DIGEST_SHA1);
              signature.initSign(privkey);
              signature.update(fileContent);
  
              // Build CMS
              X509Certificate certFromKeystore = (X509Certificate) 
              ks.getCertificate(KEYSTORE_ALIAS);
              List certList = new ArrayList();
              CMSTypedData data = new CMSProcessableByteArray(fileContent);
              certList.add(certFromKeystore);
              Store certs = new JcaCertStore(certList);
  
  
  
  
  
      CMSSignedDataGenerator gen = new CMSSignedDataGenerator();
                      ContentSigner sha1Signer = new 
                      JcaContentSignerBuilder(DIGEST_SHA1).setProvider("SunMSCAPI")
                              .build(privkey);
                      gen.addSignerInfoGenerator(new JcaSignerInfoGeneratorBuilder(
                              new JcaDigestCalculatorProviderBuilder().setProvider(BC_PROVIDER).build()).build(sha1Signer,
                                      certFromKeystore));
                      gen.addCertificates(certs);
                      CMSSignedData signedData = gen.generate(data, false); 
                      BASE64Encoder encoder = new BASE64Encoder();
                      String signedContent = encoder.encode((byte[]) signedData.getSignedContent().getContent());
                      System.out.println("=============Signed content:=============" + "\n" + signedContent + "***"
                              + signedContent.length() + "\n");
                      String envelopedData = encoder.encode(signedData.getEncoded());
                      System.out.println("==============Enveloped data===============" + "\n" + envelopedData + "***"
                              + envelopedData.length());
  
                      System.out.println("======Saving signature to DATABASE======");
                      // create a mysql database connection
                      String myDriver = "org.postgresql.Driver";
                      String myUrl = "jdbc:postgresql://localhost:5444/bmtcdemo";
                      Class.forName(myDriver);
                      Connection conn = DriverManager.getConnection(myUrl, "enterprisedb", "unibrain");
                      java.sql.Statement st = conn.createStatement();
                      PreparedStatement pst = conn.prepareStatement("INSERT INTO f_signature (sign) VALUES(?)");
                      pst.setString(1, envelopedData);
                      int i = pst.executeUpdate();
                      System.out.println("saved success====Rows affected====" + i);
                  }
  
              } catch (Exception e) {
                  e.getMessage();
              }
          }
  
  }
  </code></pre>
  
  <p>Output:   </p>
  
  <pre><code>MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAAoIAwggXLMIIE
  s6ADAgECAgQA4swkMA0GCSqGSIb3DQEBCwUAMIGOMQswCQYDVQQGEwJJTjEqMCgGA1UEChMhZU11
  ZGhyYSBDb25zdW1lciBTZXJ2aWNlcyBMaW1pdGVkMR0wGwYDVQQLExRDZXJ0aWZ5aW5nIEF1dGhv
  cml0eTE0MDIGA1UEAxMrZS1NdWRocmEgU3ViIENBIGZvciBDbGFzcyAzIEluZGl2aWR1YWwgMjAx
  NDAeFw0xODAxMTcxMzAxMzZaFw0yMDAxMTcxMzAxMzZaMIH6MQswCQYDVQQGEwJJTjERMA8GA1UE
  ChMIUGVyc29uYWwxSTBHBgNVBBQTQDlGQkY0MjNBN0FFMTNFNDE4QzIwN0FBNDVDQTRFMzhGRjc3
  MDRFMDc0NzcwQjlCOUEwQTBDRTYyNUM5RDFEOTQxDzANBgNVBBETBjUzMzI1NTEXMBUGA1UECBMO
  QU5ESFJBIFBSQURFU0gxSTBHBgNVBAUTQDY5YzU5NGM2OTc4NmU2NDU2YmE5N2RkNjkwNmM1YjM5
  ODhkODdjZmIwODFiMzQ2ZTFiY2FiMWEwYWI5Yjg4OWYxGDAWBgNVBAMTD0dVUlUgRVNXQVJBIFJB
  TzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAME9iaVzUFc/v6axddTodLrgyXaqIPLi
  4CBTMz4vPczgQnE+y+zFtivN0CZY7oUU9rC2BijrtSSWI+bxraXHmIRDPqkzLxK9RUvWqdjmAhMt
  FcmA9nqyLJiVuK1uvfY+mLDcKFN/JKn/dpDTe9R2YZtW9JCm6N4J/xaDsmK0f1oauVUmsAdcMEIv
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