Assume we should apply an hmac sha256 on different messages made of incremental numbers (Ex. 1 to 1 billion). Does it make sense in terms of security to apply a different fixed key for each message?

message 1 key X1

message 2 key X2


message n key Xn

Will applying a key for each message impact the collision rate? If yes, will it increase or decrease the collision possibility?

The aim is to generate unique deterministic tokens starting from sequential values as source domain.

  • $\begingroup$ There is uuid for this. There are deterministic modes that prevents the collision. $\endgroup$ – kelalaka Aug 22 '20 at 7:40

No, it is not useful to change the key between invocations of HMAC.

That would not change the expected probability of collision, which remains about $n(n-1)/2^{257}$ for $n$ values under a model of HMAC-SHA-256 as a PRF. For $n=2^{40}$ (over a million million, the UK bilion), that's less than $2^{-177}$, the probability of 177 consecutive tails with a fair coin, that is negligible in practice.

That probability can be brought to zero using encryption of a counter with a block cipher.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.