title: How to convert php function to kotlin android?
body: <p>I have a project I'm working on that uses an API for it request, but in order to preform them I need to generate the token first. </p>
  
  <p>Before the API was update everything was working, after the update I don't know how to adjust my code to make it work again.</p>
  
  <p>This was the code that worked before the update <strong>(Android | Kotlin)</strong>:</p>
  
  <pre><code>fun hmacHash(str: String, secret: String): String {
      val sha256HMAC = Mac.getInstance("HmacSHA256")
  
      val secretKey = SecretKeySpec(secret.toByteArray(), "HmacSHA256")
      sha256HMAC.init(secretKey)
  
      return convertToHex(sha256HMAC.doFinal(str.toByteArray()))
  }
  
  fun convertToHex(data: ByteArray): String {
      val buf = StringBuilder()
      for (b in data) {
          var halfbyte = (b.toInt() shr 4) and (0x0F.toByte()).toInt()
          var two_halfs = 0
          do {
              buf.append(if (halfbyte in 0..9) ('0'.toInt() + halfbyte).toChar() else ('a'.toInt() + (halfbyte - 10)).toChar())
              halfbyte = (b and 0x0F).toInt()
          } while (two_halfs++ &lt; 1)
      }
      return buf.toString()
  }
  </code></pre>
  
  <p>Which was equivalent to this <strong>PHP code</strong>:</p>
  
  <pre><code>hash_hmac('sha256', $string, $privateKey);
  </code></pre>
  
  <p>But now after the update the php code looks like this:</p>
  
  <pre><code>hash_hmac('sha256', $string, hex2bin($privateKey));
  </code></pre>
  
  <p>And I don't know how to adjust my code to make it work with this new change. </p>