How to generate a verification code/number?

前端 未结 9 2049
慢半拍i
慢半拍i 2020-12-07 16:51

I\'m working on an application where users have to make a call and type a verification number with the keypad of their phone.

I would like to be able to detect if th

9条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-07 17:41

    For 1M combinations you'll need 6 digits. To make sure that there aren't any accidentally valid codes, I suggest 9 digits with a 1/1000 chance that a random code works. I'd also suggest using another digit (10 total) to perform an integrity check. As far as distribution patterns, random will suffice and the check digit will ensure that a single error will not result in a correct code.

    Edit: Apparently I didn't fully read your request. Using a credit card number, you could perform a hash on it (MD5 or SHA1 or something similar). You then truncate at an appropriate spot (for example 9 characters) and convert to base 10. Then you add the check digit(s) and this should more or less work for your purposes.

提交回复
热议问题