|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf8 -*- |
| 3 | + |
| 4 | +"""Encode and decode hexahexacontadecimal numbers. |
| 5 | +
|
| 6 | +Hexahexacontadecimal is a compact format to express a number in a URL. It uses all characters allowed in |
| 7 | +a URL without escaping -- the [unreserved characters](http://tools.ietf.org/html/rfc3986#section-2.3) -- |
| 8 | +making it the shortest possible way to express an integer in a URL. |
| 9 | +
|
| 10 | +Note that urllib.quote escapes tilde (~) (http://bugs.python.org/issue16285), which is not necessary as |
| 11 | +of RFC3986. |
| 12 | +
|
| 13 | +## Hexahexacontadecimal vs Base 64 in URLs |
| 14 | +
|
| 15 | +>>> n = 292231454903657293676544 |
| 16 | +>>> import base64 |
| 17 | +>>> urlquote(base64.urlsafe_b64encode(long_to_binary(n))) |
| 18 | +'PeHmHzZFTcAAAA%3D%3D' |
| 19 | +>>> urlquote(hexahexacontadecimal_encode_int(n)) |
| 20 | +'gpE4Xoy7fw5AO' |
| 21 | +
|
| 22 | +Worst case scenario for plain Base 64: |
| 23 | +
|
| 24 | +>>> n = 64 ** 5 + 1 |
| 25 | +>>> urlquote(base64.urlsafe_b64encode(long_to_binary(n))) |
| 26 | +'QAAAAQ%3D%3D' |
| 27 | +>>> urlquote(hexahexacontadecimal_encode_int(n)) |
| 28 | +'ucrDZ' |
| 29 | +
|
| 30 | +Worst case for hexahexacontadecimal: |
| 31 | +
|
| 32 | +>>> n = 66 ** 5 + 1 |
| 33 | +>>> urlquote(base64.urlsafe_b64encode(long_to_binary(n))) |
| 34 | +'SqUUIQ%3D%3D' |
| 35 | +>>> urlquote(hexahexacontadecimal_encode_int(n)) |
| 36 | +'100001' |
| 37 | +
|
| 38 | +That big SHA-512 you always wanted to write in a URL: |
| 39 | +
|
| 40 | +>>> n = 2 ** 512 |
| 41 | +>>> urlquote(base64.urlsafe_b64encode(long_to_binary(n))) |
| 42 | +'AQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%3D' |
| 43 | +>>> urlquote(hexahexacontadecimal_encode_int(n)) |
| 44 | +'JK84xqGD9FMXPNubPghADlRhBUzlqRscC2h~8xmi99PvuQsUCIB2CHGhMUQR8FLm72.Hbbctkqi89xspay~y4' |
| 45 | +
|
| 46 | +## Are the savings really significant? |
| 47 | +
|
| 48 | +If you're currently doing your BASE64 encoding the naive way, then yes: |
| 49 | +
|
| 50 | +>>> sum(len(urlquote(base64.urlsafe_b64encode(long_to_binary(n)))) for n in xrange(10 ** 5)) |
| 51 | +531584 |
| 52 | +>>> sum(len(urlquote(hexahexacontadecimal_encode_int(n))) for n in xrange(10 ** 5)) |
| 53 | +295578 |
| 54 | +
|
| 55 | +## But what if I use Base64 without padding? |
| 56 | +
|
| 57 | +Then the savings are not as significant. But it's still an improvement. Using the code from http://stackoverflow.com/a/561704/76900: |
| 58 | +
|
| 59 | +>>> from hexahexacontadecimal.num_encode_base64 import num_encode as num_encode_base64 |
| 60 | +>>> n = 64 ** 5 + 1 |
| 61 | +>>> urlquote(num_encode_base64(n)) |
| 62 | +'BAAAAB' |
| 63 | +>>> urlquote(hexahexacontadecimal_encode_int(n)) |
| 64 | +'ucrDZ' |
| 65 | +>>> n = 66 ** 5 + 1 |
| 66 | +>>> urlquote(num_encode_base64(n)) |
| 67 | +'BKpRQh' |
| 68 | +>>> urlquote(hexahexacontadecimal_encode_int(n)) |
| 69 | +'100001' |
| 70 | +>>> n = 2 ** 512 |
| 71 | +>>> urlquote(num_encode_base64(n)) |
| 72 | +'EAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA' |
| 73 | +>>> urlquote(hexahexacontadecimal_encode_int(n)) |
| 74 | +'JK84xqGD9FMXPNubPghADlRhBUzlqRscC2h~8xmi99PvuQsUCIB2CHGhMUQR8FLm72.Hbbctkqi89xspay~y4' |
| 75 | +>>> sum(len(urlquote(num_encode_base64(n))) for n in xrange(10 ** 5)) |
| 76 | +295840 |
| 77 | +>>> sum(len(urlquote(hexahexacontadecimal_encode_int(n))) for n in xrange(10 ** 5)) |
| 78 | +295578 |
| 79 | +
|
| 80 | +Why settle for less? |
| 81 | +
|
| 82 | +""" |
| 83 | + |
| 84 | +from io import StringIO |
| 85 | +import urllib |
| 86 | + |
| 87 | +BASE66_ALPHABET = u"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_.~" |
| 88 | +BASE = len(BASE66_ALPHABET) |
| 89 | + |
| 90 | + |
| 91 | +def urlquote(s, safe=None): |
| 92 | + """Like urllib.quote() but don't escape ~, in accordance with RFC3986.""" |
| 93 | + |
| 94 | + return urllib.quote(s, safe='~' + (safe or '')) |
| 95 | + |
| 96 | + |
| 97 | +def long_to_binary(n): |
| 98 | + """Take an integer and write it as a binary string. |
| 99 | +
|
| 100 | + >>> long_to_binary(0) |
| 101 | + '\\x00' |
| 102 | + >>> long_to_binary(255) |
| 103 | + '\\xff' |
| 104 | + >>> long_to_binary(512 + 3) |
| 105 | + '\\x02\\x03' |
| 106 | + """ |
| 107 | + |
| 108 | + h = '%x' % n |
| 109 | + return ('0' * (len(h) % 2) + h).decode('hex') |
| 110 | + |
| 111 | + |
| 112 | +def binary_to_long(b): |
| 113 | + """Take a binary string and read it as an integer. |
| 114 | +
|
| 115 | + >>> binary_to_long('\\x00') |
| 116 | + 0 |
| 117 | + >>> binary_to_long('\\xff') |
| 118 | + 255 |
| 119 | + >>> binary_to_long('\\x02\\x03') |
| 120 | + 515 |
| 121 | + """ |
| 122 | + |
| 123 | + return int(b.encode('hex'), 16) |
| 124 | + |
| 125 | + |
| 126 | +def hexahexacontadecimal_encode_int(n): |
| 127 | + """Represent a number in hexahexacontadecimal, a compact format of unreserved URL characters. |
| 128 | +
|
| 129 | + >>> hexahexacontadecimal_encode_int(0) |
| 130 | + '0' |
| 131 | + >>> hexahexacontadecimal_encode_int(1) |
| 132 | + '1' |
| 133 | + >>> hexahexacontadecimal_encode_int(65) |
| 134 | + '~' |
| 135 | + >>> hexahexacontadecimal_encode_int(66) |
| 136 | + '10' |
| 137 | + >>> hexahexacontadecimal_encode_int(67) |
| 138 | + '11' |
| 139 | + >>> hexahexacontadecimal_encode_int(302231454903657293676544) |
| 140 | + 'iFsGUkO.0tsxw' |
| 141 | +
|
| 142 | + """ |
| 143 | + |
| 144 | + if n == 0: |
| 145 | + return BASE66_ALPHABET[0].encode('ascii') |
| 146 | + |
| 147 | + r = StringIO() |
| 148 | + while n: |
| 149 | + n, t = divmod(n, BASE) |
| 150 | + r.write(BASE66_ALPHABET[t]) |
| 151 | + return r.getvalue().encode('ascii')[::-1] |
| 152 | + |
| 153 | + |
| 154 | +def hexahexacontadecimal_decode_int(s): |
| 155 | + """Parse a number expressed in hexahexacontadecimal as an integer (or long). |
| 156 | +
|
| 157 | + >>> hexahexacontadecimal_decode_int('0') |
| 158 | + 0 |
| 159 | + >>> hexahexacontadecimal_decode_int('1') |
| 160 | + 1 |
| 161 | + >>> hexahexacontadecimal_decode_int('~') |
| 162 | + 65 |
| 163 | + >>> hexahexacontadecimal_decode_int('10') |
| 164 | + 66 |
| 165 | + >>> hexahexacontadecimal_decode_int('11') |
| 166 | + 67 |
| 167 | + >>> hexahexacontadecimal_decode_int('iFsGUkO.0tsxw') |
| 168 | + 302231454903657293676544L |
| 169 | +
|
| 170 | + """ |
| 171 | + |
| 172 | + n = 0 |
| 173 | + for c in s: |
| 174 | + n = n * BASE + BASE66_ALPHABET.index(c) |
| 175 | + |
| 176 | + return n |
0 commit comments