Swiftpack.co - paylike/swift-luhn as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by paylike.
paylike/swift-luhn 0.2.0
Luhn implementation for Swift
⭐️ 0
🕓 1 week ago
iOS macOS
.package(url: "https://github.com/paylike/swift-luhn.git", from: "0.2.0")



Calculate/verify check digit (luhn) of credit cards

This library is a clone of the original javascript implementation from Paylike.



// dependencies: 
.package(url: "[email protected]:paylike/swift-luhn.git", .upToNextMajor(from: "0.2.0")

// target:
.product(name: "PaylikeLuhn", package: "swift-luhn")

Cocoapods: https://cocoapods.org/pods/PaylikeLuhn

pod 'PaylikeLuhn'


// Is the card number valid?
let card = "6123451234567893"
PaylikeLuhn.isValid(card)    // true

// Calculate check digit
// MII + IIN + "Individual account number"
let incomplete = "612345123456789"

PaylikeLuhn.calculateCheckDigit(incomplete);    // "1"

Implemented according to ISO/IEC 7812-1:2015(E) which goes:

  1. Double the value of alternate digits beginning with the first right-hand digit (low order).

  2. Add the individual digits comprising the products obtained in Step 1 to each of the unaffected digits in the original number.

  3. Subtract the total obtained in Step 2 from the next higher number ending in 0 [this is the equivalent of calculating the “tens complement” of the low-order digit (unit digit) of the total]. If the total obtained in Step 2 is a number ending in zero (30, 40, etc.), the check digit is 0.


Stars: 0
Last commit: 1 week ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics