N-rG N-rG - 3 months ago 40
Java Question

Compute HMAC-SHA512 with secret key in java

i want to build excatly a function which produces a HMAC with a secret key like this site provides:

http://www.freeformatter.com/hmac-generator.html

The java 8 lib only provides MessageDigest and KeyGenerator which both only supports up to SH256.

Also google doesnt give me any result to an implementation to generate a HMAC.

Does someone know a implementation?

I have this code to generate an ordinary SH256 but i guess this doesnt help me much:

public static String get_SHA_512_SecurePassword(String passwordToHash) throws Exception {
String generatedPassword = null;

MessageDigest md = MessageDigest.getInstance("SHA-512");
byte[] bytes = md.digest(passwordToHash.getBytes("UTF-8"));
StringBuilder sb = new StringBuilder();
for (int i = 0; i < bytes.length; i++) {
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
System.out.println(generatedPassword);
return generatedPassword;
}

Answer

Hope this helps:

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

public class Test1 {
public static void main(String[] args) {
    Mac sha512_HMAC = null;
    String result = null;
    String key =  "Welcome1";

    try{
        byte [] byteKey = key.getBytes("UTF-8");
        final String HMAC_SHA256 = "HmacSHA512";
        sha512_HMAC = Mac.getInstance(HMAC_SHA256);      
        SecretKeySpec keySpec = new SecretKeySpec(byteKey, HMAC_SHA256);
        sha512_HMAC.init(keySpec);
        byte [] mac_data = sha512_HMAC.
         doFinal("My message".getBytes("UTF-8"));
        //result = Base64.encode(mac_data);
        result = bytesToHex(mac_data);
        System.out.println(result);
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchAlgorithmException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        System.out.println("Done");
    }
}

public static String bytesToHex(byte[] bytes) {
    final  char[] hexArray = "0123456789ABCDEF".toCharArray();
    char[] hexChars = new char[bytes.length * 2];
    for ( int j = 0; j < bytes.length; j++ ) {
        int v = bytes[j] & 0xFF;
        hexChars[j * 2] = hexArray[v >>> 4];
        hexChars[j * 2 + 1] = hexArray[v & 0x0F];
    }
    return new String(hexChars);
}

}

For converting from byte array to hex refer this stackoverflow answer : here