title: SHA256 in PHP & Java
body: <p>I'm porting some Java code to PHP code. In Java I have a hash SHA256 code as below:</p>
  
  <pre><code>public static String hashSHA256(String input)
          throws NoSuchAlgorithmException {
      MessageDigest mDigest = MessageDigest.getInstance("SHA-256");
  
  byte[] shaByteArr = mDigest.digest(input.getBytes(Charset.forName("UTF-8")));
      StringBuilder hexStrBuilder = new StringBuilder();
      for (int i = 0; i &lt; shaByteArr.length; i++) {
          hexStrBuilder.append(Integer.toHexString(0xFF &amp; shaByteArr[i]));
      }
  
      return hexStrBuilder.toString();
  }
  </code></pre>
  
  <p>In PHP, I hash as below:</p>
  
  <pre><code>$hash = hash("sha256", utf8_encode($input));
  </code></pre>
  
  <p>I run the sample code with both <strong><em>input = "test"</em></strong>. However, I got 2 hash strings which are not the same:</p>
  
  <pre><code>Java: 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2bb822cd15d6c15b0f0a8
  PHP: 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
  </code></pre>
  
  <p>Can someone explain to me why and how to get them match each other? Please note that I cannot modify the Java implementation code, only to modify PHP.</p>
  
  <p>Really appreciate!</p>