Skip to main content

Simple validator for identification numbers based on Luhn algorithm

Project description

luhn-validator

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

Checks 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 as a simple method of distinguishing valid numbers from mistyped or otherwise incorrect numbers.

Features

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

Installation

To install using pip, run:

pip install luhn-validator

Usage Examples

>>> from luhn_validator import validate

>>> # Simple checksum validation
>>> validate(1101798278)
True

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

>>> # Additional checks for length (10 digits) and prefix (either 1 or 2)
>>> validate(1101798278, 10, [1, 2])
True

API Reference

function validate

validate(
    number: int,
    length: Optional[int] = None,
    prefix: Optional[Union[int, List[int]]] = None,
) -> bool

Validate format and checksum of an identification number based on 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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

luhn-validator-1.1.1.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

luhn_validator-1.1.1-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file luhn-validator-1.1.1.tar.gz.

File metadata

  • Download URL: luhn-validator-1.1.1.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for luhn-validator-1.1.1.tar.gz
Algorithm Hash digest
SHA256 9e291b3c43dd4b9f38de8f44da3d7dd30bdccfe2b78dd836b7991aafe527e9dd
MD5 984a4e3af73d6aa1b3ce69cde1ad536c
BLAKE2b-256 543d2f75c29186f358f664cda27565d54d8d100f93832ca43a65bf311fb6b9bf

See more details on using hashes here.

File details

Details for the file luhn_validator-1.1.1-py3-none-any.whl.

File metadata

  • Download URL: luhn_validator-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for luhn_validator-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3c148f78bbcbf6067d1c1372e843351900b8425acb793ce78fbcba2946eeedef
MD5 6b8c377e28e9f99fdef3dc721321c19d
BLAKE2b-256 4545ae0e7a6c9bf21e4e989b4f6faaa12224cbb3625d1378671a2865aae73815

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page