0

i'm already search for this problem in so many articles and developer environment, but i'd stuck.

I'm searching how to convert String into HMAC-SHA256 in Android Java.

My PHP code is

$data = "Help";
$secret = "A5D!@#";
$key = hash_hmac('sha256', $data, $secret);
echo $key;

// the key is = ede672d9979a804d7a480e511ba556d506d41a1af5959155db208a0416093c7c

I'm really confused about how to convert string to HMAC SHA256 in Android. Is it possible to do in Android (Java Based) ?

Thanks for read this question, hopefully someone can help me. I really appreciate it, if there is someone can help me to solved this problem.

#XOXO

1 Answers1

1

Yes, this is possible in android(java). Is $data = "Help" your string?, I am not sure. If so then, you can follow the code, this is more or less similar to your PHP code.

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;

    ... ... ...

    String data = "Hello";
    String secret = "A5D!@#";
     

    Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
    SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(),"HmacSHA256");
    sha256_HMAC.init(secret_key);

    String hash = Base64.encodeBase64String(sha256_HMAC.doFinal(data.getBytes()));
Md Kawser Habib
  • 1,966
  • 2
  • 10
  • 25