Пример #1
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");
  }