Esempio n. 1
0
 protected Container sign() {
   Container container =
       ContainerBuilder.aContainer()
           .withConfiguration(createDigiDoc4JConfiguration())
           .withDataFile(
               new ByteArrayInputStream("file contents".getBytes()),
               "dds_JÜRIÖÖ € žŠ päev.txt",
               "application/octet-stream")
           .build();
   byte[] hashToSign =
       prepareSigning(container, CertificatesForTests.SIGN_CERT, createSignatureParameters());
   byte[] signatureValue = signWithRsa(CertificatesForTests.PRIVATE_KEY_FOR_SIGN_CERT, hashToSign);
   container.signRaw(signatureValue);
   return container;
 }
Esempio n. 2
0
  /** External signing example */
  public static void main(String[] args) throws Exception {
    System.setProperty("digidoc4j.mode", "TEST");
    Configuration configuration = new Configuration(Configuration.Mode.TEST);
    Container container =
        ContainerBuilder.aContainer()
            .withConfiguration(configuration)
            .withDataFile("testFiles/test.txt", "text/plain")
            .build();

    SignatureToken externalSigner =
        new ExternalSigner(getSignerCert()) {
          @Override
          public byte[] sign(DigestAlgorithm digestAlgorithm, byte[] dataToSign) {

            // IMPLEMENT YOUR EXTERNAL SIGNING HERE

            try {
              KeyStore keyStore = KeyStore.getInstance("PKCS12");
              try (FileInputStream stream = new FileInputStream("testFiles/signout.p12")) {
                keyStore.load(stream, "test".toCharArray());
              }
              PrivateKey privateKey = (PrivateKey) keyStore.getKey("1", "test".toCharArray());
              final String javaSignatureAlgorithm = "NONEwith" + privateKey.getAlgorithm();

              return DSSUtils.encrypt(javaSignatureAlgorithm, privateKey, addPadding(dataToSign));
            } catch (Exception e) {
              throw new DigiDoc4JException("Loading private key failed");
            }
          }

          private byte[] addPadding(byte[] digest) {
            return ArrayUtils.addAll(SHA256.digestInfoPrefix(), digest);
          }
        };

    Signature signature =
        SignatureBuilder.aSignature(container).withSignatureToken(externalSigner).invokeSigning();

    container.addSignature(signature);
    container.save("prototype.bdoc");
  }