-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Not luhn compliant #403
Comments
From a 5-second googling it seems like all the major credit card providers all use the same checksum validation algorithm (Luhn). There are also personal identification numbers that use this algorithm for their checksums. |
I think it's also used for IBANs. If it were available in the core I'd update those generators. |
Many PHP Validation libraries have luhn validation. I don't think it's possible to generate luhn-valid numbers. Instead, we could wrap the Taken from the lithium validation class: <?php
function isValidLuhn($value) {
if (empty($value) || !is_string($value)) {
return false;
}
$sum = 0;
$length = strlen($value);
for ($position = 1 - ($length % 2); $position < $length; $position += 2) {
$sum += $value[$position];
}
for ($position = ($length % 2); $position < $length; $position += 2) {
$number = $value[$position] * 2;
$sum += ($number < 10) ? $number : $number - 9;
}
return ($sum % 10 === 0);
} |
Fixed in #414 |
@fzaninotto amazing! Thanks 😃 |
Great work! Much better implementation now. I noticed one little naming thing in the rewritten tests in test/Faker/Provider/sv_SE/PersonTest.php. I've written a comment here: https://github.com/fzaninotto/Faker/pull/414/files#r17042304 |
Hi guys, I noticed the credit cards numbers generated by this library won't pass a luhn validator. Is this by design?
The text was updated successfully, but these errors were encountered: