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
- Full type annotations and 100% test coverage
- Zero runtime dependencies
📦 Installation
To install using uv, run:
uv add luhncheck
To install using pip, run:
pip install luhncheck
🚀 Basic Usage
from luhncheck import is_luhn
# Simple checksum validation
print(is_luhn("1101798278")) # True
# Additional check for length (9 digits)
print(is_luhn("1101798278", 9)) # False
# Additional checks for prefix (either 1 or 2)
print(is_luhn("1101798278", 10, ["1", "2"])) # True
# Validate numbers containing hyphens
print(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:
Truewhen the number is valid; otherwise,False.
🤝 Contributing
If you're interested in contributing, please check out the Contributing guide for more information on how you can help!
📄 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file luhncheck-2.3.0.tar.gz.
File metadata
- Download URL: luhncheck-2.3.0.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cdc48a221c3e7459b744e4f9fd1ed3148fccd8977c30aca1245bd09758c26204
|
|
| MD5 |
4dad2337ea667efdaf95a99e2af7899a
|
|
| BLAKE2b-256 |
650062fdccc0e78850f9f4fe344a243bf723599c6e11486ec7573b83973cc248
|
Provenance
The following attestation bundles were made for luhncheck-2.3.0.tar.gz:
Publisher:
pypi-release.yml on dralshehri/luhncheck
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
luhncheck-2.3.0.tar.gz -
Subject digest:
cdc48a221c3e7459b744e4f9fd1ed3148fccd8977c30aca1245bd09758c26204 - Sigstore transparency entry: 804595864
- Sigstore integration time:
-
Permalink:
dralshehri/luhncheck@12cdc48d8af986eb33977c260e7d7509a0dd0b76 -
Branch / Tag:
refs/tags/v2.3.0 - Owner: https://github.com/dralshehri
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-release.yml@12cdc48d8af986eb33977c260e7d7509a0dd0b76 -
Trigger Event:
release
-
Statement type:
File details
Details for the file luhncheck-2.3.0-py3-none-any.whl.
File metadata
- Download URL: luhncheck-2.3.0-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
710ffad751297fa27c8087372dbbfeb560305c08c813777f9ef0a681b664cde7
|
|
| MD5 |
d4c074076ee649d9eb23a03007d6c6bf
|
|
| BLAKE2b-256 |
0581d0887114179704b542fb15eb820f7e03bfd865a044cfb9c1cb5863459b38
|
Provenance
The following attestation bundles were made for luhncheck-2.3.0-py3-none-any.whl:
Publisher:
pypi-release.yml on dralshehri/luhncheck
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
luhncheck-2.3.0-py3-none-any.whl -
Subject digest:
710ffad751297fa27c8087372dbbfeb560305c08c813777f9ef0a681b664cde7 - Sigstore transparency entry: 804595866
- Sigstore integration time:
-
Permalink:
dralshehri/luhncheck@12cdc48d8af986eb33977c260e7d7509a0dd0b76 -
Branch / Tag:
refs/tags/v2.3.0 - Owner: https://github.com/dralshehri
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi-release.yml@12cdc48d8af986eb33977c260e7d7509a0dd0b76 -
Trigger Event:
release
-
Statement type: