Каноникализация (приведение XML к канонической форме)
ClincalDocument
static void hash_from_file_ClinicalDocument(string _filenameXML, string _filenameTXT)
{
StreamWriter sw = new StreamWriter(_filenameTXT);
Gost3411 gost3411 = new Gost3411CryptoServiceProvider();
XmlDocument doc = new XmlDocument();
doc.Load(new XmlTextReader(_filenameXML));
XmlElement root = doc.DocumentElement; // Полный xml файл
// Console.WriteLine(root.InnerXml); Console.ReadKey();
// Ищем тег Body, с нэмспейсом
String ClinicalDocumentXML = root.OuterXml; // Получаем полное содержимое тега ClinicalDocument
sw.Write("ClinicalDocumentFromFile= "); sw.WriteLine(ClinicalDocumentXML); // Пишем исходное body в файл
byte[] result = gost3411.ComputeHash(Encoding.UTF8.GetBytes(ClinicalDocumentXML));
sw.Write("HashClinicalDocument= "); sw.WriteLine(Convert.ToBase64String(result)); // Пишем хэш исходного body
// Приводим тэг ClinicalDocument к канонической форме
XmlNode ClinicalDocumentAll = doc.DocumentElement;
// sw.Write("ClinicalDocumentAll= "); sw.WriteLine(ClinicalDocumentAll.OuterXml);
String ClinicalDocumentCan = Canonicalization(ClinicalDocumentAll);
sw.Write("ClinicalDocumentCan= "); sw.WriteLine(ClinicalDocumentCan);
result = gost3411.ComputeHash(Encoding.UTF8.GetBytes(ClinicalDocumentCan));
sw.Write("HashClinicalDocumentCan= "); sw.WriteLine(Convert.ToBase64String(result));
export_can_node(ClinicalDocumentCan, "doc/ClinicalDocument_cann"); // экспорт результата каноникализации в файл
sw.WriteLine("********************************");
// Console.WriteLine(signedInfo.InnerXml); Console.ReadKey();
sw.Close();
return;
}
public static string Canonicalization(XmlNode node)
{
var nodeStream = new MemoryStream();
XmlWriter writer = XmlWriter.Create(nodeStream);
node.WriteTo(writer);
writer.Flush();
nodeStream.Position = 0;
var transform = new XmlDsigExcC14NTransform();
transform.LoadInput(nodeStream);
var outputStream = (MemoryStream)transform.GetOutput(typeof(Stream));
return new StreamReader(outputStream).ReadToEnd();
//C# метод канокализации не добавляет в XPath неймсппейс
// result = s.Replace("<XPath>", "<XPath xmlns:dsig=\"http://www.w3.org/2000/09/xmldsig#\">");
//todo: Поверить после! Тут они не попадаются, проверить нечем.
}
public static string export_can_node(string node_can, string filename_can)
{
XmlDocument doc_ = new XmlDocument();
doc_.LoadXml(node_can);
using (XmlTextWriter xmltw = new XmlTextWriter(filename_can+".xml",
new UTF8Encoding(false)))
{
doc_.WriteTo(xmltw);
}
return null;
}