From 3f8ca2b0efc9453b58daea31e0ed5d70712040a5 Mon Sep 17 00:00:00 2001 From: Markus Quaritsch <markus.quaritsch@tugraz.at> Date: Wed, 3 May 2017 17:07:17 +0200 Subject: [PATCH] comparison of two different implementations to create the hash, both yield the same result! --- VECTO.sln | 11 +++ VectoCommon/VectoHashing/VectoHashing.csproj | 1 + VectoCommon/VectoHashing/XmlHashProvider.cs | 8 +- VectoCommon/VectoHashing/XmlHashTest.cs | 38 ++++++++ .../Properties/AssemblyInfo.cs | 36 +++++++ .../Testdata/XML/simple_document.xml | 6 ++ VectoCommon/VectoHashingTest/UnitTest1.cs | 27 ++++++ .../VectoHashingTest/VectoHashingTest.csproj | 96 +++++++++++++++++++ 8 files changed, 219 insertions(+), 4 deletions(-) create mode 100644 VectoCommon/VectoHashing/XmlHashTest.cs create mode 100644 VectoCommon/VectoHashingTest/Properties/AssemblyInfo.cs create mode 100644 VectoCommon/VectoHashingTest/Testdata/XML/simple_document.xml create mode 100644 VectoCommon/VectoHashingTest/UnitTest1.cs create mode 100644 VectoCommon/VectoHashingTest/VectoHashingTest.csproj diff --git a/VECTO.sln b/VECTO.sln index e454383d41..2d1ee16364 100644 --- a/VECTO.sln +++ b/VECTO.sln @@ -58,6 +58,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VectoCore BugReportTests", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VectoHashing", "VectoCommon\VectoHashing\VectoHashing.csproj", "{B673E12F-D323-4C4C-8805-9915B2C72D3D}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "VectoHashingTest", "VectoCommon\VectoHashingTest\VectoHashingTest.csproj", "{760C1C5B-A767-463E-BA85-F0BCFC23A550}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -236,6 +238,14 @@ Global {B673E12F-D323-4C4C-8805-9915B2C72D3D}.Release|Any CPU.Build.0 = Release|Any CPU {B673E12F-D323-4C4C-8805-9915B2C72D3D}.Release|x64.ActiveCfg = Release|Any CPU {B673E12F-D323-4C4C-8805-9915B2C72D3D}.Release|x86.ActiveCfg = Release|Any CPU + {760C1C5B-A767-463E-BA85-F0BCFC23A550}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {760C1C5B-A767-463E-BA85-F0BCFC23A550}.Debug|Any CPU.Build.0 = Debug|Any CPU + {760C1C5B-A767-463E-BA85-F0BCFC23A550}.Debug|x64.ActiveCfg = Debug|Any CPU + {760C1C5B-A767-463E-BA85-F0BCFC23A550}.Debug|x86.ActiveCfg = Debug|Any CPU + {760C1C5B-A767-463E-BA85-F0BCFC23A550}.Release|Any CPU.ActiveCfg = Release|Any CPU + {760C1C5B-A767-463E-BA85-F0BCFC23A550}.Release|Any CPU.Build.0 = Release|Any CPU + {760C1C5B-A767-463E-BA85-F0BCFC23A550}.Release|x64.ActiveCfg = Release|Any CPU + {760C1C5B-A767-463E-BA85-F0BCFC23A550}.Release|x86.ActiveCfg = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -258,5 +268,6 @@ Global {2C58BA97-2954-4D19-920F-A24B78FC80A4} = {D29E62A2-CF08-469F-B29F-1B25DCB35386} {7C364099-9B85-473A-8A42-BBEBE4798FF5} = {CC93EA68-F3FE-4BCB-9292-1101F94A4D09} {B673E12F-D323-4C4C-8805-9915B2C72D3D} = {73A5BF70-6168-456F-95E5-A1402BFA488C} + {760C1C5B-A767-463E-BA85-F0BCFC23A550} = {73A5BF70-6168-456F-95E5-A1402BFA488C} EndGlobalSection EndGlobal diff --git a/VectoCommon/VectoHashing/VectoHashing.csproj b/VectoCommon/VectoHashing/VectoHashing.csproj index a0f3da0a2d..0c743eed72 100644 --- a/VectoCommon/VectoHashing/VectoHashing.csproj +++ b/VectoCommon/VectoHashing/VectoHashing.csproj @@ -42,6 +42,7 @@ <ItemGroup> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="XmlHashProvider.cs" /> + <Compile Include="XmlHashTest.cs" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. diff --git a/VectoCommon/VectoHashing/XmlHashProvider.cs b/VectoCommon/VectoHashing/XmlHashProvider.cs index bc25160ed3..017157e92b 100644 --- a/VectoCommon/VectoHashing/XmlHashProvider.cs +++ b/VectoCommon/VectoHashing/XmlHashProvider.cs @@ -4,7 +4,7 @@ using System.Security.Cryptography; using System.Security.Cryptography.Xml; using System.Xml; -namespace VectoDB +namespace TUGraz.VectoHashing { public class XmlHashProvider { @@ -17,10 +17,10 @@ namespace VectoDB public string ComputeHash(string xpath) { - var xml = new XmlDocument() { PreserveWhitespace = true }; + var xml = new XmlDocument(); var manager = new XmlNamespaceManager(xml.NameTable); - manager.AddNamespace("ved", "urn:tugraz:ivt:VectoAPI:EngineeringDefinitions:v0.6"); - manager.AddNamespace("tns", "urn:tugraz:ivt:VectoAPI:EngineeringInput:v0.6"); + manager.AddNamespace("ved", "urn:tugraz:ivt:VectoAPI:EngineeringDefinitions:v0.8"); + manager.AddNamespace("tns", "urn:tugraz:ivt:VectoAPI:EngineeringInput:v0.8"); using (var fs = new FileStream(File, FileMode.Open)) { using (var sr = new StreamReader(fs)) { diff --git a/VectoCommon/VectoHashing/XmlHashTest.cs b/VectoCommon/VectoHashing/XmlHashTest.cs new file mode 100644 index 0000000000..a4070c10fe --- /dev/null +++ b/VectoCommon/VectoHashing/XmlHashTest.cs @@ -0,0 +1,38 @@ +using System; +using System.Security.Cryptography; +using System.Security.Cryptography.Xml; +using System.Text; +using System.Xml; + +namespace TUGraz.VectoHashing +{ + public class XmlHashTest + { + public string ComputeHash(XmlDocument doc) + { + var signedXml = new SignedXml(doc); + var reference = new Reference("#elemID") { + DigestMethod = "http://www.w3.org/2001/04/xmlenc#sha256" + }; + reference.AddTransform(new XmlDsigC14NTransform()); + //reference.AddTransform(new XmlDsigVectoTransform()); + + signedXml.AddReference(reference); + signedXml.ComputeSignature(HMAC.Create()); + var xmlDigitalSignature = reference.GetXml(); + + var sig = doc.CreateElement("Signature"); + sig.AppendChild(doc.ImportNode(xmlDigitalSignature, true)); + + doc.DocumentElement.AppendChild(sig); + if (doc.FirstChild is XmlDeclaration) { + doc.RemoveChild(doc.FirstChild); + } + var xmltw = new XmlTextWriter("simple_document_hashed.xml", new UTF8Encoding(false)); + doc.WriteTo(xmltw); + xmltw.Close(); + var references = signedXml.SignedInfo.References; + return Convert.ToBase64String(((Reference)references[0]).DigestValue); + } + } +} \ No newline at end of file diff --git a/VectoCommon/VectoHashingTest/Properties/AssemblyInfo.cs b/VectoCommon/VectoHashingTest/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000..455800ff08 --- /dev/null +++ b/VectoCommon/VectoHashingTest/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("VectoHashingTest")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("VectoHashingTest")] +[assembly: AssemblyCopyright("Copyright © 2017")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("bd51185f-fccf-42e3-b3e1-c59fcd74134a")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/VectoCommon/VectoHashingTest/Testdata/XML/simple_document.xml b/VectoCommon/VectoHashingTest/Testdata/XML/simple_document.xml new file mode 100644 index 0000000000..3fdd9dfbf5 --- /dev/null +++ b/VectoCommon/VectoHashingTest/Testdata/XML/simple_document.xml @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="UTF-8"?> +<root> + <element id="elemID"> + <message>hello world</message> + </element> +</root> diff --git a/VectoCommon/VectoHashingTest/UnitTest1.cs b/VectoCommon/VectoHashingTest/UnitTest1.cs new file mode 100644 index 0000000000..b60fcf1667 --- /dev/null +++ b/VectoCommon/VectoHashingTest/UnitTest1.cs @@ -0,0 +1,27 @@ +using System; +using System.Xml; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using TUGraz.VectoHashing; + +namespace VectoHashingTest +{ + [TestClass] + public class UnitTest1 + { + public const string SimpleXML = @"Testdata\XML\simple_document.xml"; + + [TestMethod] + public void TestCompareHashing() + { + var hasher1 = new XmlHashProvider(SimpleXML); + var hash1 = hasher1.ComputeHash("//*[@id='elemID']"); + + var doc = new XmlDocument(); + doc.Load(SimpleXML); + var hasher2 = new XmlHashTest(); + var hash2 = hasher2.ComputeHash(doc); + + Assert.AreEqual(hash1, hash2); + } + } +} \ No newline at end of file diff --git a/VectoCommon/VectoHashingTest/VectoHashingTest.csproj b/VectoCommon/VectoHashingTest/VectoHashingTest.csproj new file mode 100644 index 0000000000..dacb575089 --- /dev/null +++ b/VectoCommon/VectoHashingTest/VectoHashingTest.csproj @@ -0,0 +1,96 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{760C1C5B-A767-463E-BA85-F0BCFC23A550}</ProjectGuid> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>VectoHashingTest</RootNamespace> + <AssemblyName>VectoHashingTest</AssemblyName> + <TargetFrameworkVersion>v4.5</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + <ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion> + <VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath> + <ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath> + <IsCodedUITest>False</IsCodedUITest> + <TestProjectType>UnitTest</TestProjectType> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="System" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <Choose> + <When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'"> + <ItemGroup> + <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" /> + </ItemGroup> + </When> + <Otherwise> + <ItemGroup> + <Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework" /> + </ItemGroup> + </Otherwise> + </Choose> + <ItemGroup> + <Compile Include="UnitTest1.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup /> + <ItemGroup> + <Content Include="Testdata\XML\simple_document.xml"> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </Content> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\VectoHashing\VectoHashing.csproj"> + <Project>{B673E12F-D323-4C4C-8805-9915B2C72D3D}</Project> + <Name>VectoHashing</Name> + </ProjectReference> + </ItemGroup> + <Choose> + <When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'"> + <ItemGroup> + <Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> + <Private>False</Private> + </Reference> + <Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> + <Private>False</Private> + </Reference> + <Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> + <Private>False</Private> + </Reference> + <Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"> + <Private>False</Private> + </Reference> + </ItemGroup> + </When> + </Choose> + <Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" /> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project> \ No newline at end of file -- GitLab