Question

My Friend told me to use "HMACSHA256" algo and i have used the below code :

SecretKey signingKey = new SecretKeySpec("123".getBytes(), "HMACSHA256");  
    Mac mac = Mac.getInstance("HMACSHA256");  
    mac.init(signingKey);  
    byte[] digest = mac.doFinal("ABCDEF".getBytes());     
    System.out.println("HMA : "+digest.length);

Just wanted to know that :

1) is the above implementation looks standard "HMACSHA256" ?

2) Output (digest) is coming as 256 bits or 16 bytes.Is this correct.

If we are using HMACSHA256 algo, how many bytes we need to expect to come as an output.

Was it helpful?

Solution

The number of output bits for HMAC digests is equal to the bits generated by the underlying algorithm.

  • For MD5 this number of bits is 128.
  • For SHA-1 this number of bits is 160.
  • For SHA-256 this number of bits if 256.

Your code looks fine and is generating the correct digest size. You can read more on cryptographic hashes here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top