Skip to main content
Tests Coverage Status

Query string safe Base64

Encoding and decoding arbitrary strings into strings that are safe to put into a URL query param.

The problem

urlsafe_b64encode and urlsafe_b64decode from base64 are not enough because they leave = used for padding chars unquoted:

import base64

base64.urlsafe_b64encode('a')
'YQ=='

And there are 2 problems with that

  1. = sign gets quoted:

import urllib

urllib.quote('=')
'%3D'
  1. Some libraries tolerate the = in query string values:

from urlparse import urlsplit, parse_qs

parse_qs(urlsplit('http://aaa.com/asa?q=AAAA=BBBB=CCCC').query)
{'q': ['AAAA=BBBB=CCCC']}

but the RFC 3986 underspecifies the query string so we cannot rely on = chars being handled by all web applications as it is done by urlparse.

Therefore we consider chars: [‘+’, ‘/’, ‘=’] unsafe and we replace them with [‘-’, ‘_’, ‘.’]. Characters + and / are already handled by urlsafe_* functions from base64 so only = is left. Since the = is used exclusively for padding, we simply remove it, and re-attach the padding during decoding. Because of that, querystringsafe_base64 is able to decode padded and unpadded string.

The solution

import querystringsafe_base64

querystringsafe_base64.encode(b'foo-bar')
b'Zm9vLWJhcg'

querystringsafe_base64.decode(b'Zm9vLWJhcg..')
b'foo-bar'

querystringsafe_base64.decode(b'Zm9vLWJhcg')
b'foo-bar'

CHANGELOG

1.2.0

  • Remove padding characters from encoded string.

1.1.1

  • Fixed packaging

1.1.0

  • Always expect bytes

  • Add type annotations

1.0.0

  • support for restore missing padding during decode process

0.2.0

  • Support for python3

0.1.5

  • Move querystringsafe_base64 module to the root

  • Use install instead of develop during tests

0.1.4

  • Remove bdist_wheel from distributons

0.1.3

  • Install pandoc (travis)

0.1.2

  • Add setup.cfg and pypandoc to tests

0.1.1

  • add MANIFEST.in file

0.1.0

  • package structure

  • tests

Release files for querystringsafe-base64 1.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for querystringsafe-base64 1.2.0
File Interpreter ABI Platform
querystringsafe_base64-1.2.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Release files / querystringsafe_base64-1.2.0-py2.py3-none-any.whl

Download URL querystringsafe_base64-1.2.0-py2.py3-none-any.whl
Size 7.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
850913d05558d31a6bdb90711c3ef5ca855d8e1730bf8bc0ca2bcc1ef273c130
BLAKE2b-256 checksum
How to use checksums
be13b11c71984a9bcc59dc09ca9b487673731b79fef37422fbeeca7cc54d5d7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/2.7.15

Release history Release notifications | RSS feed

This release

1.2.0 This release

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.0

1 release file

0.2.0

6 release files

0.1.5

3 release files

0.1.4

1 release file

0.1.3

0.1.1

0.1.0

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page