1

I need to generate simple, 4 digits confirmation SMS code, which based on given information, e. g. username, or birthdate, whatever. So, for two identical inputs, I need two same output codes.

I can't store this code in database.

I'm writing in C#, and have been thinking about GetHashCode method, but he is highly not recommended to use anywhere. Maybe some hashing, but they have much more that 4 character string.

How can I generate such code?

Yurii N.
  • 341

2 Answers2

5

Just take the pieces of info you need, concatenate them together into a string, then hash it with a cryptographic hash function. Take the bottom 4 digits of the hash and call it a day.

whatsisname
  • 27,703
-1

based on given information, e. g. username, or birthdate, whatever. So, for two identical inputs, I need two same output

Why? The point of most SMS confirmations is to confirm the identity of the person initiating a particular action. To this end, it makes no difference what's in the code you send. So long as the receiving user types in what you sent, that's all you need. Also, generating this code deterministically based on other [personal] data is a potential security hole.

I can't store this code in database.

SMS is not a synchronous protocol.

Just because you sent a message doesn't mean that it's been received or even that the user has their phone charged up and switched on at the time. It may be several minutes before the user is in possession of the code you sent and is ready to type it back in.

Where else are you going to store it whilst you're waiting??

GetHashCode returns a unique value for every .Net object within a running application but, as soon as that object goes out of scope and is garbage collected, that value is gone and cannot be recovered.
Unless you store it somewhere.

Phill W.
  • 13,093