Validate Credit Card Number — Luhn’s Check Algorithm Explained in Swift

Derek Kim
5 min readAug 21, 2023

Hello everyone, this is Derek.

It has been quite a while since I’ve written a blog due to lot of things going on in my life like preparing for job interviews and family emergency, but I am back at it now!

Today I will be writing about Luhn’s Check Algorithm, which is widely used algorithm for checking the credit card number’s validity.

Ever wondered how that small piece of plastic in your wallet knows when you mistype a single digit? The magic behind this is the Luhn’s Check Algorithm, an unsung hero in the world of finance, tirelessly working behind the scenes to ensure that credit card numbers are valid.

Luhn’s.. What Now?

In the simplest of words, the Luhn’s Check Algorithm, or Modulus 10 Algorithm, is a formula used by many industries to validate identification numbers, especially credit card numbers. If you ever typed in a wrong card number online and got an instant error without even hitting the submit button — yup, that’s Luhn’s algorithm doing its thing.

Very important facts listed in Wikipedia:

It is not intended to be a cryptographically secure hash function, but it was more designed to protect against accidental errors, not malicious attacks.

--

--