I would like to generate coupon codes , e.g. AYB4ZZ2. However, I would also like to be able to mark the used coupons and limit their global number, let\'s say <
Choose a cryptographic function c. There are a few requirements on c, but for now let us take SHA1.
choose a secret key k.
Your coupon code generating function could be, for number n:
"n"+"k" (this is known as salting in password management)sprintf "%09d%s" n s, i.e. the concatenation of zero-padded n and the truncated hash s.Yes, it is trivial to guess n the number of the coupon code (but see below). But it is hard to generate another valid code.
Your requirements are satisfied:
Some comments:
n is too obvious, you can obfuscate it lightly, like base64 encoding, and alternating in the code the characters of n and s.