setattr(c, j[plen:], classmethod(b[j]))
for i in [gcciphers, gchashes, gcmacs, gcprps]:
for c in i.itervalues():
- d[c.name.replace('-', '_')] = c
+ d[c.name.replace('-', '_').translate(None, '/')] = c
for c in gccrands.itervalues():
- d[c.name.replace('-', '_') + 'rand'] = c
+ d[c.name.replace('-', '_').translate(None, '/') + 'rand'] = c
_init()
## A handy function for our work: add the methods of a named class to an
def __repr__(me):
return 'bytes(%r)' % hex(me)
_augment(ByteString, _tmp)
+ByteString.__hash__ = str.__hash__
bytes = ByteString.fromhex
+###--------------------------------------------------------------------------
+### Hashing.
+
+class _tmp:
+ def check(me, h):
+ hh = me.done()
+ return ctstreq(h, hh)
+_augment(GHash, _tmp)
+_augment(Poly1305Hash, _tmp)
+
+###--------------------------------------------------------------------------
+### NaCl `secretbox'.
+
+def secret_box(k, n, m):
+ E = xsalsa20(k).setiv(n)
+ r = E.enczero(poly1305.keysz.default)
+ s = E.enczero(poly1305.masksz)
+ y = E.encrypt(m)
+ t = poly1305(r)(s).hash(y).done()
+ return ByteString(t + y)
+
+def secret_unbox(k, n, c):
+ E = xsalsa20(k).setiv(n)
+ r = E.enczero(poly1305.keysz.default)
+ s = E.enczero(poly1305.masksz)
+ y = c[poly1305.tagsz:]
+ if not poly1305(r)(s).hash(y).check(c[0:poly1305.tagsz]):
+ raise ValueError, 'decryption failed'
+ return E.decrypt(c[poly1305.tagsz:])
+
###--------------------------------------------------------------------------
### Multiprecision integers and binary polynomials.