_sys.stderr.write("\n")
lostexchook = default_lostexchook
+## Text/binary conversions.
+def _bin(s): return s
+
+## Iterating over dictionaries.
+def _iteritems(dict): return dict.iteritems()
+def _itervalues(dict): return dict.itervalues()
+
## How to fix a name back into the right identifier. Alas, the rules are not
## consistent.
def _fixname(name):
if i[0] != '_':
d[i] = b[i];
for i in [gcciphers, gcaeads, gchashes, gcmacs, gcprps]:
- for c in i.itervalues():
+ for c in _itervalues(i):
d[_fixname(c.name)] = c
- for c in gccrands.itervalues():
+ for c in _itervalues(gccrands):
d[_fixname(c.name + 'rand')] = c
_init()
else: pp.text(','); pp.breakable()
printfn(i)
def _pp_dict(pp, items):
- def p((k, v)):
+ def p(kv):
+ k, v = kv
pp.begin_group(0)
pp.pretty(k)
pp.text(':')
def fromhex(x):
return ByteString(_unhexify(x))
fromhex = staticmethod(fromhex)
- def __hex__(me):
- return _hexify(me)
+ def hex(me): return _hexify(me)
+ __hex__ = hex
def __repr__(me):
- return 'bytes(%r)' % hex(me)
+ return 'bytes(%r)' % me.hex()
_augment(ByteString, _tmp)
ByteString.__hash__ = str.__hash__
bytes = ByteString.fromhex
### Symmetric encryption.
class _tmp:
- def encrypt(me, n, m, tsz = None, h = ByteString('')):
+ def encrypt(me, n, m, tsz = None, h = ByteString.zero(0)):
if tsz is None: tsz = me.__class__.tagsz.default
e = me.enc(n, len(h), len(m), tsz)
if not len(h): a = None
c0 = e.encrypt(m)
c1, t = e.done(aad = a)
return c0 + c1, t
- def decrypt(me, n, c, t, h = ByteString('')):
+ def decrypt(me, n, c, t, h = ByteString.zero(0)):
d = me.dec(n, len(h), len(c), len(t))
if not len(h): a = None
else: a = d.aad().hash(h)
## Python gets really confused if I try to augment `__new__' on native
## classes, so wrap and delegate. Sorry.
- def __init__(me, perso = '', *args, **kw):
+ def __init__(me, perso = _bin(''), *args, **kw):
super(_ShakeBase, me).__init__(*args, **kw)
me._h = me._SHAKE(perso = perso, func = me._FUNC)
Shake._Z = _ShakeBase._Z = ByteString.zero(200)
class KMAC (_ShakeBase):
- _FUNC = 'KMAC'
+ _FUNC = _bin('KMAC')
def __init__(me, k, *arg, **kw):
super(KMAC, me).__init__(*arg, **kw)
with me.bytepad(): me.stringenc(k)
me.rightenc(0)
return super(KMAC, me).xof()
@classmethod
- def _bare_new(cls): return cls("")
+ def _bare_new(cls): return cls(_bin(""))
class KMAC128 (KMAC): _SHAKE = Shake128; _TAGSZ = 16
class KMAC256 (KMAC): _SHAKE = Shake256; _TAGSZ = 32
return type(me)(me._d*n, me._n*d)
__div__ = __truediv__
__rdiv__ = __rtruediv__
- def __cmp__(me, you):
- n, d = _split_rat(you)
- return cmp(me._n*d, n*me._d)
- def __rcmp__(me, you):
+ def _order(me, you, op):
n, d = _split_rat(you)
- return cmp(n*me._d, me._n*d)
+ return op(me._n*d, n*me._d)
+ def __eq__(me, you): return me._order(you, lambda x, y: x == y)
+ def __ne__(me, you): return me._order(you, lambda x, y: x != y)
+ def __le__(me, you): return me._order(you, lambda x, y: x <= y)
+ def __lt__(me, you): return me._order(you, lambda x, y: x < y)
+ def __gt__(me, you): return me._order(you, lambda x, y: x > y)
+ def __ge__(me, you): return me._order(you, lambda x, y: x >= y)
class IntRat (BaseRat):
RING = MP
class _tmp:
def __repr__(me):
return '%s({%s})' % (_clsname(me),
- ', '.join(['%r: %r' % kv for kv in me.iteritems()]))
+ ', '.join(['%r: %r' % kv for kv in _iteritems(me)()]))
def _repr_pretty_(me, pp, cyclep):
ind = _pp_bgroup_tyname(pp, me)
if cyclep: pp.text('...')
- else: _pp_dict(pp, me.iteritems())
+ else: _pp_dict(pp, _iteritems(me))
pp.end_group(ind, ')')
_augment(KeyAttributes, _tmp)
class _tmp:
def __repr__(me):
return '%s({%s})' % (_clsname(me),
- ', '.join(['%r: %r' % kv for kv in me.iteritems()]))
+ ', '.join(['%r: %r' % kv for kv in _iteritems(me)]))
def _repr_pretty_(me, pp, cyclep):
ind = _pp_bgroup_tyname(pp, me, '({ ')
if cyclep: pp.text('...')
- else: _pp_dict(pp, me.iteritems())
+ else: _pp_dict(pp, _iteritems(me))
pp.end_group(ind, ' })')
_augment(KeyDataStructured, _tmp)
### RSA encoding techniques.
class PKCS1Crypt (object):
- def __init__(me, ep = '', rng = rand):
+ def __init__(me, ep = _bin(''), rng = rand):
me.ep = ep
me.rng = rng
def encode(me, msg, nbits):
return _base._p1crypt_decode(ct, nbits, me.ep, me.rng)
class PKCS1Sig (object):
- def __init__(me, ep = '', rng = rand):
+ def __init__(me, ep = _bin(''), rng = rand):
me.ep = ep
me.rng = rng
def encode(me, msg, nbits):
return _base._p1sig_decode(msg, sig, nbits, me.ep, me.rng)
class OAEP (object):
- def __init__(me, mgf = sha_mgf, hash = sha, ep = '', rng = rand):
+ def __init__(me, mgf = sha_mgf, hash = sha, ep = _bin(''), rng = rand):
me.mgf = mgf
me.hash = hash
me.ep = ep
class _tmp:
def __repr__(me):
- return '{%s}' % ', '.join(['%r: %r' % kv for kv in me.iteritems()])
+ return '{%s}' % ', '.join(['%r: %r' % kv for kv in _iteritems(me)])
def _repr_pretty_(me, pp, cyclep):
ind = _pp_bgroup(pp, '{ ')
if cyclep: pp.text('...')
- else: _pp_dict(pp, me.iteritems())
+ else: _pp_dict(pp, _iteritems(me))
pp.end_group(ind, ' }')
_augment(_base._MiscTable, _tmp)