Giter Site home page Giter Site logo

serluca / luhn Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 2.0 28 KB

A base-independent implementation of the Luhn algorithm for Swift. Useful for generating and verifying check digits in arbitrary bases.

License: MIT License

Swift 78.51% Ruby 8.53% Objective-C 12.97%

luhn's Introduction

Base indipendent Luhn

Twitter: @serluca License Carthage compatible CocoaPods Compatible Platform Build Status codecov

A base-independent implementation of the Luhn algorithm for Swift. Useful for generating and verifying check digits in arbitrary bases.

To consider a porting of https://github.com/benhodgson/baluhn in swift.

From Wikipedia:

The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, is a simple checksum formula used to validate a variety of identification numbers, such as credit card numbers, IMEI numbers, National Provider Identifier numbers in US and Canadian Social Insurance Numbers

Installation

Cocoapods

CocoaPods is a dependency manager for Cocoa projects. You can install it with the following command:

$ gem install cocoapods

To integrate Luhn into your Xcode project using CocoaPods, specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
use_frameworks!

pod 'Luhn'

Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Luhn into your Xcode project using Carthage, specify it in your Cartfile file:

github "serluca/Luhn"

Swift Package Manager

Swift Package Manager coming soon.

Manually

Download the project and copy the Sources folder content into your project to use it in.

Usage

Luhn provides two functions: verify and generate.

generate calculates the Luhn check character for the given input string in the given base. This character should be appended to the input string to produce a valid Luhn string.

verify tests whether or not a string is a valid Luhn string in the given base. By default, luhn operates in base 10:

To use it, just import Luhn in your projectimport Luhn

Example

Clone and open the playground to see a live usage of the library.

// Genereta a value to have a valid luhn using the decimal alphabet
try Luhn.generate(baseString: "7992739871") // Generate 3

// Verify if the string is a valid luhn using the decimal alphabet
try Luhn.verify(string: "79927398713") // A valid lunh
try Luhn.verify(string: "79927398714") // An invalid luhn

// Using a defined alphabet
let alphabet = "abcdefghijklmnopqrstuvwxyz"
try Luhn.generate(baseString: "swift", alphabet: alphabet) // Returns l
try Luhn.verify(string: "swiftl", alphabet: alphabet) // A valid luhn
try Luhn.verify(string: "swiftz", alphabet: alphabet) // An invalid luhn

Special Thanks

Special thanks to @benhodgson

License

Luhn is released under the MIT license. See LICENSE for details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.