【问题标题】:how to add timestamp without Digital Signature如何在没有数字签名的情况下添加时间戳
【发布时间】:2013-06-24 11:05:39
【问题描述】:

我想在我的 PDF 文档中添加时间戳(没有数字签名)。我该怎么做?

我可以使用 Itext 进行数字签名(我这里有 TSAClient):

MakeSignature.signDetached(appearance, digest, signature, chain, null, null, tsa, 0, subfilter);

但是没有数字签名怎么做类似的事情呢?使用 Bouncy Castle 或 Itext 或 Pdfbox... 或与其他库..

【问题讨论】:

    标签: java itext bouncycastle pdfbox


    【解决方案1】:

    在 iText 中您正在寻找

    LtvTimestamp.timestamp(appearance, tsa, signatureName);
    

    另请参阅。 JavaDoc 文档:

    /**
     * Signs a document with a PAdES-LTV Timestamp. The document is closed at the end.
     * @param sap the signature appearance
     * @param tsa the timestamp generator
     * @param signatureName the signature name or null to have a name generated
     * automatically
     * @throws DocumentException 
     * @throws IOException 
     * @throws GeneralSecurityException
     */
    

    您可能需要阅读Digital Signatures for PDF documents 中的第 5.4.1 节添加文档安全存储 (DSS) 和文档级时间戳,以便在上下文中使用。

    请注意,旧的 PDF 查看器无法正确识别文档级时间戳,因为他们最近才进入 PDF 世界,即 PAdES-4

    【讨论】:

      【解决方案2】:

      要使用 PDFBox 做到这一点,您需要一些简单的 SignatureInterface 实现,如下所示:

      public class TimestampSignatureImpl implements SignatureInterface {
          private TSAClient tsaClient;
          public TimestampSignatureImpl(TSAClient tsaClient) {
              super();
              this.tsaClient = tsaClient;
          }
          @Override
          public byte[] sign(InputStream paramInputStream) throws IOException {
              return tsaClient.getTimeStampToken(IOUtils.toByteArray(paramInputStream));
          }
      }
      

      还有一些像这样的 PDSignature:

      PDSignature signature = new PDSignature();
      signature.setFilter(PDSignature.FILTER_ADOBE_PPKLITE); 
      signature.setSubFilter(COSName.getPDFName("ETSI.RFC3161"));
      signature.setSignDate(Calendar.getInstance());
      

      然后像这样签署您的 pdf:

      PDDocument pdf = PDDocument.load(inputFile);
      MessageDigest digest = MessageDigest.getInstance("SHA-256");
      TSAClient tsaClient = new TSAClient(new URL("your time stamp authority"), null, null, digest);
      pdf.addSignature(signature, new TimestampSignatureImpl(tsaClient));
      pdf.saveIncremental(new FileOutputStream(outputFile));
      pdf.close();
      

      P.S:TSAClient 取自 PDFBox 示例。

      【讨论】:

        【解决方案3】:

        使用iText7,您可以通过调用PdfSigner 类的以下方法来添加DTS(文档时间戳)

        ITSAClient tsa = new TSAClientBouncyCastle(tsaUrl, tsaUser, tsaPass);
        pdfSigner.timestamp(tsa, "SignatureTimeStamp");
        

        ITSAClient tsa = new TSAClientBouncyCastle(tsaUrl, tsaUser, tsaPass, 8192, "SHA-256"); 
        pdfSigner.timestamp(tsa, "SignatureTimeStamp");
        

        还有,java 文档

        /**
         * Signs a document with a PAdES-LTV Timestamp. The document is closed at the end.
         * NOTE: This method closes the underlying pdf document. This means, that current instance
         * of PdfSigner cannot be used after this method call.
         *
         * @param tsa           the timestamp generator
         * @param signatureName the signature name or null to have a name generated
         *                      automatically
         * @throws IOException
         * @throws GeneralSecurityException
         */
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-12-21
          • 2021-05-29
          • 1970-01-01
          • 1970-01-01
          • 2010-12-15
          • 2020-10-13
          • 1970-01-01
          • 2018-09-16
          相关资源
          最近更新 更多