Registry / auth-security / luhn
library0.2.0pypypi✓ verified 85d ago

The `luhn` Python library, currently at version 0.2.0, provides a simple and lightweight implementation of the Luhn algorithm. It's designed to generate and verify Luhn check digits for identification numbers, commonly used for credit cards and other IDs to detect accidental errors. This version was released in 2015 and has a stable, albeit minimal, API.

pip install luhn
INSTALL
IMPORT
SIG · LUHN
L
luhn
auth-securitypythonv0.2.0
Install
2.4s avg
Import
Disk
17MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.2.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.2MB
glibc
py 3.103.920 runs
installs and imports cleanly · install 2.4s · import 0.000s · 20MB
17MB installed
● package 17MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

verify
from luhn import verify
generate
from luhn import generate
append
from luhn import append
*
from luhn import *
Commonly used for convenience to import all functions, but explicit imports are generally recommended for clarity.

Demonstrates how to verify a number, generate a check digit, and append a check digit using the `luhn` library's primary functions.

from luhn import verify, generate, append # Verify a Luhn compliant string valid_number = '79927398713' invalid_number = '79927398710' print(f"'{valid_number}' is valid: {verify(valid_number)}") print(f"'{invalid_number}' is valid: {verify(invalid_number)}") # Generate a check digit for a string base_number = '53461861341123' check_digit = generate(base_number) print(f"Check digit for '{base_number}' is: {check_digit}") # Append a check digit to a string appended_number = append(base_number) print(f"'{base_number}' with check digit appended: {appended_number}")
Debug
Known issues
gotchaThe `luhn` library expects string input consisting solely of digits. Passing non-digit characters (e.g., spaces, hyphens, letters) will result in a `ValueError` during the internal conversion to integers.
fix
Ensure input strings are stripped of any non-digit characters before passing them to `verify`, `generate`, or `append`. For example, `number_string.replace(' ', '').replace('-', '')`.
affects: 0.1.0, 0.1.1, 0.2.0
gotchaThe Luhn algorithm itself is designed to detect single-digit errors and most adjacent digit transpositions, but it is not cryptographically secure and will not detect all types of transcription errors (e.g., '09' to '90' transposition).
fix
For applications requiring higher security or more robust error detection, consider alternative or additional checksum algorithms, such as Verhoeff or Damm algorithms, or cryptographic hash functions.
affects: All versions (inherent to Luhn algorithm)
deprecatedThis library (version 0.2.0, released in 2015) is older and may not receive active development or updates. Newer, more actively maintained Luhn implementations (e.g., `luhncheck`) are available on PyPI, offering additional features or potentially better performance.
fix
While functional for basic Luhn operations, consider evaluating more recent alternatives like `luhncheck` if your project requires new features, active maintenance, or broader compatibility.
affects: 0.2.0 and earlier
Errors
Common errors & fixes
ValueError: invalid literal for int() with base 10:
The input string contained non-digit characters (e.g., spaces, hyphens, letters) which cannot be converted to an integer by the library's internal `map(int, string)` call.
fix
Pre-process the input string to remove any non-digit characters. Example: `cleaned_number = input_string.strip().replace(' ', '').replace('-', '')`.
TypeError: 'int' object is not iterable
An integer was passed to a function (e.g., `verify`, `generate`, `append`) that expects a string of digits.
fix
Convert the integer to a string before passing it to the `luhn` functions. Example: `verify(str(12345))`.
Incorrect Luhn validation result for long numbers or specific patterns.
This is typically not a bug in the `luhn` library itself (which correctly implements the standard algorithm), but rather a misunderstanding of the Luhn algorithm's limitations, such as its inability to detect certain transposition errors (e.g., 09-90).
fix
Review the specifications of the Luhn algorithm to understand its inherent strengths and weaknesses. If more robust error detection is needed, consider alternative checksums.
Upgrade
Version history
0.2.0latest on PyPI · released Jun 23, 2015
Audit
Dependencies

No dependency data recorded yet.

Agent activity
17 hits · last 30 days
node
14
OpenAI (training)
1
Resources
luhn — pip install luhn · libregistry