Skip to main content

Simple validator for identification numbers based on the Luhn algorithm

Project description

luhncheck

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

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.7+ 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.

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

luhncheck-2.0.0.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

luhncheck-2.0.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file luhncheck-2.0.0.tar.gz.

File metadata

  • Download URL: luhncheck-2.0.0.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for luhncheck-2.0.0.tar.gz
Algorithm Hash digest
SHA256 a92195deeb6935740c7efc13b53b28116f84d55585ce5bab91181214b9f5276f
MD5 9579c9d4bcdcd402170ad587f293523a
BLAKE2b-256 4c310c7358ef0bcb65840cc8d48a300a9b6d2a54ab0b64656a2cd242372f7967

See more details on using hashes here.

File details

Details for the file luhncheck-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: luhncheck-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 4.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.3

File hashes

Hashes for luhncheck-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d5ffec5935b7edde35f64b870bd0eb9b90f067ef12e6f68fe0aa8acba5746fdc
MD5 e54ec295c47bedc08423809181c56778
BLAKE2b-256 0d5bd85e0e6fd7f2ff8f6035f05d8970d68d5c60c98df73407211e9b22dd7602

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