Is there any function for creating Hmac256 string in android?
14,114
Solution 1
Calculate the message digest with the hashing algorithm HMAC-SHA256 in the Android platform:
private void generateHashWithHmac256(String message, String key) {
try {
final String hashingAlgorithm = "HmacSHA256"; //or "HmacSHA1", "HmacSHA512"
byte[] bytes = hmac(hashingAlgorithm, key.getBytes(), message.getBytes());
final String messageDigest = bytesToHex(bytes);
Log.i(TAG, "message digest: " + messageDigest);
} catch (Exception e) {
e.printStackTrace();
}
}
public static byte[] hmac(String algorithm, byte[] key, byte[] message) throws NoSuchAlgorithmException, InvalidKeyException {
Mac mac = Mac.getInstance(algorithm);
mac.init(new SecretKeySpec(key, algorithm));
return mac.doFinal(message);
}
public static String bytesToHex(byte[] bytes) {
final char[] hexArray = "0123456789abcdef".toCharArray();
char[] hexChars = new char[bytes.length * 2];
for (int j = 0, v; j < bytes.length; j++) {
v = bytes[j] & 0xFF;
hexChars[j * 2] = hexArray[v >>> 4];
hexChars[j * 2 + 1] = hexArray[v & 0x0F];
}
return new String(hexChars);
}
This approach does not require any external dependency.
Solution 2
Try below code
public static String encode(String key, String data) throws Exception {
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
sha256_HMAC.init(secret_key);
return Hex.encodeHexString(sha256_HMAC.doFinal(data.getBytes("UTF-8")));
}
Hex.encodeHexString() to use this method, add below dependency to your app gradle.
compile 'org.apache.directory.studio:org.apache.commons.codec:1.8'
This will convert resulted string to hex string same as your php hash_hmac() funtion generates.
Related videos on Youtube
![Bikesh M](https://i.stack.imgur.com/QZqB5.jpg?s=256&g=1)
Author by
Bikesh M
I am a <?PHP developer I am a pythonDeveloper : I am a JavaDeveloper { I am an AndroidDeveloper { I am a NodeJsDeveloper {
Updated on September 16, 2022Comments
-
Bikesh M almost 2 years
Is there any function for creating Hmac256 string in android ? I am using php as my back end for my android application, in php we can create hmac256 string using the php function hash_hmac () [ ref ] is there any function like this in Android
Please help me.
-
Michael over 8 yearsHave you tried using the
Mac
class with"Hmac256"
as the algorithm name? -
Michael over 8 yearsI'm sure you can find plenty of examples of how to use that class if you search around.
-
-
f.trajkovski over 5 yearsI am getting this using your method: error: cannot find symbol return Hex.encodeHexString(sha256_HMAC.doFinal(data.getBytes("UTF-8")));
-
Jesse Buss about 5 yearsMake sure you're importing the correct Hex object. Should be
org.apache.commons.codec.binary.Hex
(as specified in the dependency above) notcom.google.android.gms.common.util.Hex
-
Kannan_SJD almost 5 yearsWhat does this mean final char[] hexArray = "0123456789abcdef".toCharArray();
-
Ryan Amaral about 3 yearsIs an array of
char
that is initialised with the hexadecimal numeral system. The word 'hexadecimal' means sixteen because this type of digital numbering system uses 16 different digits from 0-to-9, and A-to-F.