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

<p>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).</p>

<p>Here are some of the requirements :</p>

<ul>
<li>It must be difficult to type a valid random code </li>
<li>It must be difficult to have a valid code if I make a typo (transposition of digits, wrong digit)</li>
<li>I must have a reasonable number of possible combinations (let's say 1M)</li>
<li>The code must be as short as possible, to avoid errors from the user</li>
</ul>

<p>Given these requirements, how would you generate such a number?</p>

<p>EDIT :</p>

<p>@Haaked: The code has to be numerical because the user types it with its phone.</p>

<p>@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.</p>

<p>Followup : I've found several algorithms to <em>check</em> the validity of numbers (See this interesting Google Code project : <a href="http://code.google.com/p/checkdigits/wiki/CheckDigitSystems" rel="nofollow noreferrer">checkDigits</a>).</p>

[{'answer_id': 46241, 'author': 'Kyle Cronin', 'author_id': 658, 'author_profile': 'https://Stackoverflow.com/users/658', 'pm_score': 2, 'selected': False, 'text': '<p>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 <a href="http://en.wikipedia.org/wiki/Check_digit" rel="nofollow noreferrer">integrity check</a>. 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.</p>\n\n<p><strong>Edit:</strong> 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.</p>\n'}, {'answer_id': 46243, 'author': 'Chris Upchurch', 'author_id': 2600, 'author_profile': 'https://Stackoverflow.com/users/2600', 'pm_score': 0, 'selected': False, 'text': "<blockquote>\n  <ul>\n  <li>I must have a reasonnable number of possible combinations (let's say 1M)</li>\n  <li>The code must be as short as possible, to avoid errors from the user</li>\n  </ul>\n</blockquote>\n\n<p>Well, if you want it to have at least one million combinations, then you need at least six digits.  Is that short enough?</p>\n"}, {'answer_id': 46262, 'author': 'matt b', 'author_id': 4249, 'author_profile': 'https://Stackoverflow.com/users/4249', 'pm_score': 0, 'selected': False, 'text': "<p>When you are creating the verification code, do you have access to the caller's phone number?</p>\n\n<p>If so I would use the caller's phone number and run it through some sort of hashing function so that you can guarantee that the verification code you gave to the caller in step 1 is the same one that they are entering in step 2 (to make sure they aren't using a friend's validation code or they simply made a very lucky guess).</p>\n\n<p>About the hashing, I'm not sure if it's possible to take a 10 digit number and come out with a hash result that would be &lt; 10 digits (I guess you'd have to live with a certain amount of collision) but I think this would help ensure the user is who they say they are.</p>\n\n<p>Of course this won't work if the phone number used in step 1 is different than the one they are calling from in step 2.</p>\n"}, {'answer_id': 46268, 'author': 'Haacked', 'author_id': 598, 'author_profile': 'https://Stackoverflow.com/users/598', 'pm_score': 1, 'selected': False, 'text': '<p>Does it have to be only numbers? You could create a random number between 1 and 1M (I\'d suggest even higher though) and then <a href="http://www.crockford.com/wrmg/base32.html" rel="nofollow noreferrer">Base32 encode it</a>. The next thing you need to do is Hash that value (using a secret salt value) and base32 encode the hash. Then append the two strings together, perhaps separated by the dash.</p>\n\n<p>That way, you can verify the incoming code algorithmically. You just take the left side of the code, hash it using your secret salt, and compare that value to the right side of the code.</p>\n'}, {'answer_id': 46284, 'author': 'Rob Rolnick', 'author_id': 4798, 'author_profile': 'https://Stackoverflow.com/users/4798', 'pm_score': 0, 'selected': False, 'text': '<p>Assuming you already know how to detect which key the user hit, this should be doable reasonably easily. In the security world, there is the notion of a "one time" password. This is sometimes referred to as a "disposable password." Normally these are restricted to the (easily typable) ASCII values. So, [a-zA-z0-9] and a bunch of easily typable symbols. like comma, period, semi colon, and parenthesis. In your case, though, you\'d probably want to limit the range to [0-9] and possibly include * and #.</p>\n\n<p>I am unable to explain all the technical details of how these one-time codes are generated (or work) adequately. There is some intermediate math behind it, which I\'d butcher without first reviewing it myself. Suffice it to say that you use an algorithm to generate a stream of one time passwords. No matter how mnay previous codes you know, the subsequent one should be impossibel to guess! In your case, you\'ll simply use each password on the list as the user\'s random code. </p>\n\n<p>Rather than fail at explaining the details of the implementation myself, I\'ll direct you to a 9 page article where you can read up on it youself: <a href="https://www.grc.com/ppp.htm" rel="nofollow noreferrer">https://www.grc.com/ppp.htm</a> </p>\n'}, {'answer_id': 46286, 'author': 'Adam Davis', 'author_id': 2915, 'author_profile': 'https://Stackoverflow.com/users/2915', 'pm_score': 0, 'selected': False, 'text': '<p>It sounds like you have the unspoken requirement that it must be quickly determined, via algorithm, that the code is valid.  This would rule out you simply handing out a list of one time pad numbers.</p>\n\n<p>There are several ways people have done this in the past.</p>\n\n<ol>\n<li>Make a public key and private key.  Encode the numbers 0-999,999 using the private key, and hand out the results.  You\'ll need to throw in some random numbers to make the result come out to the longer version, and you\'ll have to convert the result from base 64 to base 10.  When you get a number entered, convert it back to base64, apply the private key, and see if the intereting numbers are under 1,000,000 (discard the random numbers).</li>\n<li>Use a <a href="http://www.concentric.net/~Ttwang/tech/inthash.htm" rel="nofollow noreferrer">reversible hash function</a></li>\n<li>Use the first million numbers from a PRN seeded at a specific value.  The "checking" function can get the seed, and know that the next million values are good.  It can either generate them each time and check one by one when a code is received, or on program startup store them all in a table, sorted, and then use binary search (maximum of compares) since one million integers is not a whole lot of space.</li>\n</ol>\n\n<p>There are a bunch of other options, but these are common and easy to implement.</p>\n\n<p>-Adam</p>\n'}, {'answer_id': 46325, 'author': 'Michael Sharek', 'author_id': 1958, 'author_profile': 'https://Stackoverflow.com/users/1958', 'pm_score': 0, 'selected': False, 'text': '<p>You linked to the <a href="http://code.google.com/p/checkdigits/" rel="nofollow noreferrer">check digits</a> project, and using the "encode" function seems like a good solution.  It says:</p>\n\n<blockquote>\n  <p>encode may throw an exception if \'bad\' data (e.g. non-numeric) is passed to it, while verify only returns true or false. The idea here is that encode normally gets it\'s data from \'trusted\' internal sources (a database key for instance), so it should be pretty usual, in fact, exceptional that bad data is being passed in.</p>\n</blockquote>\n\n<p>So it sounds like you could pass the encode function a database key (5 digits, for instance) and you could get a number out that would meet your requirements.</p>\n'}, {'answer_id': 46486, 'author': 'Costo', 'author_id': 1130, 'author_profile': 'https://Stackoverflow.com/users/1130', 'pm_score': 6, 'selected': True, 'text': '<p>After some research, I think I\'ll go with the <strong>ISO 7064 Mod 97,10</strong> formula. It seems pretty solid as it is used to validate IBAN (International Bank Account Number).</p>\n\n<p>The formula is very simple:</p>\n\n<ol>\n<li>Take a number : <code>123456</code></li>\n<li>Apply the following formula to obtain the 2 digits checksum :  <code>mod(98 - mod(number * 100, 97), 97)</code>  => 76</li>\n<li>Concat number and checksum to obtain the code => 12345676</li>\n<li>To validate a code, verify that <code>mod(code, 97) == 1</code></li>\n</ol>\n\n<p>Test :</p>\n\n<ul>\n<li><code>mod(12345676, 97) = 1</code>  => GOOD</li>\n<li><code>mod(21345676, 97) = 50</code> => BAD !</li>\n<li><code>mod(12345678, 97) = 10</code> => BAD ! </li>\n</ul>\n\n<p>Apparently, this algorithm catches most of the errors.</p>\n\n<p>Another interesting option was the <a href="http://en.wikipedia.org/wiki/Verhoeff_algorithm" rel="noreferrer">Verhoeff algorithm</a>. It has only one verification digit and is more difficult to implement (compared to the simple formula above). </p>\n'}, {'answer_id': 46504, 'author': 'Jim McKeeth', 'author_id': 255, 'author_profile': 'https://Stackoverflow.com/users/255', 'pm_score': 2, 'selected': False, 'text': '<p>You want to segment your code.  Part of it should be a 16-bit CRC of the rest of the code.  </p>\n\n<p>If all you want is a verification number then just use a sequence number (assuming you have a single point of generation).  That way you know you are not getting duplicates.  </p>\n\n<p>Then you prefix the sequence with a CRC-16 of that sequence number AND some private key.  You can use anything for the private key, as long as you keep it private.  Make it something big, at least a <a href="http://www.guidgenerator.com/" rel="nofollow noreferrer">GUID</a>, but it could be the text to <a href="http://www.gutenberg.org/etext/2600" rel="nofollow noreferrer">War and Peace from project Gutenberg</a>.  Just needs to be secret and constant.  Having a private key prevents people from being able to forge a key, but using a 16 bit CR makes it easier to break.</p>\n\n<p>To validate you just split the number into its two parts, and then take a CRC-16 of the sequence number and the private key. </p>\n\n<p>If you want to obscure the sequential portion more, then split the CRC in two parts.  Put 3 digits at the front and 2 at the back of the sequence (zero pad so the length of the CRC is consistent).</p>\n\n<p>This method allows you to start with smaller keys too.  The first 10 keys will be 6 digits.</p>\n'}]
['https://Stackoverflow.com/questions/46231', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1130/']