Download check digits and checksums - Cork Institute of Technology

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

List of prime numbers wikipedia , lookup

Mechanical calculator wikipedia , lookup

Proofs of Fermat's little theorem wikipedia , lookup

Addition wikipedia , lookup

Elementary mathematics wikipedia , lookup

Location arithmetic wikipedia , lookup

Approximations of π wikipedia , lookup

Arithmetic wikipedia , lookup

Positional notation wikipedia , lookup

Elementary arithmetic wikipedia , lookup

Transcript
CHECKSUMS
Validating user input
using checksums
CHECK DIGITS AND CHECKSUMS
are error checking codes that can detect if a user has made a mistake while entering data. One or more
redundant pieces of information is added to the data.
Very often an extra digit is added to the
end of the data. This is called a check
digit. The check digit chosen must match
the rest of the data in some way and if it
does not, then it means someone must
have made a mistake.
If the last digit of a phone number was
always equal to the number of even digits
in the rest of the number, it might be able
to detect wrong numbers. Such a scheme
would not be very useful though. It would
not be able to detect the transposition of
digits e.g. 1243 instead of 1234 and this
is one of the most common mistakes.
Such a scheme would also be costly
since the number of possible customers a
phone company could handle would be
only 10% of the capacity if all the digits
were used.
Luhn Algorithm for Credit Cards
However, in many applications the ability
to detect errors as the user enters the
data is worth the effort. The most widely
used checksum on the internet in the
Luhn algorithm for validating credit card
numbers. To validate a credit card
number you must multiply every second
digit from the end by 2. If multiplication
by 2 results in a number greater than 9
then the digits should be added together
(mod 9 gives the same result). If the
results of the doubling are added to the
non-doubled numbers, the sum should
be evenly divisible by 10.
Of course a valid check digit does not
ensure that the card is real, is not stolen,
and that the owner has any money. But
detecting data entry errors early saves a
web site sending an incorrect number to
the bank for validation, and reduces user
waiting time and frustration
4 5 9 9 2 2 8 3 0 8 0 5 2 6 0 0
8
4
1
6
0
0
4
0
8 5 9 9 4 2 7 3 0 8 0 5 4 6 0 0
70
.
[1]
1
8
International Standard Book
Number (ISBN)
Euro Bank Note Serial Numbers
For over 40 years the book industry has
assigned each new book a unique
identifier called the International Standard
Book Number (ISBN). The last digit of the
ISBN is a check digit. Starting at the end
of the number each digit is multiplied by
its position from the end of the number.
The last digit is multiplied by 1, the
second last by 2, the third last by 3, all
the way to 10. The numbers are summed
and if the number is valid it should be
evenly divisible by 11. Check digits
calculated using the reminder of integer
division (modulus or mod) are very
common and division by prime numbers
tends to work well. The Mod 11
checksum in the ISBN number poses a
problem because Mod 11 can result in
values ranging from 0 to 10 inclusive.
Because there is no single digit to
represent 10 the ISBN check digit may be
a digit or an X. X is the Roman numeral
for 10.
1
5
6
4
7
8
2
1
4
X
* 10 9
8
7
6
5
4
3
2
1
+ 10 45 48 28 42 40 8
3
8 10
242 evenly divisible by 11
ISBN-13 and European Article
Number
Over time it became clear that the ISBN
system would eventually run out of
numbers, so the system was extended to
13 digits. ISBN-13 also uses a single
check digit, but it is calculated differently
from ISBN-10. An ISBN-13 number is
also valid European Article Number (EAN)
so the check sum is calculated in the
same way as it is for items in the
supermarket.
The checksum is calculated by
multiplying every second digit by 3, and
summing the multiplied and nonmultiplied digits. As usual, the check digit
itself is not multiplied.
9
7
8
9 21 8
1 5
6
4
7
8
2
1
3 5 18 4 21 8
6
1 12 4
120 evenly divisible by 10
4 4
Documento Nacional de Identidad
Euro bank notes use a check digit at the
end. The letter at the start of the serial
number specifies the country where the
note was printed. This can be converted
to a number based on its position in the
alphabet (A=1, B=2, C=3 etc). If the rest
of the number is treated as a large
integer, then the letter position can be
added to it. The mod 9 of the sum should
be 8. [Alternatively you can sum the letter
value and the individual digits, then sum
the digits. This will also result in 8.]
In Spain the ID number (DNI) has 8
numeric digits followed by a single letter
check digit at the end. If the numeric part
of the number is treated as a large
integer, then mod 23 gives a value that
corresponds to a letter. The mapping of
numbers to letters is somewhat arbitrary.
0 1 2 3 4 5 6 7 8 9 10 11
T R W A G M Y F P D X B
X04371312854
X
12 13 14 15 16 17 18 19 20 21 22
4371312854
24
N J Z S Q V H L C K E
4371312854
4371312878
Find Out More
04371312878 mod 9 = 8
Check your own PPS Number
Personal Public Service Number
PPS numbers issued to residents of
Ireland use a single letter check digit. This
letter is calculated by multiplying each
digit by its position from the end of the
number. So the numbers are multiplied by
8, by 7, by 6, all the way to the digit
before the letter. The modulus 23 of sum
of these values should correspond to the
position of the letter in the alphabet. If
mod 23 results in 0 then W is used.
9
9
8
72
1
7
63
9
6
6
2
5
45
7
4
8
4
3
21
P
2
8
223
223 mod 23 = 16 = P
For many years women were assigned
the numbers of their husbands with a W
appended at the end for wife. Although
this practice stopped many years ago a
large number of women still have these
numbers and so have 2 letters at the end
of their PPSN. The first if these letters is
the check digit. However even these
numbers are being phased out and the
women who have them are being
encouraged to get new PPSNs.
[2]
The circumstances under which you can
be asked for your PPSN are regulated by
law. See if you can find out more.
Find out which letters are assigned to
which countries on Euro notes.
Check some notes from your wallet.
Find out what the digits in Finland’s ID
number sosiaaliturvatunnus mean.
The numbering system used in France
uses a mod 97 two-digit checksum at the
end of the number. The numbering
system used in France today is quite old
and has a dark history. See if you can find
out more.
For a mod 23 checksum only 23 of the 26
letters of the alphabet are used. Both the
Finnish and the Spanish choose not to
employ the first 23 letters. Which letters
are left out and why?
Be strict with types
JAVASCRIPT
JavaScript is very flexible with types and
the string “35” can often be used as the
integer 35 without any problems.
However this flexibility can sometimes
cause confusion and unexpected
program behaviour. If your program has
a string or string fragment that you plan
to treat as an integer it might be wise to
first convert it to an integer.
Use loops
Many checksum algorithms lend
themselves to implementation using
loops. It is inefficient to take the first digit
of a number and multiply it by 8, take the
next digit and multiply it by 7 and so on.
A carefully written loop might do the job
much more efficiently. For example, take
the nth digit and multiply it by 8 – n + 1.
The last character of the substring is the
n-1th character and not character n. This
can cause confusion.
If the second parameter is omitted the
extraction will go to the end of the string.
floor ( )
The floor(x) method returns the value of a
number rounded down to the nearest
integer. This can be useful if you are
trying to extract specific digits from a
number.
For data that is entirely numeric, there is
no need to deal with the input as a string.
It may be possible to extract the required
digits mathematically. If x is 1027, for
example, x mod 10 gives 7.
Math.floor (x) is 102.
parseInt ( )
indexOf ( )
The indexOf( ) method evaluates to the
position of the first occurrence of a
substring in a string. If a second integer
parameter is provided the search for the
substring will start at that position. It
returns -1 if the substring is not found.
myString.indexOf (lookfor, lookfrom)
If myS is "Cork Institute of
Technology"
then myS.indexOf ("o",5)
evaluates to 10
The parseInt("3" ) function takes a string
and evaluates to integer. It requires a
second parameter to specify the number
base being used (e.g. 16 for
hexadecimal). If the base is not specified
the number base used will depend on the
format of the string.
parseInt(string, base)
charAt ( )
The charAt(x) method returns the
character at a specified position.
myString.charAt(index)
The problem of the arbitrary mappings of
numbers to letters in Finnish and
If myS is "Cork Institute of Technology"
Spanish ID check digits can be solved by
then myS.charAt(5) is "I"
declaring a string with the letters in the
appropriate order and using the position
of the letter in the string to give the
charCodeAt ( )
required value.
The charCodeAt(x) method returns the
Unicode of the character at the specified
position. For roman characters the
Unicode is also the ASCII value.
JavaScript for Checksums
JavaScript can be used to
validate the checksums of
data entered on a web
page. Here are some
useful tips and
JavaScript methods and
function.
substring ( )
The substring() method extracts the
characters in a string between two
points in a string.
myString.substring(m,n)
If myS is "Cork Institute of
Technology" then
my.substring (2,6) evaluates to
“rk I”.
Don’t forget that the first character of the
string is character 0, not 1.
[3]
myString.charCodeAt(index)
Subtracting 48 from the ASCII value of a
digit gives the value of the digit.
Subtracting 64 from the ASCII value of an
upper case letter gives the position of
that letter in the alphabet.
If myS is "Cork Institute of
Technology" then myS.charAt(5) is
73