Encoding and decoding arbitrary strings into strings that are safe to put into a URL query param.
Project description
[![Build Status](https://travis-ci.org/ClearcodeHQ/querystringsafe_base64.svg?branch=master)](https://travis-ci.org/ClearcodeHQ/querystringsafe_base64) [![Coverage Status](https://img.shields.io/coveralls/ClearcodeHQ/querystringsafe_base64.svg)](https://coveralls.io/r/ClearcodeHQ/querystringsafe_base64)
# 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
= sign gets quoted:
>>> import urllib
>>> urllib.quote('=') '%3D'
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('foo-bar') >>> 'Zm9vLWJhcg..'>>> querystringsafe_base64.decode('Zm9vLWJhcg..') >>> 'foo-bar'
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for querystringsafe_base64-0.1.5.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | d86cfb1577d20a7a9e61a01828805034b2e001061addabdf43ff97ae4bdaaa62 |
|
MD5 | 036ddb989e297e0b2d580434e088cc2f |
|
BLAKE2b-256 | 37b356bf3c2f41f635f9a02240ffd4735a86303ee7bb02a38cf0a32d8de8662f |
Hashes for querystringsafe_base64-0.1.5-py2.7.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | 70a3af691783e3187dbc5755e339a670b6ed2496f0f82b1ec4f4f0a68e75493e |
|
MD5 | 01108485ce3d58988f2273097edf4e57 |
|
BLAKE2b-256 | bfab11bc71cfa9708cd248f5175c4e972ab40a00e89a3c1a7ee839acfd9dea1e |
Hashes for querystringsafe_base64-0.1.5-py2.6.egg
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6d9dced617bb2f40b4cb56379c6dd3e7b92ef30338ecdded521671b67971c235 |
|
MD5 | bac7aee626b1186644510a0d06ba329e |
|
BLAKE2b-256 | 8f5f94c8ede06449458d22717bac69b0681cf93cf8e21397dbda7f9c2d0e4867 |