如何生成验证code /多少? [英] How to generate a verification code/number?

查看:234
本文介绍了如何生成验证code /多少?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个应用程序,用户必须拨打电话,并输入验证号码与手机的键盘。

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 the number they type is correct or not. The phone system does not have access to a list of valid numbers, but instead it will validate the number against an algorithm (like a credit card number).

下面是一些要求:

  • 它必须是很难键入一个有效的随机code
  • 它必须是很难有一个有效的code,如果我做一个错字(数字换位,错误数字)
  • 我必须有可能的组合reasonnable数(比如说1M)
  • 的code必须是尽可能短,以避免用户错误

考虑到这些要求,你会怎么产生这样一个数字?

Given these requirements, how would you generate such a number ?

编辑:

@Haaked:该code的是数值,因为用户键入它与它的电话

@Haaked : The code has to be numerical, because the user type it with it's phone.

@马特B:在第一个步骤中,将code为显示在网页上,第二个步骤是调用并在code型。我不知道用户的电话号码。

@matt b : On the first step, the code is displayed on a Web page, the second step is to call and type in the code. I don't know the user's phone number.

Folowup:我发现了几个算法的检查的数字(有效期见本野趣谷歌code项目:的 checkDigits )。

Folowup : I've found several algorithms to check the validity of numbers (See this intersting Google Code project : checkDigits).

推荐答案

经过一番研究,我想我会去的 ISO 7064mod97,10 公式。看来pretty的固体,因为它是用来验证IBAN(国际银行帐户号码)。

After some research, I think I'll go with the ISO 7064 Mod 97,10 formula. It seems pretty solid as it is used to validate IBAN (International Bank Account Number).

该公式很简单:

  1. 在取号: 123456
  2. 应用下面的公式来获得2位数的校验: MOD(98 - MOD(数量* 100,97),97) => 76
  3. Concat的号码和校验,以获得$ ​​C $ C => 12345676
  4. 要验证code,确认 MOD(code,97)== 1
  1. Take a number : 123456
  2. Apply the following formula to obtain the 2 digits checksum : mod(98 - mod(number * 100, 97), 97) => 76
  3. Concat number and checksum to obtain the code => 12345676
  4. To validate a code, verify that mod(code, 97) == 1

测试:

  • MOD(12345676,97)= 1 =>好
  • MOD(21345676,97)= 50 => BAD!
  • MOD(12345678,97)= 10 => BAD!
  • mod(12345676, 97) = 1 => GOOD
  • mod(21345676, 97) = 50 => BAD !
  • mod(12345678, 97) = 10 => BAD !

显然,这种算法捕获大部分错误。

Apparently, this algorithm catches most of the errors.

另一个有趣的选项是费尔赫夫算法。它只有一个验证数字和更难以实施(相对于简单的公式段)。

Another interesting option was the Verhoeff algorithm. It has only one verification digit and is more difficult to implement (compared to the simple formula above).

这篇关于如何生成验证code /多少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆