title: Android Java and PHP HASH HMAC SHA256 different result
body: <p>I saw some same questions in stack-overflow but it doesn't find answer.</p>
  
  <p>Android Java:</p>
  
  <pre><code>import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.util.Base64;
  import android.util.Log;
  import javax.crypto.Mac;
  import javax.crypto.spec.SecretKeySpec;
  
  private String hash_hmac(String str, String secret) throws Exception{
      Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
      byte[] string = str.getBytes();
      String stringInBase64 = Base64.encodeToString(string, Base64.DEFAULT);
      SecretKeySpec secretKey = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
      sha256_HMAC.init(secretKey);
      String hash = Base64.encodeToString(sha256_HMAC.doFinal(stringInBase64.getBytes()), Base64.DEFAULT);
      return hash;
  }
  
      String str = "1234";
      String key = "1234";
  
      try {
  
          Log.d("HMAC:", hash_hmac(str,key));
  
      } catch (Exception e) {
          Log.d("HMAC:","stop");
          e.printStackTrace();
      }
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>UHWlHiPrOxxncJTagnlvVcln3xKZ1PKaojFeTMDLXws=
  </code></pre>
  
  <p>And in <strong>PHP</strong> code:</p>
  
  <pre><code>$str=base64_encode('1234');
  $key='1234';
  print(base64_encode(hash_hmac('sha256', $str, $key,true)));
  </code></pre>
  
  <p>Result:</p>
  
  <pre><code>FBp7PnSG4MQNgcFWAjjlkxNdiqivrmNfnRZur1Dko2Q=
  </code></pre>
  
  <p>As you can see, i have different results. What i do wrong?</p>
  
  <p>PS. I edit code, as we say, but it not helped me. Please, explain, why i have different results?</p>