From c4b97e160d6f6a3e4ac6a940c1bda0c6759ca4e8 Mon Sep 17 00:00:00 2001 From: Fredrik Tolf Date: Fri, 18 Jul 2014 10:03:14 +0200 Subject: [PATCH] Added more sane wrappings of the binascii functions. --- wrw/auth.py | 8 ++------ wrw/proto.py | 36 +++++++++++++++++++++++++++++++++++- wrw/session.py | 6 +++--- 3 files changed, 40 insertions(+), 10 deletions(-) diff --git a/wrw/auth.py b/wrw/auth.py index 1858214..27e2a98 100644 --- a/wrw/auth.py +++ b/wrw/auth.py @@ -1,5 +1,5 @@ import binascii, hashlib, threading, time -from . import resp +from . import resp, proto class unauthorized(resp.httperror): def __init__(self, challenge, message=None, detail=None): @@ -31,11 +31,7 @@ def parsebasic(req): if mech != "basic": return None, None try: - data = data.encode("us-ascii") - except UnicodeError: - return None, None - try: - raw = binascii.a2b_base64(data) + raw = proto.unb64(data) except binascii.Error: return None, None try: diff --git a/wrw/proto.py b/wrw/proto.py index f03fd2b..58bd626 100644 --- a/wrw/proto.py +++ b/wrw/proto.py @@ -1,4 +1,4 @@ -import time, calendar +import time, calendar, collections, binascii, base64 statusinfo = { 400: ("Bad Request", "Invalid HTTP request."), @@ -195,3 +195,37 @@ def parurl(url, pars={}, **augment): return url + "?" + qs else: return url + +# Wrap these, since binascii is a bit funky. :P +def enhex(bs): + return base64.b16encode(bs).decode("us-ascii") +def unhex(es): + if not isinstance(es, collections.ByteString): + try: + es = es.encode("us-ascii") + except UnicodeError: + raise binascii.Error("non-ascii character in hex-string") + return base64.b16decode(es) +def enb32(bs): + return base64.b32encode(bs).decode("us-ascii") +def unb32(es): + if not isinstance(es, collections.ByteString): + try: + es = es.encode("us-ascii") + except UnicodeError: + raise binascii.Error("non-ascii character in base32-string") + if (len(es) % 8) != 0: + es += b"=" * (8 - (len(es) % 8)) + es = es.upper() # The whole point of Base32 is that it's case-insensitive :P + return base64.b32decode(es) +def enb64(bs): + return base64.b64encode(bs).decode("us-ascii") +def unb64(es): + if not isinstance(es, collections.ByteString): + try: + es = es.encode("us-ascii") + except UnicodeError: + raise binascii.Error("non-ascii character in base64-string") + if (len(es) % 4) != 0: + es += b"=" * (4 - (len(es) % 4)) + return base64.b64decode(es) diff --git a/wrw/session.py b/wrw/session.py index f9b4a3f..8141827 100644 --- a/wrw/session.py +++ b/wrw/session.py @@ -1,5 +1,5 @@ -import threading, time, pickle, random, os, binascii -from . import cookie, env +import threading, time, pickle, random, os +from . import cookie, env, proto __all__ = ["db", "get"] @@ -8,7 +8,7 @@ def gennonce(length): class session(object): def __init__(self, lock, expire=86400 * 7): - self.id = binascii.b2a_hex(gennonce(16)).decode("us-ascii") + self.id = proto.enhex(gennonce(16)) self.dict = {} self.lock = lock self.ctime = self.atime = self.mtime = int(time.time()) -- 2.11.0