使用 itextsharp 将单独签名的散列放置到 PDF 中的多个位置

placing the separately signed hash to Multiple places in PDF using itextsharp

我想在 PDF 的多个位置放置相同的外部签名哈希(签名值)。

我已参考页面 'how-to-place-the-same-digital-signatures-to-multiple-places-in-pdf-using-itextsh' 并尝试实施 mkl 提供的解决方法(请参考此 How to place the Same Digital signatures to Multiple places in PDF using itextsharp.net)。

并且有效。我将它移植到使用 web 服务/ api 从外部签署签名者字节,它也可以工作。现在由于其中一项要求,我更改了计算哈希的方式。

现在而不是(旧的):

byte[] hash = DigestAlgorithms.Digest(data, "SHA256");
byte[] sh = sgn.getAuthenticatedAttributeBytes(hash, null, null, CryptoStandard.CMS);

我正在尝试使用(新的):

        int contentEstimated=8192;
        HashAlgorithm sha = new SHA256CryptoServiceProvider();
        int read = 0;
        byte[] buff = new byte[contentEstimated];
        while ((read = data.Read(buff, 0, contentEstimated)) > 0)
        {
            sha.TransformBlock(buff, 0, read, buff, 0);
        }
        sha.TransformFinalBlock(buff, 0, 0);
        byte[] hash = Org.BouncyCastle.Utilities.Encoders.Hex.Encode(sha.Hash);

        string hashtext = Encoding.UTF8.GetString(hash, 0, hash.Length); //for writing it to file or sharing it to another api
        byte[] hash1 = StringToByteArray(hashtext);

        byte[] sh = sgn.getAuthenticatedAttributeBytes(hash1, null, null, CryptoStandard.CMS); or 
        byte[] sh = sgn.getAuthenticatedAttributeBytes(hash, null, null, CryptoStandard.CMS); //tried both

如果我尝试在现有实现中使用它,签名将变得无效并出现错误 "Document has been altered or corrupt since it was signed"。你能告诉我我哪里做错了吗?

在大多数引用的页面上,他们使用了这种带有嵌入函数的哈希生成方法,其中计算出的哈希被嵌入到 pdf 中,

    byte[] paddedSig = new byte[csize];
    System.Array.Copy(pk, 0, paddedSig, 0, pk.Length);
    PdfDictionary dic2 = new PdfDictionary();
    dic2.Put(PdfName.CONTENTS, new PdfString(paddedSig).SetHexWriting(true));
    appearance.Close(dic2);

谢谢。 - 坦迈

在评论中,OP 澄清说他想调整解决方案 here 以使用接受文档散列的外部签名服务(更准确地说是十六进制文档的 SHA256 散列值format) 和 returns 一个 full-fledged CMS 签名容器。

在这种情况下,原来的AllPagesSignatureContainer方法Sign

public byte[] Sign(Stream data)
{
    String hashAlgorithm = externalSignature.GetHashAlgorithm();
    PdfPKCS7 sgn = new PdfPKCS7(null, chain, hashAlgorithm, false);
    IDigest messageDigest = DigestUtilities.GetDigest(hashAlgorithm);
    byte[] hash = DigestAlgorithms.Digest(data, hashAlgorithm);
    byte[] sh = sgn.getAuthenticatedAttributeBytes(hash, null, null, CryptoStandard.CMS);
    byte[] extSignature = externalSignature.Sign(sh);
    sgn.SetExternalDigest(extSignature, null, externalSignature.GetEncryptionAlgorithm());
    return sgn.GetEncodedPKCS7(hash, null, null, null, CryptoStandard.CMS);
}

必须更改为不使用 PdfPKCS7 sgn 本身创建 CMS 容器,而只是计算文档哈希,将其发送到服务并使用服务返回的容器:

public byte[] Sign(Stream data)
{
    String hashAlgorithm = externalSignature.GetHashAlgorithm();
    IDigest messageDigest = DigestUtilities.GetDigest(hashAlgorithm);
    byte[] hash = DigestAlgorithms.Digest(data, hashAlgorithm);
    byte[] hexHash = Org.BouncyCastle.Utilities.Encoders.Hex.Encode(hash);
    string hexHashString = Encoding.UTF8.GetString(hexHash , 0, hexHash.Length);
    var response = [... call service with document hash hexHashString ...];
    byte[] signatureContainer = [... extract binary CMS container from response ...];
    return signatureContainer;
}

OP 没有提到任何关于响应格式的内容,所以我不能多说 extract binary CMS container from response 部分。它可能包括选择较大响应结构的一个属性,它可能包括解码编码值(可能是十六进制编码的字符串),...