Simple validator for identification numbers based on the Luhn algorithm
Project description
luhn-validator
A Python package to validate identification numbers using 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 as a simple method of distinguishing 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/or prefix(es).
- Can validate numbers containing hyphens or spaces.
- 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
>>> # Simple checksum validation with hyphens and prefix of string type
>>> validate("01-055102-109831-4", None, "01")
True
>>> # Simple checksum validation using integer type
>>> validate(4146274478407735)
True
API Reference
function validate
validate(
number: Union[str, int],
length: Optional[int] = None,
prefix: Optional[Union[Union[str, int], List[Union[str, int]]]] = None,
) -> bool
Validate format and checksum of an identification number based on Luhn algorithm.
Args:
number
: Identification number to validate (str
orint
).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 (str
orint
). 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, otherwiseFalse
.
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 luhn-validator-1.1.2.tar.gz
.
File metadata
- Download URL: luhn-validator-1.1.2.tar.gz
- Upload date:
- Size: 6.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 64294d77920942ed450ed7d4e969fa4899b604518f249879f55fb3ed20eb9c3e |
|
MD5 | 4c5d1b9fe56d8d563895d03d235d8356 |
|
BLAKE2b-256 | 160c26b5bdadefda52c15c160896184c74d72f31176b26df072727ed5f5cfd72 |
File details
Details for the file luhn_validator-1.1.2-py3-none-any.whl
.
File metadata
- Download URL: luhn_validator-1.1.2-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 873325e913980e13213f4c9104c22b1452c7992873656913361a2e24ca1802a5 |
|
MD5 | 209cb7df5493639a23ca16738804e8c8 |
|
BLAKE2b-256 | 01f615130fc2d2efea614ab31eda8d345132340e8de49fab5911721bc8c26a28 |