title: Compute SHA256 Hash in Android/Java and C#
body: <p>I am trying to generate a SHA256 hash in android, that I then pass to an ASP.NET Web API web service and compare the hash there. As such, I need to construct a hash in Android, that given the same inputs in ASP.NET will generate an equivalent hash. I'm pulling my hair out trying to figure out what I'm doing wrong.</p>
  <p>Here's the Android code:</p>
  <pre><code>public String computeHash(String input) throws NoSuchAlgorithmException{
      MessageDigest digest = MessageDigest.getInstance(&quot;SHA-256&quot;);
      digest.reset();
      try{
        digest.update(input.getBytes(&quot;UTF-8&quot;));
      } catch (UnsupportedEncodingException e){
        e.printStackTrace();
      }
      
      byte[] byteData = digest.digest(input.getBytes());
      StringBuffer sb = new StringBuffer();
  
      for (int i = 0; i &lt; byteData.length; i++){
        sb.append(Integer.toString((byteData[i] &amp; 0xff) + 0x100, 16).substring(1));
      }
      return sb.toString();
  }
  </code></pre>
  <p>Here's the C# code for the server:</p>
  <pre><code>    private static string ComputeHash(string input, HashAlgorithm algorithm)
      {
  
          Byte[] inputBytes = Encoding.UTF8.GetBytes(input);
          Byte[] hashedBytes = algorithm.ComputeHash(inputBytes);
  
          StringBuilder sb = new StringBuilder();
  
          for (int i = 0; i &lt; hashedBytes.Length; i++)
          {
              sb.Append(String.Format(&quot;{0:x2}&quot;, hashedBytes[i]));
          }
  
          return sb.ToString();
      }
  </code></pre>