# Verify a scanned barcode with math

I have an id number that needs to be read off a sheet of paper with a barcode by a scanner. Most of the time this will work flawlessly but sometimes there are mistakes (like if paper is partly torn).

I want to be able to flag the occurrences where the number does not make sense so that a human can review. This need for a high level of accuracy is because the paper is providing consent to enroll in a program and I to need to limit (if not eliminate) someone from being enrolled without consent. So if I just use the id number(123456789) to create the barcode and then scan interprets as 123456788 then the wrong person is automatically enrolled.

My current idea for a solution is to add additional numbers to the barcode that when read could verify that the id number is the correct one, but I am unsure of a good solution for this. An example of this would be to supply 061524-123456789 as the barcode. Now, when processed I can use a formula that verifies the id by adding the digits in groups of three (1+2+3=6, 4+5+6=15, etc..) and comparing to the first group of numbers. The conversion at the time of barcode creating and barcode reading should render the same results else it will be flagged. Obviously, there are problems with the example because the different id numbers could have the same confirmation.

So, does anyone know of a reliable mathematical formula which will create a smaller verification code that can be used to verify later?

Conditions:

• I need the "key" to be a shorter in characters as the total number digits needs to be limited.

• A programming solution is acceptable.

Note: I am completely out of my league here so I don't know how to tag this question. Help with that is appreciated.

• en.wikipedia.org/wiki/Check_digit, may be of interest to you – BadAtMaths Aug 14 '15 at 15:45
• @BadAtMaths yes I believe it that is what I am looking for. Thank you. – natedavisolds Aug 14 '15 at 15:47
• This is definitely a solved problem. Do some research on bar code standards and choose one that works for you. You can probably find programming libraries that make it easy. – Matthew Leingang Aug 14 '15 at 15:50
• @MatthewLeingang I was having difficulty searching for the correct key words. Knowing nothing about the subject but knowing the solution must be out there. And this is what the social Q&A helps with :) – natedavisolds Aug 14 '15 at 15:55
• Hopefully you can find something that suits your needs. I just wanted to make sure you didn't set out to reinvent the wheel. – Matthew Leingang Aug 15 '15 at 20:36

## 1 Answer

A common solution is modular arithmetic. For example, taking the code modulo $97$ yields a two digit number you can append to the end of the code. Generally a prime number is chosen for the modulus (and $97$ is the largest two-digit prime). Your example code satisfies $$123456789\equiv39\pmod{97},$$ so your longer code could be 39-123456789. You can choose more primes to get more verification numbers to reduce the chances of errors even further.