Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compute HMAC-SHA512 with secret key in java

Tags:

java

hmac

I want to exactly build 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 support up to SH256.

Also, Google doesn't give me any result for an implementation to generate a HMAC.

Does someone know a implementation?

I have this code to generate an ordinary SH256 but I guess this doesn't 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; } 
like image 786
PowerFlower Avatar asked Sep 06 '16 18:09

PowerFlower


People also ask

How do I get a secret key for HMAC?

First, enter the plain-text and the cryptographic key to generate the code. Then, you can use select the hash function you want to apply for hashing. The default is SHA-256. Then you can submit your request by clicking on the compute hash button to generate the HMAC authentication code for you.

How do I use HMAC authentication in Java?

Java provides a built-in Mac class for HMAC generating.After initializing the Mac object, we call the doFinal() method to perform the HMAC operation. This method returns a byte array containing the HMAC result. In this test, we're using the HmacSHA512 algorithm with simple string data and keys.

What is HMAC sha512?

HMACSHA512 is a type of keyed hash algorithm that is constructed from the SHA-512 hash function and used as a Hash-based Message Authentication Code (HMAC). The HMAC process mixes a secret key with the message data and hashes the result. The hash value is mixed with the secret key again, and then hashed a second time.

What key is HMAC?

You can use an HMAC KMS key with the GenerateMac and VerifyMac operations to verify the integrity and authenticity of data within AWS KMS. HMAC algorithms combine a cryptographic hash function and a shared secret key.


Video Answer


1 Answers

The simplest way can be -

private static final String HMAC_SHA512 = "HmacSHA512";  private static String toHexString(byte[] bytes) {     Formatter formatter = new Formatter();     for (byte b : bytes) {         formatter.format("%02x", b);     }     return formatter.toString(); }  public static String calculateHMAC(String data, String key)     throws SignatureException, NoSuchAlgorithmException, InvalidKeyException {     SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(), HMAC_SHA512);     Mac mac = Mac.getInstance(HMAC_SHA512);     mac.init(secretKeySpec);     return toHexString(mac.doFinal(data.getBytes())); }  public static void main(String[] args) throws Exception {     String hmac = calculateHMAC("data", "key");     System.out.println(hmac); } 

You can change the HMAC_SHA512 variable to any of the Mac algorithm and the code will work the same way.

like image 80
Dhanendra Pratap Singh Avatar answered Sep 28 '22 16:09

Dhanendra Pratap Singh