Skip to main content

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

Project description

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 = 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 for us. The . character has been chosen because it often appears in real world query strings and it is not used by base64.

The solution

import querystringsafe_base64

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

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

CHANGELOG

1.1.1

  • Fixed pacakging

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

querystringsafe_base64-1.1.1-py2.py3-none-any.whl (6.0 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file querystringsafe_base64-1.1.1-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for querystringsafe_base64-1.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 bad9187c0406456020ee354be7f51d2e0dfc262dadde0449cbdd1fd78d437eed
MD5 51ca2ed2b1cf84eb4427942a44bbf9bc
BLAKE2b-256 50b8893818f11d2c9035347685a30db643e2cad18aa6ead0568da7074fc9eb69

See more details on using hashes here.

Supported by

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