Skip navigation

Verhoeff’s Dihedral Group D5 Check

I just finished implementing the Verhoeff checksum algorithm in PHP, you can download it as verhoeff.zip. Yes, mathematically it’s a bit more elaborate than other checksum algorithms, but with look-up tables it’s fairly simple and lightweight.

The implementation provides two functions calcsum($number) and checksum($number). The first one return a check digit for the decimal number used as argument, just append the returned digit to the number and you got yourself a checkable number. The second function checks the number (including the appended check digit) used as argument, the number is only valid if the function return zero.

I noticed a small bug due to a misplace modulus (simple PHP syntax typo), the verhoeff.php is now updated, tested and in working order. The bug only affected the calculation of check digits, not the check function.

11 comments

  1. ruben paredes
    Posted October 27, 2006 at 19:27 | Permalink

    Gracias por su Colaboración

  2. ANTHONY
    Posted February 12, 2007 at 9:56 | Permalink

    IVE BEEN READING WIKIPEDIAS EXPLANATION OF TH VERHOEFF SCHEME. but when it comes to their example am not able to figure out where they get the values for the column titled previous c. please help me on this. send expalnation to my email.

  3. Posted February 28, 2007 at 10:41 | Permalink

    For the benefit of all, I’ve realized that I could post my reply here as well:

    You asked about where the previous c value came from. I guess it is easier to follow the example on Wikipedia if you take a look at my PHP implementation while following it along, not that it is any complicated matter. The c start with a value of zero, when the algorithm iterates over the string of digits c is both input to the d(j,k) function (the j value) and the output replacing the previous or default value. Thus, in the Wikipedia example, previous c represent the c value from the previous iteration or the default value on the first iteration.

  4. mauricio
    Posted March 7, 2007 at 2:03 | Permalink

    thanks, still not clear what is the role of the calcsum() function. Also, what do you do when you have numbers with more than 8 digits total (like in a credit card)?

  5. Posted March 7, 2007 at 10:45 | Permalink

    The calcsum($number) function return the checksum digit for the series of digits used as argument, if you give it 142857 as argument it will return the checksum digit 0. Append the digit to the number so it now reads 1428570.

    The checksum($number) do the opposite. Give it a number with check digit to check, like 1428570, as argument and will return a 0 if the number is valid (it will always be a zero if valid no matter what the check digit was).

    The Verhoeff algorithm have no limitation on the number of digits (as many other algorithms have) it can check, it’s unlimited.

  6. Peter Harding
    Posted March 30, 2007 at 11:42 | Permalink

    The results of the wikipedia article differ from the results of the recipe given in the “Numerical Recipes in C” book. I’ve found a website that agrees with the book (but maybe they used it!). I’ve retrieved Verhoeff’s original paper but I’m not up on group theory - it looks the same upside down to me!

    Anyone know for sure that the wikipedia article is good? Are there any good other references around?

    Many thanks

  7. Posted March 30, 2007 at 11:56 | Permalink

    I don’t remember what text I used, don’t think it was the Wikipedia article but some textbook or report on check digits. I probably have a couple PDF files with the text that I’ve downloaded somewhere, I will try to find them.

    Hmm… wonder what result my implementation match? Scary.

  8. Peter Harding
    Posted March 30, 2007 at 14:49 | Permalink

    I can’t run PHP. If we take the example 142857, the wikipedia version gives 0 and the Numerical Recipes version gives 6. This site http://www.augustana.ab.ca/~mohrj/algorithms/checkdigit.html agrees with wikipedia, this one http://web.mit.edu/kenta/www/three/verhoeff-checksum.html.gz goes with Numerical Recipes. Num Rec has shot me in the foot before (giving code in a typeface where “one” and “ell” are identical!!), but it’s generally held to be scholarly. I was going to try and surf some pictures of old german banknotes…!

  9. Posted March 30, 2007 at 16:44 | Permalink

    Here is a live version of my script to play around with:

    http://labs.dahnielson.com/tmp/verhoeff-test.php?f=calc&n=142857

    • f can either be calc or check
    • n is the number to calculate a check digit for or check

    If you calculate the check digit for 142857 it return 0 (and if you check 1428570 it return 0). So my code looks like to be on the Wikipedia side of the table.

  10. Peter Harding
    Posted April 4, 2007 at 13:15 | Permalink

    It turns out that my client has made up lots of “units” using the numerical recipes version, so right or wrong they’re going to go with that. I’ve retrieved Verhoeff’s original paper but it’s hard group theory maths -it looks the same upside down to me! I see the wikipedia article discussion cites yet another rendering of the scheme, also held to be incorrect.

    Without the input of a skilled mathematician (Verhoeff - are you reading us?) I guess nobody will ever really know! If you find your references I’d be interested to hear about them.

    Thanks for your input!

  11. Posted April 10, 2007 at 11:57 | Permalink

    I believe this document “Numeric Department Identifiers” by the Australian Defense Force Academy was my main reference when implementing the Verhoeff algorithm.

Post a comment

Your email is never published nor shared. Required fields are marked *
*
*
Creeper