C#计算和验证SEPA(XML)paymentfile SHA256值
我对同样的问题,像马库斯·德雷尔挣扎-paymentfile> C#的计算SHA256值SEPA(XML)paymentfile
i am struggling with the same issue like Markus Dreyer: C# Calculate SHA256 value for SEPA (XML) paymentfile
根据该协议DFU我要计算SHA256哈希值:
According to the DFÜ Agreement i have to calculate a sha256 hash value:
- 使用整个包含的文档,包括打开和关闭标签创建的哈希值。
- 文档按照规范的XML,版本1.0规范化。 ( http://www.w3.org/TR/2001/REC- XML-C14N-20010315 )。
- 在包括文件的情况下,册封也将被执行协议,ING的主要文件。
- SHA-256作为哈希算法。
- The hash value is created using the entire contained document, including the opening and closing tag.
- The document is canonicalized according to Canonical XML, version 1.0. (http://www.w3.org/TR/2001/REC-xml-c14n-20010315).
- In the case of included documents, the canonisation has also to be executed accord-ing to the main document.
- SHA-256 is used as hash algorithm.
这是一个简单有效的XML文件(从金融工具导出):
This is a sample valid xml File ( exported from an financial tool ) :
<?xml version="1.0" encoding="UTF-8"?>
<conxml xmlns="urn:conxml:xsd:container.nnn.002" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:conxml:xsd:container.nnn.002 container.nnn.002.xsd">
<CreDtTm>2013-08-27T07:20:25Z</CreDtTm>
<MsgPain001>
<HashValue>33E579FE7A9AF6C32C100E8578EBD63E54A2DF47C6849F7A4BC8BEA9E2794197</HashValue>
<HashAlgorithm>SHA256</HashAlgorithm>
<Document xmlns="urn:swift:xsd:$pain.001.002.02">
<pain.001.001.02>
<GrpHdr>
<MsgId>D005201308270920191</MsgId>
<CreDtTm>2013-08-27T07:20:19Z</CreDtTm>
<BtchBookg>true</BtchBookg>
<NbOfTxs>1</NbOfTxs>
<CtrlSum>0.50</CtrlSum>
<Grpg>MIXD</Grpg>
<InitgPty>
<Nm>Test</Nm>
</InitgPty>
</GrpHdr>
<PmtInf>
<PmtInfId>D005201308270920191</PmtInfId>
<PmtMtd>TRF</PmtMtd>
<PmtTpInf>
<SvcLvl>
<Cd>SEPA</Cd>
</SvcLvl>
</PmtTpInf>
<ReqdExctnDt>2013-08-27</ReqdExctnDt>
<Dbtr>
<Nm>Test</Nm>
</Dbtr>
<DbtrAcct>
<Id>
<IBAN>DE76200700000888888888</IBAN>
</Id>
</DbtrAcct>
<DbtrAgt>
<FinInstnId>
<BIC>DEUTDEHHXXX</BIC>
</FinInstnId>
</DbtrAgt>
<ChrgBr>SLEV</ChrgBr>
<CdtTrfTxInf>
<PmtId>
<EndToEndId>NOTPROVIDED</EndToEndId>
</PmtId>
<Amt>
<InstdAmt Ccy="EUR">0.50</InstdAmt>
</Amt>
<CdtrAgt>
<FinInstnId>
<BIC>DEUTDEHHXXX</BIC>
</FinInstnId>
</CdtrAgt>
<Cdtr>
<Nm>Erwin Mustermann</Nm>
</Cdtr>
<CdtrAcct>
<Id>
<IBAN>DE09200700000123456789</IBAN>
</Id>
</CdtrAcct>
<RmtInf>
<Ustrd>Sepa Test Gutschrift</Ustrd>
</RmtInf>
</CdtTrfTxInf>
</PmtInf>
</pain.001.001.02>
</Document>
</MsgPain001>
</conxml>
据马库斯·德雷尔的解决方案,这是我的代码:
According to the Solution from Markus Dreyer, here is my code:
System.Text.UTF8Encoding enc = new UTF8Encoding(false);
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.Load(@"path to file");
XmlNodeList list = doc.GetElementsByTagName("Document");
String s = list.Item(0).OuterXml;
MemoryStream msIn = new MemoryStream(enc.GetBytes(s));
XmlDsigC14NTransform t = new XmlDsigC14NTransform(true);
t.LoadInput(msIn);
MemoryStream ms = new MemoryStream();
ms = (MemoryStream)t.GetOutput(typeof(MemoryStream));
byte[] digest = t.GetDigestedOutput(new SHA256Managed());
String result = BitConverter.ToString(digest).Replace("-", String.Empty);
在我计算我得到了价值:
55B2597B0688AB1A19760B542AA70AEF4F980D7BC9D6EBCF2B741F6299C661D3
,但预期是从文件中值:
33E579FE7A9AF6C32C100E8578EBD63E54A2DF47C6849F7A4BC8BEA9E2794197
In my calculation i got the value: 55B2597B0688AB1A19760B542AA70AEF4F980D7BC9D6EBCF2B741F6299C661D3 but expected is the value from the file: 33E579FE7A9AF6C32C100E8578EBD63E54A2DF47C6849F7A4BC8BEA9E2794197
有任何你一个想法,我缺少的是什么
Have any of you an idea, what i am missing?
通过查看这里 HTTP: //www.mobilefish.com/download/sepa_xml/pain.001.001.02.xml ,看来你失踪了的xmlns:XSI =http://www.w3.org / 2001 / XML模式实例
从文档节点(可能是因为它是由一个XML解析器剥离)的命名空间。我已经修改了一点你的代码,以便它使用使用
语法和我加了命名空间,如果缺少。现在它返回正确的哈希值
By looking here http://www.mobilefish.com/download/sepa_xml/pain.001.001.02.xml, it seems you were missing the xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
namespace from the Document node (probably because it was stripped by an XML parser). I have modified a little your code so that it use the using
syntax and I added the namespace if missing. Now it returns the right hash.
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace = true;
doc.LoadXml(xml);
XmlNodeList list = doc.GetElementsByTagName("Document");
XmlElement node = (XmlElement)list[0];
node.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
string s = node.OuterXml;
// The XmlDsigC14NTransform will strip the UTF8 BOM
using (MemoryStream msIn = new MemoryStream(Encoding.UTF8.GetBytes(s)))
{
XmlDsigC14NTransform t = new XmlDsigC14NTransform(true);
t.LoadInput(msIn);
using (var hash = new SHA256Managed())
{
byte[] digest = t.GetDigestedOutput(hash);
string result = BitConverter.ToString(digest).Replace("-", String.Empty);
}
}