I have a problem generating a correct hash for a given string by android studio. I have read a lot of solutions without understanding how to convert it the right way. I need the correct hash since i am making an HTTP request with it.
Here is my code in JAVA:
public String getHash(final String appSecret , final String sessionToken)throws NoSuchAlgorithmException ,UnsupportedEncodingException{
String input = sessionToken + appSecret;
MessageDigest digest = MessageDigest.getInstance("SHA-256");
digest.reset();
byte[] byteData = digest.digest(input.getBytes("UTF-8"));
StringBuffer sb = new StringBuffer();
for (int i = 0; i < byteData.length; i++){
sb.append(String.format("%02x", 0xFF & byteData[i]));
}
return sb.toString();
}
For an input like:
1130_11825_253402300799_1_1bcb4a27d42524de11325ec627b63878770a8651c0a0d8ddfc8fc06b92aea281634ff11f7d874c03851932304601439e
I need the exact output:
01a9d698f0587a25ad8ef56b0994ec0022364aff91d668a4b3a4b97c40167672
but i got a wrong output:
a60f61b5e9f832b153a91e8d2b1ffa28b9611b2d60c3669663cfe050ac8e28cc
I think my problem is how to read/print the string but i can't figure out how to correct it. I know that an online hash calculator return a correct hash. Thanks.
I modified
getHash
to take a a singleString
, I removed the call toreset()
and you to finishdigest()
. I also prefer thefor each
loop. Like,And call it like
I get