Skip to main content

Generalized Luhn algorithm to any numeric base

Project description

Generalized Luhn algorithm to any numerical base

Luhn algorithm is described in Annex B of ISO/IEC 7812-1:2017. As it is described at Luhn's Wikipedia page, the algorithm is used to compute check digits and check them in identification numbers, like the usually found in credit card numbers. The standard algorithm uses 10 as numeric base for the check digit.

But there are other scenarios, like nih URIs at IETF RFC 6920, where the numeric base of the check digit to be computed and validated is 16 (see examples at section 8.2 of RFC 6920).

This library is just an implementation of the algorithm for any base. There are two methods, compute and validate, which take as first parameter either an integer, or a string representing the number in the base, or an array of integers, where the value of each position is the corresponding numeric digit. compute method computes the check digit, meanwhile validate takes the whole identification number, the numerical base and the check digit, and answers either True or False.

genluhn.compute('5326-9057-e12f-e2b7-4ba0-7c89-2560-a2',16)
# It return 15

genluhn.validate('5326-9057-e12f-e2b7-4ba0-7c89-2560-a2', 16, 15)
# It returns True

genluhn.validate('5326-9057-e12f-e2b7-4ba0-7c89-2560-a2', 16, 14)
# It returns False

Accessory methods of this library are intToDigits, strToDigits and bytesToDigits, which are used when either an integer, or a string or a byteslike object have to be translated to a list of digits in the given numerical base.

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

genluhn-0.3.1.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

genluhn-0.3.1-py3-none-any.whl (14.0 kB view details)

Uploaded Python 3

File details

Details for the file genluhn-0.3.1.tar.gz.

File metadata

  • Download URL: genluhn-0.3.1.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for genluhn-0.3.1.tar.gz
Algorithm Hash digest
SHA256 a92a158501a23c9e951e61e227a3bb7952b352ea85e2c3d979b0883c2f6782b5
MD5 a96513464b955521fe13aaf04f3277ae
BLAKE2b-256 a869bc22c70131f1d63c08a6f9e6673579d764579764f3d82675a46ca29f3dc6

See more details on using hashes here.

File details

Details for the file genluhn-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: genluhn-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 14.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.8.16

File hashes

Hashes for genluhn-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fd7e12e9f0a4cbe12d3a2e153d6363901cf0318eda64fa64122360fb47b4b73b
MD5 74252ba3a9b767d017ee4fddfc1b47f5
BLAKE2b-256 a7cbc85fab03018d425f4b5ecf6c4d86994ab2fb2cfa6d53cbd3cd0c2536c50a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page