# HMAC-SHA256 vs HMAC-SHA512 for JWT API authentication

Which algorithm is recommended to use when encoding / decoding JWT web application tokens? Is using HMAC-SHA256 enough or would using HMAC-SHA512 provide more security? And on 64bit machines, is it true that HMAC-SHA512 is faster than HMAC-SHA256?

Both algorithms provide plenty of security, near the output size of the hash. So even though HMAC-512 will be stronger, the difference is inconsequential. If this ever breaks it is because the algorithm itself is broken and as both hash algorithms are related, it is likely that both would be in trouble. However, no such attack is known and the HMAC construct itself appears to be very strong indeed.

SHA-512 is indeed faster than SHA-256 on 64 bit machines. It may be that the overhead provided by the block size of SHA-512 is detrimental to HMAC-ing short length message sizes. But you can speedup larger messages sizes using HMAC-SHA-512 for sure. Then again, SHA-256 is plenty fast itself, and is faster on 32 bit and lower machines, so I'd go for HMAC-SHA-256 if lower end machines could be involved.

Note that newer x86 processors also contain SHA-1 and SHA-256 accelerator hardware, so that may shift the speed advantage back into SHA-256's favor compared to SHA-512.

• Note: these are just recommendations for the HMAC configuration; I'm not an expert on JWT web tokens. – Maarten - reinstate Monica Dec 10 '17 at 0:00
• Related question, what is the limit as to how much custom data can be stored (encoded) in a HMAC-512 token, like email, user id, browser agent, OS information, IP, etc? How is it measured, in number of maximum data characters? – W.M. Dec 10 '17 at 15:40
• @W.M. There is no maximum. A hash doesn't store data, it just hashes data to a computationally unique value, indistinguishable from random (to one not knowing the input data - of course). Well, officially there are limits to the input, but even the smaller hashes are limited to $2^{64}$ bits (more than \$10^{18} bytes), so the limit is out of reach for any kind of standard computer even to calculate. To give some more of an idea see the answer here – Maarten - reinstate Monica May 5 '18 at 12:26