[tahoe-lafs-trac-stream] [pycryptopp] #42: python hkdf

pycryptopp trac at tahoe-lafs.org
Mon Jan 9 18:32:58 UTC 2012


#42: python hkdf
------------------------+------------------------
Reporter:  dragonxue    |          Owner:  xue yu
    Type:  enhancement  |         Status:  new
Priority:  major        |      Milestone:  0.6.0
 Version:  0.5.19       |     Resolution:
Keywords:  hkdf         |  Launchpad Bug:
------------------------+------------------------

Comment (by zooko):

 The new Python hmac needs the hash function to have a DIGEST_SIZE
 attribute.

 {{{
 diff -rN -u old-darcsworld-hmac/pycryptopp/hash/sha256module.cpp new-
 darcsworld-hmac/pycryptopp/hash/sha256module.cpp
 --- old-darcsworld-hmac/pycryptopp/hash/sha256module.cpp        2012-01-09
 11:32:19.570933575 -0700
 +++ new-darcsworld-hmac/pycryptopp/hash/sha256module.cpp        2012-01-09
 11:32:19.612933575 -0700
 @@ -28,6 +28,8 @@
  typedef struct {
      PyObject_HEAD

 +       Py_ssize_t digest_size;
 +
      /* internal */
      CryptoPP::SHA256* h;
      PyStringObject* digest;
 @@ -59,7 +61,7 @@
  SHA256_digest(SHA256* self, PyObject* dummy) {
      if (!self->digest) {
          assert (self->h);
 -        self->digest =
 reinterpret_cast<PyStringObject*>(PyString_FromStringAndSize(NULL,
 self->h->DigestSize()));
 +        self->digest =
 reinterpret_cast<PyStringObject*>(PyString_FromStringAndSize(NULL,
 self->digest_size));
          if (!self->digest)
              return NULL;
 self->h->Final(reinterpret_cast<byte*>(PyString_AS_STRING(self->digest)));
 @@ -100,6 +102,12 @@
      {NULL},
  };

 +static PyMemberDef SHA256_members[] = {
 +    {"number", T_PYSSIZET, offsetof(SHA256, digest_Size), READONLY,
 +     "digest size"},
 +    {NULL}  /* Sentinel */
 +};
 +
  static PyObject *
  SHA256_new(PyTypeObject* type, PyObject *args, PyObject *kwdict) {
      SHA256* self = reinterpret_cast<SHA256*>(type->tp_alloc(type, 0));
 @@ -108,6 +116,7 @@
      self->h = new CryptoPP::SHA256();
      if (!self->h)
          return PyErr_NoMemory();
 +       self->digest_size = self->h->DigestSize();
      self->digest = NULL;
      return reinterpret_cast<PyObject*>(self);
  }
 @@ -162,7 +171,7 @@
      0,                        /* tp_iter */
      0,                        /* tp_iternext */
      SHA256_methods,      /* tp_methods */
 -    0,                         /* tp_members */
 +    SHA256_members,                         /* tp_members */
      0,                         /* tp_getset */
      0,                         /* tp_base */
      0,                         /* tp_dict */
 diff -rN -u old-darcsworld-hmac/pycryptopp/test/test_sha256.py new-
 darcsworld-hmac/pycryptopp/test/test_sha256.py
 --- old-darcsworld-hmac/pycryptopp/test/test_sha256.py  2012-01-09
 11:32:19.574933575 -0700
 +++ new-darcsworld-hmac/pycryptopp/test/test_sha256.py  2012-01-09
 11:32:19.613933575 -0700
 @@ -35,6 +35,11 @@
  h_5fd4 =
 a2b_hex("7c4fbf484498d21b487b9d61de8914b2eadaf2698712936d47c3ada2558f6788")

  class SHA256(unittest.TestCase):
 +    def test_digest_size(self):
 +        self.failUnless(hasattr(sha256.SHA256, 'digest_size'))
 +        self.failUnless(isinstance(sha256.SHA256.digest_size, int))
 +        self.failUnlessEqual(sha256.SHA256.digest_size, 32)
 +
      def test_digest(self):
          empty_digest = sha256.SHA256().digest()
          self.failUnless(isinstance(empty_digest, str))
 }}}

-- 
Ticket URL: <http://tahoe-lafs.org/trac/pycryptopp/ticket/42#comment:3>
pycryptopp <https://tahoe-lafs.org/trac/pycryptopp>



More information about the tahoe-lafs-trac-stream mailing list