Note that there are some explanatory texts on larger screens.

plurals
  1. POSimple Hash that is always equal between C# and Java
    text
    copied!<p>I have a C# WebService and a (Java) Android Application. Is there a SIMPLE hash function that produces the same result between these two languages? The simplest C# hash is a String.GetHashCode(), but I can't replicate it in Java. The simplest Java hash is not simple at all. And I don't know if I can replicate it exactly in C#. </p> <p>In case it's relevant, I'm hashing passwords before sending it across the internet. I'm currently using Encode64, but that's obviously not secure since we can reverse it.</p> <hr> <p>EDIT: Ok, I settled on using SHA256. Incase somebody else needs a quick solution, here are the code that I used, considering that I wanted both the C# and the Java to output the exact same string and I needed the simplest possible solution.</p> <p><strong>Java</strong> </p> <pre><code>public String Hash(String s) { StringBuffer sb = new StringBuffer(); try { MessageDigest md = MessageDigest.getInstance("SHA-256"); md.update(s.getBytes()); byte byteData[] = md.digest(); for (int i = 0; i &lt; byteData.length; i++) { sb.append(Integer.toString((byteData[i] &amp; 0xff) + 0x100, 16) .substring(1)); } } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return sb.toString(); } </code></pre> <p><strong>C#</strong></p> <pre><code>public static string Hash(String s) { HashAlgorithm Hasher = new SHA256CryptoServiceProvider(); byte[] strBytes = Encoding.UTF8.GetBytes(s); byte[] strHash = Hasher.ComputeHash(strBytes); return BitConverter.ToString(strHash).Replace("-","").ToLowerInvariant().Trim(); } </code></pre> <p>Thanks guys! :)</p>
 

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