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?
.NET
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();
Java
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;
}
.NET output
587691443778c1da54c3fd04bb35ec68a5a7fecdJava output:
89665a8268d7d1901aba529dc8c9cea0f910c1bdThe input is a UTF-8 encoded CDA document that gets created here:
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());
UPDATE:
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.
Java reads in this:
value="21.9456" unit="kg/m²"
.net reads in this:
value="21.9456" unit="kg/m²"
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..
I had the following java code:
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] digest = md.digest(password);
and I converted it to C# as follows:
var sha1 = SHA1Managed.Create();
byte[] outputBytes = sha1.ComputeHash(password);
In order to get my hashes to match, I did NOT include the following code that you had in your example:
string hex = BitConverter.ToString(hashBytes);
hex = hex.Replace("-", "").ToLower();
精彩评论