1

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.

1 Answer 1

11

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Quick translation to bytes: For MD5 this number of bytes is 16. For SHA-1 this number of bytes is 20. For SHA-256 this number of bytes is 32.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.