title: Java MD5 Hash Not Matching .NET Hash
body: <p>I have a webservice that is written in C# handling some validation of values.  In it I need to check a MD5 hash generated in the calling Java client.</p>
  
  <p>The Java client generates the hash in this manner</p>
  
  <pre><code>Charset utf8Charset = Charset.forName("UTF-8");
  
  byte[] bytesOfPhrase = phrase.getBytes(utf8Charset);
  MessageDigest md = MessageDigest.getInstance("MD5");
  
  byte[] thedigest = md.digest(bytesOfPhrase);
  this._AuthenticationToken = new String(thedigest, utf8Charset);
  </code></pre>
  
  <p>The C# webservice generates its has in this manner:</p>
  
  <pre><code>private static string HashString(string toHash)
  {
      MD5CryptoServiceProvider md5Provider = new MD5CryptoServiceProvider();
  
      byte[] hashedBytes = md5Provider.ComputeHash(_StringEncoding.GetBytes(toHash));
      return Convert.ToBase64String(hashedBytes);
  }
  </code></pre>
  
  <p>I've tried several charsets in the Java code, but none of them produce a string that is anywhere similar to the Java produced string.  Using hard coded values that are the same during every call (meaning that I've hardcoded the parameters so the hashes should match) still produces an odd Java string.</p>
  
  <p>C# Example of hashed values:</p>
  
  <blockquote>
    <p>6wM7McddLBjofdFJ3rU6/g==</p>
  </blockquote>
  
  <p>I'd post the example of the string Java produces, but it has some very odd characters that I do not think I can paste in here.</p>
  
  <p>What am I doing wrong?</p>