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.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 isNone
, 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 isNone
, 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file luhncheck-2.2.0.tar.gz
.
File metadata
- Download URL: luhncheck-2.2.0.tar.gz
- Upload date:
- Size: 7.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45fdd6f9ff920ebb5e245a36160ce12a4933fa1d0acc788cf91ac02fa78218af |
|
MD5 | 57c74f74122fe1d2ca665f94a2a315d1 |
|
BLAKE2b-256 | c6ce7c7464f1faafcba5e9d8c9b693be4c42e51a90d787f3c5fb826f6e680d57 |
File details
Details for the file luhncheck-2.2.0-py3-none-any.whl
.
File metadata
- Download URL: luhncheck-2.2.0-py3-none-any.whl
- Upload date:
- Size: 4.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc5964d04bfd38541e580b4c83e42ba2b0b3f72284cd15dd56955f13a96a0479 |
|
MD5 | e8a632d149cb75e683e590cb62b3fcbf |
|
BLAKE2b-256 | 63755b8d0f5af760f9d5afbfd99b990d87b7ec8b58cec47d8cb18f96f4811856 |