base32-lib

https://img.shields.io/travis/inveniosoftware/base32-lib.svg https://img.shields.io/coveralls/inveniosoftware/base32-lib.svg https://img.shields.io/github/tag/inveniosoftware/base32-lib.svg https://img.shields.io/pypi/dm/base32-lib.svg https://img.shields.io/github/license/inveniosoftware/base32-lib.svg

Small library to generate, encode and decode random base32 strings with nice properties.

Usage

import base32_lib as base32

# Generate
## Generate a random identifier
base32.generate()
# -> 'abcd1234'

# Generate a random identifier with bells and whistle
base32.generate(length=10, split_every=5, checksum=True)
# -> '3sbk2-5j060'

# Encode a pre-existing number
base32.encode(1234, split_every=3, checksum=True) == "16j-82"

# Decode an identifier
base32.decode("16j-82", checksum=True) == 1234

base32.decode("16i-82", checksum=True)
# raises ValueError

Features

  • Generation, encoding and decoding of base32 strings
  • Douglas Crockford base32 encoding
  • URL-safe strings with no problematic special characters
  • Decoding of any-case strings
  • Configurable length strings
  • Randomness through cryptographically secure random number generator
  • Optional ISO-7064 checksum
  • Optional hyphenation

Installation

The base32-lib package is on PyPI so all you need is:

pip install base32-lib

Development

pipenv run pip install -e .[docs,tests]

Tests

pipenv run ./run-tests.sh

API

Small library to generate, encode and decode random base32 identifiers.

base32_lib.generate(length=8, split_every=0, checksum=False)[source]

Generate random base32 string.

Parameters:
  • length – non-hyphen identifier length including checksum
  • split_every – hyphenates every that many characters
  • checksum – computes and appends ISO-7064 checksum
Returns:

identifier as a string

base32_lib.encode(number, split_every=0, min_length=0, checksum=False)[source]

Encodes number to URI-friendly Douglas Crockford base32 string.

Parameters:
  • number – number to encode
  • split_every – if provided, insert ‘-‘ every split_every characters going from left to right
  • checksum – append modulo 97-10 (ISO 7064) checksum to string
Returns:

A random Douglas Crockford base32 encoded string composed only of valid URI characters.

base32_lib.decode(encoded, checksum=False)[source]

Decodes encoded string (via above) to a number.

The string is normalized before decoding.

If checksum is enabled, raises a ValueError on checksum error.

Parameters:
  • encoded – string to decode
  • checksum – extract checksum and validate
Returns:

original number.

Changes

Version 1.0.2 (2020-05-07)

  • Deprecate Python versions lower than 3.6.0. Now supporting 3.6.0 and 3.7.0.

Version 1.0.1 (2019-11-15)

  • Enforce appropriate length of the encoded base32 strings.

Version 1.0.0 (2019-11-06)

  • First public release.

Contributing

Bug reports, feature requests, and other contributions are welcome. If you find a demonstrable problem that is caused by the code of this library, please:

  1. Search for already reported problems.
  2. Check if the issue has been fixed or is still reproducible on the latest master branch.
  3. Create an issue with a test case.

If you create a feature branch, you can run the tests to ensure everything is operating correctly:

$ ./run-tests.sh

License

MIT License

Copyright (C) 2019 CERN.

Copyright (C) 2019 Northwestern University, Galter Health Sciences Library & Learning Center.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Authors

  • Guillaume Viger
  • Lars Holm Nielsen
  • Pablo Panero