As nonce is supposed to be random and used only one time, when receiving a nonce, I need to check it with all past nonce records. That means I need a database to store every nonce I had before. Of course I can reduce the database size by limiting the life circle for nonces. But I am curious about how people handle this issue in practice. Is there any efficient way to store and verify a nonce? Thanks.


First of all, As nonce is supposed to be random is not, strictly speaking, true. Nonce in general is only required to be unique.

It is, however, often convenient to generate nonce as (pseudo-)random bytes. In this case and if the nonce is sufficiently big (say, 12+ bytes) there is no need to check against previous nonces as you will never encounter a repeat in your life.

|improve this answer|||||
  • Thank you for your input. But I think nonce is used for preventing reply attack so that we do need to check the nonce if it appeared before. – Sissi Aug 14 '15 at 14:42
  • 1
    If standard csprng of your system happens to produce same 12-byte chunks you have a bigger problem then repeating nonce. – Cthulhu Aug 14 '15 at 14:46
  • The mathematical demonstration that "there is no need to check against previous nonces as you will never encounter a repeat in your life" is here. – João Pimentel Ferreira Jul 30 '18 at 15:19

You can store nonces in any way you want, e.g. in a database; also note that you don't need to store past nonces longer than their validity time. In fact, a nonce should be timestamped, and valid only during a limited time since its creation. For this, all systems (nonce producer, client, and server) need to be time synchronized.

|improve this answer|||||
  • Thanks. But I have another question about this: time synchronization. For example, nonce is used to prevent against reply attack, and timestamps may also have this benefit. Then why do we use nonce? I thought it was because the pricy time synchronization. – Sissi Aug 14 '15 at 14:41
  • You do not need databases to store nonces: stackoverflow.com/a/50088697/1243247 That is a non-cense :) – João Pimentel Ferreira Jul 30 '18 at 15:20

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.