Giter VIP home page Giter VIP logo

luhncheck's Introduction

LuhnCheck

A Python package to validate identification numbers using the Luhn algorithm with additional optional checks.

Release Status Coverage Status Code Quality PyPI Version Package License

Overview

The Luhn algorithm or Luhn formula, also known as the "modulus 10" or "mod 10" algorithm, named after its creator, IBM scientist Hans Peter Luhn, is a simple checksum formula used to validate a variety of identification numbers, such as:

  • US National Provider Identifier numbers.
  • Canadian Social Insurance Numbers.
  • Saudi Arabia National and Resident ID numbers.
  • South African ID numbers.
  • Swedish National identification numbers.
  • Swedish Corporate Identity Numbers (OrgNr).
  • Greek Social Security Numbers (ΑΜΚΑ).
  • Credit card numbers.
  • IMEI numbers.

The algorithm is in the public domain and is in wide use today. It was designed to protect against accidental errors. Most credit cards and many government identification numbers use the algorithm to distinguish valid numbers from mistyped or otherwise incorrect numbers.

Features

  • Simple API to validate numbers based on the Luhn algorithm.
  • Extended validation to cover number length and prefix(es).
  • Can validate numbers containing hyphens or spaces.
  • Works on Python 3.8+ with zero dependencies.
  • Thoroughly tested with 100% test coverage.

Installation

To install using pip, run:

pip install luhncheck

Usage Examples

>>> from luhncheck import is_luhn

>>> # Simple checksum validation
>>> is_luhn("1101798278")
True

>>> # Additional check for length (9 digits)
>>> is_luhn("1101798278", 9)
False

>>> # Additional checks for prefix (either 1 or 2)
>>> is_luhn("1101798278", 10, ["1", "2"])
True

>>> # Validate numbers containing hyphens
>>> is_luhn("01-055102-109831-4", None, "01")
True

API Reference

function is_luhn

is_luhn(
    number: str,
    length: int | None = None,
    prefix: str | list[str] | None = None
) -> bool

Validate checksum and format of an identification number based on the Luhn algorithm.

Args:

  • number: Identification number to validate.
  • length: How many digits the number must contain. (The default is None, which implies skipping the length check).
  • prefix: Exact digit(s) the number must start with. When a list of digits is provided, one of the values must match. (The default is None, which implies skipping the prefix check).

Returns:

  • True when the number is valid; otherwise, False.

License

This project is licensed under the terms of the MIT license.

luhncheck's People

Contributors

ddimmich avatar dralshehri avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

luhncheck's Issues

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.