from base64 import b32encode, b32decode def b2a(i): assert isinstance(i, str), "tried to idlib.b2a non-string '%s'" % (i,) return b32encode(i).lower() def b2a_or_none(i): if i is None: return None return b2a(i) def a2b(i): assert isinstance(i, str), "tried to idlib.a2b non-string '%s'" % (i,) try: return b32decode(i.upper()) except TypeError: print "b32decode failed on a %s byte string '%s'" % (len(i), i) raise def peerid_to_short_string(peerid): return b2a(peerid)[:4]