SHA1 Digest或Xades SignedProperties的计算不起作用

时间:2015-11-21 23:48:11

标签: c# xml digest

我有以下XML文件:

<xad:SignedProperties Id="Sig_20151117_172752_SP">
                <xad:SignedSignatureProperties>
                    <xad:SigningTime>2015-11-17T16:27:59Z</xad:SigningTime>
                    <xad:SigningCertificate>
                        <xad:Cert>
                            <xad:CertDigest>
                                <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
                                <ds:DigestValue>vkVMqVMRsiSbo3Zgvk9sTfVtRDs=</ds:DigestValue>
                            </xad:CertDigest>
                            <xad:IssuerSerial>
                                <ds:X509IssuerName>CN=CERTEUROPE ADVANCED CA V4, OU=0002 434202180, O=Certeurope, C=FR</ds:X509IssuerName>
                                <ds:X509SerialNumber>747583</ds:X509SerialNumber>
                            </xad:IssuerSerial>
                        </xad:Cert>
                    </xad:SigningCertificate>
                    <xad:SignaturePolicyIdentifier>
                        <xad:SignaturePolicyId>
                            <xad:SigPolicyId>
                                <xad:Identifier>1234567</xad:Identifier>
                                <xad:Description>Description de la politique de signature numérique</xad:Description>
                            </xad:SigPolicyId>
                            <xad:SigPolicyHash>
                                <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
                                <ds:DigestValue>ZXphcmxramhxZGY=</ds:DigestValue>
                            </xad:SigPolicyHash>
                            <xad:SigPolicyQualifiers>
                                <xad:SigPolicyQualifier>
                                    <xad:SPURI>1234567</xad:SPURI>
                                </xad:SigPolicyQualifier>
                            </xad:SigPolicyQualifiers>
                        </xad:SignaturePolicyId>
                    </xad:SignaturePolicyIdentifier>
                    <xad:SignerRole>
                        <xad:ClaimedRoles/>
                    </xad:SignerRole>
                </xad:SignedSignatureProperties>
                <xad:SignedDataObjectProperties>
                    <xad:CommitmentTypeIndication>
                        <xad:CommitmentTypeId>
                            <xad:Identifier>1.2.840.113549.1.9.16.6.1</xad:Identifier>
                            <xad:Description>1.2.840.113549.1.9.16.6.1</xad:Description>
                        </xad:CommitmentTypeId>
                        <xad:ObjectReference>#D0-Reference</xad:ObjectReference>
                    </xad:CommitmentTypeIndication>
                </xad:SignedDataObjectProperties>
            </xad:SignedProperties>

(我故意将其缩进,在开始时,它是线性XML)。

我需要获得以下摘要:

<ds:Reference Id="Sig_20151117_172752_SP-Reference" Type="http://uri.etsi.org/01903/#SignedProperties" URI="#Sig_20151117_172752_SP">
            <ds:Transforms>
                <ds:Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/>
            </ds:Transforms>
            <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/>
            <ds:DigestValue>euEROs8DacsBe3xqXBY5T+M07AI=</ds:DigestValue>
        </ds:Reference>

我使用Canonicalization C14N这样做。

我尝试将这些名称空间添加到第一个标记中:

的xmlns:XAD =&#34; HTTP://uri.etsi.org/01903/v1.3.2#" 的xmlns:DS =&#34; HTTP://www.w3.org/2000/09/xmldsig#"

按以下顺序:

但这些尝试都不起作用。有什么遗失的吗? (这里是我用来计算SHA-1摘要的C#方法:

public string CalculateHashSHA1(string input)
        {
            using (SHA1Managed sha1 = new SHA1Managed())
            {
                return Convert.ToBase64String(sha1.ComputeHash(System.Text.Encoding.UTF8.GetBytes(input)));
            }
        }

编辑:

我试图获取此摘要:euEROs8DacsBe3xqXBY5T + M07AI =

1 个答案:

答案 0 :(得分:1)

事实上,我需要使用这种方法:

XmlDsigExcC14NTransform innerTransform = new XmlDsigExcC14NTransform(false);

结果是正确的。