public void IntegrityCheckTest01() { try { // create new instance of ED and store xml data ED edInstance = new ED(File.ReadAllBytes(@"C:\Test Files\mydata.xml"), "text/xml"); // assign integrity checking algorithm edInstance.IntegrityCheckAlgorithm = EncapsulatedDataIntegrityAlgorithm.SHA1; edInstance.IntegrityCheck = edInstance.ComputeIntegrityCheck(); // Corrupt the data, validate integrity check should fail edInstance.Data[20] = 0; if (!edInstance.ValidateIntegrityCheck()) { Console.WriteLine("Integrity check worked!"); } Assert.AreEqual(edInstance.ValidateIntegrityCheck(), false); } catch (Exception e) { // Don't handle } }
public void IntegrityCheckTest02() { try { // create new instance of ED and store xml data ED edInstance = new ED(File.ReadAllBytes(@"C:\Test Files\mydata.xml"), "text/xml"); // assign integrity checking algorithm edInstance.IntegrityCheckAlgorithm = EncapsulatedDataIntegrityAlgorithm.SHA1; edInstance.IntegrityCheck = edInstance.ComputeIntegrityCheck(); // Do NOT corrput the data if (edInstance.ValidateIntegrityCheck()) { Console.WriteLine("Integrity check found no corruption."); } else { Console.WriteLine("Integrity check found corruption when data was not corrupted."); } Assert.AreEqual(edInstance.ValidateIntegrityCheck(), true); } catch (Exception e) { // Don't handle } }
public void EDValidateHash() { byte[] filebyte = new byte[1024]; ED edTest = new ED(filebyte, "application/pdf"); edTest.Representation = EncapsulatedDataRepresentation.B64; edTest.IntegrityCheckAlgorithm = EncapsulatedDataIntegrityAlgorithm.SHA1; edTest.IntegrityCheck = edTest.ComputeIntegrityCheck(); Assert.IsTrue(edTest.ValidateIntegrityCheck()); }
public void IntegrityCheckTest03() { try { // Create first ED instance ED edInstance1 = new ED(File.ReadAllBytes(@"C:\Test Files\testData.txt"), "text/xml"); edInstance1.IntegrityCheckAlgorithm = EncapsulatedDataIntegrityAlgorithm.SHA1; edInstance1.IntegrityCheck = edInstance1.ComputeIntegrityCheck(); // Create second ED instance ED edInstance2 = new ED(File.ReadAllBytes(@"C:\Test Files\testData.txt"), "text/xml"); edInstance2.IntegrityCheckAlgorithm = EncapsulatedDataIntegrityAlgorithm.SHA256; edInstance2.IntegrityCheck = edInstance2.ComputeIntegrityCheck(); // Corrput the data edInstance1.Data[10] = 0; edInstance2.Data[10] = 0; if (!edInstance1.ValidateIntegrityCheck() && !edInstance2.ValidateIntegrityCheck()) { Console.WriteLine("Integrity check worked!"); Console.WriteLine("Check 1:"); foreach (Byte bytes in edInstance1.IntegrityCheck) { Console.Write("{0}", bytes); } Console.WriteLine(""); Console.WriteLine("Check 2: "); foreach (Byte bytes in edInstance2.IntegrityCheck) { Console.Write("{0}", bytes); } } Assert.AreNotEqual(edInstance1.IntegrityCheck, edInstance2.IntegrityCheck); } catch (Exception e) { // Don't handle } }
public void EDValidateHash() { byte[] filebyte = new byte[1024]; ED edTest = new ED(filebyte, "application/pdf"); edTest.Representation = EncapsulatedDataRepresentation.B64; edTest.IntegrityCheckAlgorithm = EncapsulatedDataIntegrityAlgorithm.SHA1; edTest.IntegrityCheck = edTest.ComputeIntegrityCheck(); Assert.IsTrue(edTest.ValidateIntegrityCheck()); }
public void IntegrityCheckTest01() { try { // create new instance of ED and store xml data ED edInstance = new ED(File.ReadAllBytes(@"C:\Test Files\mydata.xml"), "text/xml"); // assign integrity checking algorithm edInstance.IntegrityCheckAlgorithm = EncapsulatedDataIntegrityAlgorithm.SHA1; edInstance.IntegrityCheck = edInstance.ComputeIntegrityCheck(); // Corrupt the data, validate integrity check should fail edInstance.Data[20] = 0; if (!edInstance.ValidateIntegrityCheck()) Console.WriteLine("Integrity check worked!"); Assert.AreEqual(edInstance.ValidateIntegrityCheck(), false); } catch (Exception e) { // Don't handle } }
public void IntegrityCheckTest03() { try { // Create first ED instance ED edInstance1 = new ED(File.ReadAllBytes(@"C:\Test Files\testData.txt"), "text/xml"); edInstance1.IntegrityCheckAlgorithm = EncapsulatedDataIntegrityAlgorithm.SHA1; edInstance1.IntegrityCheck = edInstance1.ComputeIntegrityCheck(); // Create second ED instance ED edInstance2 = new ED(File.ReadAllBytes(@"C:\Test Files\testData.txt"), "text/xml"); edInstance2.IntegrityCheckAlgorithm = EncapsulatedDataIntegrityAlgorithm.SHA256; edInstance2.IntegrityCheck = edInstance2.ComputeIntegrityCheck(); // Corrput the data edInstance1.Data[10] = 0; edInstance2.Data[10] = 0; if (!edInstance1.ValidateIntegrityCheck() && !edInstance2.ValidateIntegrityCheck()) { Console.WriteLine("Integrity check worked!"); Console.WriteLine("Check 1:"); foreach (Byte bytes in edInstance1.IntegrityCheck) Console.Write("{0}", bytes); Console.WriteLine(""); Console.WriteLine("Check 2: "); foreach (Byte bytes in edInstance2.IntegrityCheck) Console.Write("{0}", bytes); } Assert.AreNotEqual(edInstance1.IntegrityCheck, edInstance2.IntegrityCheck); } catch (Exception e) { // Don't handle } }
public void IntegrityCheckTest02() { try { // create new instance of ED and store xml data ED edInstance = new ED(File.ReadAllBytes(@"C:\Test Files\mydata.xml"), "text/xml"); // assign integrity checking algorithm edInstance.IntegrityCheckAlgorithm = EncapsulatedDataIntegrityAlgorithm.SHA1; edInstance.IntegrityCheck = edInstance.ComputeIntegrityCheck(); // Do NOT corrput the data if (edInstance.ValidateIntegrityCheck()) { Console.WriteLine("Integrity check found no corruption."); } else { Console.WriteLine("Integrity check found corruption when data was not corrupted."); } Assert.AreEqual(edInstance.ValidateIntegrityCheck(), true); } catch (Exception e) { // Don't handle } }
/// <summary> /// Parse an object from <paramref name="s"/> /// </summary> public object Parse(System.Xml.XmlReader s, DatatypeR2FormatterParseResult result) { // Parse base (ANY) from the stream ANYFormatter baseFormatter = new ANYFormatter(); string pathName = s is XmlStateReader ? (s as XmlStateReader).CurrentPath : s.Name; // Parse ED ED retVal = baseFormatter.Parse <ED>(s); // Now parse our data out... Attributes if (s.GetAttribute("mediaType") != null) { retVal.MediaType = s.GetAttribute("mediaType"); } if (s.GetAttribute("language") != null) { retVal.Language = s.GetAttribute("language"); } if (s.GetAttribute("compression") != null) { retVal.Compression = (EncapsulatedDataCompression?)Util.FromWireFormat(s.GetAttribute("compression"), typeof(EncapsulatedDataCompression)); } if (s.GetAttribute("integrityCheckAlgorithm") != null) { retVal.IntegrityCheckAlgorithm = Util.Convert <EncapsulatedDataIntegrityAlgorithm>(s.GetAttribute("integrityCheckAlgorithm")); } if (s.GetAttribute("value") != null) { retVal.Representation = EncapsulatedDataRepresentation.TXT; if (retVal.MediaType == null) { retVal.MediaType = "text/plain"; } retVal.Value = s.GetAttribute("value"); } // Elements and inner data #region Elements if (!s.IsEmptyElement) { // Exit markers int sDepth = s.Depth; string sName = s.Name; Encoding textEncoding = System.Text.Encoding.UTF8; s.Read(); // Read until exit condition is fulfilled while (!(s.NodeType == System.Xml.XmlNodeType.EndElement && s.Depth == sDepth && s.Name == sName)) { string oldName = s.Name; // Name try { if (s.LocalName == "thumbnail") // Format using ED { var hostResult = Host.Parse(s, typeof(ED)); result.Code = hostResult.Code; result.AddResultDetail(hostResult.Details); retVal.Thumbnail = (ED)hostResult.Structure; } else if (s.LocalName == "reference") // Format using TEL { var hostResult = Host.Parse(s, typeof(TEL)); result.Code = hostResult.Code; result.AddResultDetail(hostResult.Details); retVal.Reference = (TEL)hostResult.Structure; } else if (s.LocalName == "translation") // Translation { var hostResult = Host.Parse(s, typeof(ED)); result.Code = hostResult.Code; result.AddResultDetail(hostResult.Details); if (retVal.Translation == null) { retVal.Translation = new SET <ED>(); } retVal.Translation.Add((ED)hostResult.Structure); } else if (s.LocalName == "data") // Data { retVal.Representation = EncapsulatedDataRepresentation.B64; retVal.Data = Convert.FromBase64String(s.ReadString()); } else if (s.LocalName == "xml") // data { retVal.Representation = EncapsulatedDataRepresentation.XML; retVal.Data = textEncoding.GetBytes(s.ReadInnerXml()); } else if (s.LocalName == "integrityCheck") { retVal.IntegrityCheck = Convert.FromBase64String(s.ReadString()); } } catch (MessageValidationException e) { result.AddResultDetail(new MARC.Everest.Connectors.ResultDetail(MARC.Everest.Connectors.ResultDetailType.Error, e.Message, s.ToString(), e)); } finally { if (s.Name == oldName) { s.Read(); } } } } #endregion // Finally, the hash, this will validate the data if (!retVal.ValidateIntegrityCheck()) { result.AddResultDetail(new ResultDetail(ResultDetailType.Warning, string.Format("Encapsulated data with content starting with '{0}' failed integrity check!", retVal.ToString().PadRight(10, ' ').Substring(0, 10)), s.ToString(), null)); } // Validate baseFormatter.Validate(retVal, pathName, result); return(retVal); }
public object Parse(System.Xml.XmlReader s, DatatypeFormatterParseResult result) { // Parse base (ANY) from the stream ANYFormatter baseFormatter = new ANYFormatter(); string pathName = s is XmlStateReader ? (s as XmlStateReader).CurrentPath : s.Name; // Parse ED ED retVal = baseFormatter.Parse <ED>(s, result); // Now parse our data out... Attributes if (s.GetAttribute("representation") != null) { retVal.Representation = (EncapsulatedDataRepresentation)Util.FromWireFormat(s.GetAttribute("representation"), typeof(EncapsulatedDataRepresentation)); } if (s.GetAttribute("mediaType") != null) { retVal.MediaType = s.GetAttribute("mediaType"); } if (s.GetAttribute("language") != null) { retVal.Language = s.GetAttribute("language"); } if (s.GetAttribute("compression") != null) { retVal.Compression = (EncapsulatedDataCompression?)Util.FromWireFormat(s.GetAttribute("compression"), typeof(EncapsulatedDataCompression)); } if (s.GetAttribute("integrityCheckAlgorithm") != null) { switch (s.GetAttribute("integrityCheckAlgorithm")) { case "SHA-1": retVal.IntegrityCheckAlgorithm = EncapsulatedDataIntegrityAlgorithm.SHA1; break; case "SHA-256": retVal.IntegrityCheckAlgorithm = EncapsulatedDataIntegrityAlgorithm.SHA256; break; } } if (s.GetAttribute("integrityCheck") != null) { retVal.IntegrityCheck = Convert.FromBase64String(s.GetAttribute("integrityCheck")); } // Elements and inner data #region Elements string innerData = ""; if (!s.IsEmptyElement) { // Exit markers int sDepth = s.Depth; string sName = s.Name; s.Read(); // Read until exit condition is fulfilled while (!(s.NodeType == System.Xml.XmlNodeType.EndElement && s.Depth == sDepth && s.Name == sName)) { string oldName = s.Name; // Name try { if (s.LocalName == "thumbnail") // Format using ED { EDFormatter edFormatter = new EDFormatter(); edFormatter.Host = this.Host; retVal.Thumbnail = (ED)edFormatter.Parse(s, result); // Parse ED } else if (s.LocalName == "reference") // Format using TEL { TELFormatter telFormatter = new TELFormatter(); telFormatter.Host = this.Host; retVal.Reference = (TEL)telFormatter.Parse(s, result); } else if (s.NodeType == System.Xml.XmlNodeType.Text || s.NodeType == System.Xml.XmlNodeType.CDATA) { innerData += s.Value; } else if (!(s.NodeType == System.Xml.XmlNodeType.EndElement && s.Depth == sDepth && s.Name == sName) && (s.NodeType == System.Xml.XmlNodeType.Element || s.NodeType == System.Xml.XmlNodeType.EndElement)) { retVal.Representation = EncapsulatedDataRepresentation.XML; innerData += s.ReadOuterXml(); } } catch (MessageValidationException e) { result.AddResultDetail(new MARC.Everest.Connectors.ResultDetail(MARC.Everest.Connectors.ResultDetailType.Error, e.Message, s.ToString(), e)); } finally { if (s.Name == oldName) { s.Read(); } } } } #endregion Encoding textEncoding = System.Text.Encoding.UTF8; // Parse the innerData string into something meaningful if (innerData.Length > 0) { if (retVal.Representation == EncapsulatedDataRepresentation.B64) { retVal.Data = Convert.FromBase64String(innerData); } else { retVal.Data = textEncoding.GetBytes(innerData); } } // Finally, the hash, this will validate the data if (!retVal.ValidateIntegrityCheck()) { result.AddResultDetail(new ResultDetail(ResultDetailType.Warning, string.Format("Encapsulated data with content starting with '{0}' failed integrity check!", retVal.ToString().PadRight(10, ' ').Substring(0, 10)), s.ToString(), null)); } // Validate baseFormatter.Validate(retVal, pathName, result); return(retVal); }