26avr/110
PHP – Generate Luhn checksum (for valid account numbers)
Here’s a function to calculate the Luhn checksum of a 15 digits account number (the checksum’ll be the 16th digit) :
-
public function luhnChecksum($accountNumber) {
-
-
// Buffer
-
$sum = 0;
-
-
// Process each digit
-
for ($i = 0; $i < 15; $i++) {
-
$digit = $accountNumber{$i};
-
-
// each 2 digits
-
if ($i % 2 == 0) {
-
$double = $digit * 2;
-
-
// if over 10, sum of the 2 digits
-
if ($double > 10) {
-
$double = (string)$double;
-
$sum += ((int)$double{0} + (int)$double{1});
-
} else {
-
$sum += $digit;
-
}
-
} else {
-
$sum += $digit;
-
}
-
}
-
-
// Euclidian division
-
$rest = $sum % 10;
-
-
// luhn key
-
$key = 10 - $rest;
-
-
return $key;
-
}
Article for teaching purposes.
I can not in any way be held liable for its use

Posts that maybe you want to read ::
Commentaires (0)
Trackbacks (0)
(Souscrire aux commentaires de cet article)
Aucun trackbacks pour l'instant
