Note that there are some explanatory texts on larger screens.

plurals
  1. POJava MessageDigest and .NET SHA1Managed - hashes not matching
    primarykey
    data
    text
    <p>I have some .net code that generates a SHA1 hash on an xml document and need it to match a SHA1 hash generated on the same xml document in java code. The xml doc is sent to the java system and they generate a hash and match against the one i send to verify they are getting the document I intended them to. Below are the snippets in use for this and they both consistently generate the same different hashes. Any ideas why the following code would not generate the same hash?</p> <p>.NET</p> <pre><code> String fileName = @"D:\Projects\CHIEP\hasherror\cdadoc.xml"; byte[] buff = null; FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); long numBytes = new FileInfo(fileName).Length; buff = br.ReadBytes((int)numBytes); HashAlgorithm hash = new SHA1Managed(); byte[] hashBytes = hash.ComputeHash(buff); string hex = BitConverter.ToString(hashBytes); hex = hex.Replace("-", "").ToLower(); </code></pre> <p>Java</p> <pre><code>public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException { MessageDigest md; md = MessageDigest.getInstance("SHA-1"); byte[] sha1hash = new byte[40]; md.update(text.getBytes("UTF-8"), 0, text.length()); sha1hash = md.digest(); //String converted = convertToHex(sha1hash); String converted = getHexString(sha1hash); return converted; } </code></pre> <p>.NET output <br/> 587691443778c1da54c3fd04bb35ec68a5a7fecd</p> <p>Java output:<br/> 89665a8268d7d1901aba529dc8c9cea0f910c1bd</p> <p>The input is a UTF-8 encoded CDA document that gets created here:</p> <pre><code> XmlSerializer serializer = new XmlSerializer(obj.GetType()); System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(); XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, new UTF8Encoding(false)); XmlDocument xmlDoc = new XmlDocument(); serializer.Serialize(xmlTextWriter, obj); memoryStream = (System.IO.MemoryStream)xmlTextWriter.BaseStream; String xml = UTF8Encoding.UTF8.GetString(memoryStream.ToArray()); </code></pre> <p>UPDATE:</p> <p>Getting close to a solution. I found in the document there is a character that is being interpreted differently in the java than in the .net code. </p> <p>Java reads in this:</p> <pre><code>value="21.9456" unit="kg/m²" </code></pre> <p>.net reads in this:</p> <pre><code>value="21.9456" unit="kg/m²" </code></pre> <p>If I open in the xml editor of my choice it looks like the what .net reads in. I suspect it has something to do with java doing a conversion and .net simply assuming..</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload