algorithm · 2025-01-06 · 7 min read

What the Luhn algorithm actually checks (and what it doesn't)

A single formula from 1954 sits behind almost every card number. Here is exactly what it validates, why it catches typos, and why passing it proves nothing about a real account.

Every card ends with a digit computed from the rest.
Every card ends with a digit computed from the rest. — © Pixabay · Pexels

Every payment card number ends with a check digit. That digit is not random: it is computed from the other digits with a formula patented by Hans Peter Luhn at IBM. When you mistype a digit, the formula almost always fails, and a checkout form can tell you so before anything is sent to a bank. You can watch it happen live in our interactive Luhn lab.

The algorithm is sometimes called mod 10 because the final test is whether a sum is divisible by ten. It is public domain, deliberately simple, and appears in far more places than cards: phone IMEIs, Canadian social insurance numbers and many national IDs use it too.

The four steps, with an example

  • Starting from the right-most digit, double every second digit.
  • If a doubled value exceeds 9, subtract 9 (equivalent to summing its two digits: 16 → 1 + 6 = 7).
  • Add all the resulting digits together.
  • If the total is divisible by 10, the number passes.

Take Stripe's famous test number 4242 4242 4242 4242. Doubling every second digit from the right turns each 4 into 8, so the sum is 8 × 8 + 2 × 8 = 80, which is divisible by 10. Change the last digit to 3 and the sum becomes 81 — the number fails. Try exactly that in the inspection bench.

Why the check digit is derived from the number itself

The issuer picks the first 15 digits (the IIN plus the account identifier) and then computes the 16th so that the whole string passes. That is why the check digit can be verified offline with no network call — and also why it is not a secret.

What Luhn does not prove

Passing Luhn means the string is well-formed. It says nothing about whether an account exists, is active, is unblocked, or has funds.

Anyone can generate a number that passes — which is exactly how BIN attacks work and why real acceptance happens only inside an authorized payment gateway with 3-D Secure, CVV and address checks. Treat Luhn as a spell-checker for numbers, not as a lock.

Where it falls short

Luhn detects every single-digit error and most adjacent transpositions, but not all of them. The one classic blind spot is swapping 09 and 90; read the transposition Luhn cannot catch for the full picture, and see the external Wikipedia article for the formal description.

The whole algorithm fits in a dozen lines of code.
The whole algorithm fits in a dozen lines of code.

Keywords: Luhn algorithm, check digit, credit card validation, mod 10, card number checksum, Hans Peter Luhn

Frequently asked questions

Is a Luhn-valid number a real credit card?

No. Luhn only confirms the digits are internally consistent. Real cards must also exist at an issuer, be active, and pass authorization.

Which card brands use the Luhn algorithm?

Visa, Mastercard, American Express, Discover, JCB, Diners Club, UnionPay and Maestro all end their numbers with a Luhn check digit.

Can I validate a CVV with Luhn?

No. The security code is generated with issuer keys and cannot be computed from the card number.

On this site

External references

Related articles

ماذا تفحص خوارزمية لون فعلًا (وما الذي لا تفحصه) — النسخة العربية

معادلة واحدة من عام 1954 تقف خلف كل رقم بطاقة تقريبًا. إليك بالضبط ما تتحقق منه، ولماذا تكشف الأخطاء الكتابية، ولماذا المرور منها لا يثبت وجود حساب حقيقي.

ينتهي كل رقم بطاقة دفع بـخانة تحقق. هذه الخانة ليست عشوائية؛ إنها تُحسب من بقية الخانات بمعادلة سجّلها هانس بيتر لون في IBM كبراءة اختراع. عندما تخطئ في كتابة خانة واحدة تفشل المعادلة غالبًا، فيستطيع نموذج الدفع تنبيهك قبل إرسال أي شيء إلى البنك. يمكنك مشاهدة ذلك مباشرة في مختبر لون التفاعلي.

تُسمّى الخوارزمية أحيانًا mod 10 لأن الاختبار النهائي هو ما إذا كان المجموع يقبل القسمة على عشرة. وهي ملكية عامة وبسيطة عمدًا، وتظهر في أماكن أكثر بكثير من البطاقات: أرقام IMEI للهواتف وأرقام التأمين الاجتماعي الكندية وكثير من الهويات الوطنية.

الخطوات الأربع مع مثال

  • بدءًا من أقصى اليمين، ضاعف كل خانة ثانية.
  • إن تجاوز الناتج 9 اطرح منه 9 (وهو ما يعادل جمع رقميه: 16 → 1 + 6 = 7).
  • اجمع كل الخانات الناتجة.
  • إذا قبل المجموع القسمة على 10 يمرّ الرقم.

خذ رقم الاختبار الشهير من Stripe: ‎4242 4242 4242 4242. مضاعفة كل خانة ثانية من اليمين تحوّل كل 4 إلى 8، فيصبح المجموع 8 × 8 + 2 × 8 = 80، وهو يقبل القسمة على 10. غيّر آخر خانة إلى 3 فيصبح المجموع 81 — ويفشل الرقم. جرّب ذلك بالضبط في منضدة الفحص.

لماذا تُشتق خانة التحقق من الرقم نفسه

يختار المُصدِر الخانات الـ15 الأولى (IIN ثم معرّف الحساب) ثم يحسب الخانة السادسة عشرة بحيث تمرّ السلسلة كاملة. ولهذا يمكن التحقق من خانة التحقق دون اتصال بالشبكة — وأيضًا لهذا ليست سرًّا.

ما لا تثبته لون

المرور من لون يعني أن السلسلة سليمة الشكل. لا يقول شيئًا عن وجود الحساب أو نشاطه أو عدم حظره أو رصيده.

أي شخص يستطيع توليد رقم يمرّ — وهذا بالضبط ما تقوم عليه هجمات BIN، ولهذا لا يحدث القبول الحقيقي إلا داخل بوابة دفع مصرّح لها مع 3-D Secure وCVV والتحقق من العنوان. تعامل مع لون كمدقّق إملائي للأرقام لا كقفل.

أين تقصّر

تكشف لون كل أخطاء الخانة الواحدة ومعظم تبديلات الخانات المتجاورة لكن ليس كلها. النقطة العمياء الكلاسيكية هي تبديل 09 و90؛ اقرأ التبديل الذي لا تكشفه لون للصورة الكاملة، وراجع مقالة ويكيبيديا للوصف الرسمي.

أسئلة شائعة

هل الرقم الصحيح بحسب لون بطاقة حقيقية؟

لا. تؤكد لون فقط أن الخانات متسقة داخليًا. البطاقات الحقيقية يجب أن توجد لدى مُصدِر وتكون نشطة وتجتاز التفويض.

أي العلامات التجارية تستخدم خوارزمية لون؟

فيزا وماستركارد وأمريكان إكسبريس وديسكفر وJCB وداينرز كلوب ويونيون باي ومايسترو جميعها تُنهي أرقامها بخانة تحقق لون.

هل يمكن التحقق من CVV بلون؟

لا. رمز الأمان يولَّد بمفاتيح المُصدِر ولا يمكن حسابه من رقم البطاقة.

Educational content — not legal or financial advice. Open the interactive Check Digit Lab →