简体   繁体   中英

Converting hmac from Python3 to Python2

I have the following implementation of HMAC:

"""HMAC (Keyed-Hashing for Message Authentication) Python module.
Implements the HMAC algorithm as described by RFC 2104.
"""

impot hashlib
import warnings as _warnings

trans_5C = bytes((x ^ 0x5C) for x in range(256))
trans_36 = bytes((x ^ 0x36) for x in range(256))

# The size of the digests returned by HMAC depends on the underlying
# hashing module used.  Use digest_size from the instance of HMAC instead.
digest_size = None


class HMAC:
    """RFC 2104 HMAC class.  Also complies with RFC 4231.
    This supports the API for Cryptographic Hash Functions (PEP 247).
    """
    blocksize = 64  # 512-bit HMAC; can be changed in subclasses.

    def __init__(self, key, msg = None, digestmod = None):
        """Create a new HMAC object.
        key:       key for the keyed hash object.
        msg:       Initial input for the hash, if provided.
        digestmod: A module supporting PEP 247.  *OR*
                   A hashlib constructor returning a new hash object.
                   Defaults to hashlib.md5.
        Note: key and msg must be bytes objects.
        """

        if not isinstance(key, bytes):
            raise TypeError("expected bytes, but got %r" % type(key).__name__)

        if digestmod is None:
            import hashlib
            digestmod = hashlib.md5

        if hasattr(digestmod, '__call__'):
            self.digest_cons = digestmod
        else:
            self.digest_cons = lambda d=b'': digestmod.new(d)

        self.outer = self.digest_cons()
        self.inner = self.digest_cons()
        self.digest_size = self.inner.digest_size

        if hasattr(self.inner, 'block_size'):
            blocksize = self.inner.block_size
            if blocksize < 16:
                _warnings.warn('block_size of %d seems too small; using our '
                               'default of %d.' % (blocksize, self.blocksize),
                               RuntimeWarning, 2)
                blocksize = self.blocksize
        else:
            _warnings.warn('No block_size attribute on given digest object; '
                           'Assuming %d.' % (self.blocksize),
                           RuntimeWarning, 2)
            blocksize = self.blocksize

        if len(key) > blocksize:
            key = self.digest_cons(key).digest()

        key = key + bytes(blocksize - len(key))
        self.outer.update(key.translate(trans_5C))
        self.inner.update(key.translate(trans_36))
        if msg is not None:
            self.update(msg)

    def update(self, msg):
        """Update this hashing object with the string msg.
        """
        if not isinstance(msg, bytes):
            raise TypeError("expected bytes, but got %r" % type(msg).__name__)
        self.inner.update(msg)

    def copy(self):
        """Return a separate copy of this hashing object.
        An update to this copy won't affect the original object.
        """
        # Call __new__ directly to avoid the expensive __init__.
        other = self.__class__.__new__(self.__class__)
        other.digest_cons = self.digest_cons
        other.digest_size = self.digest_size
        other.inner = self.inner.copy()
        other.outer = self.outer.copy()
        return other

    def _current(self):
        """Return a hash object for the current state.
        To be used only internally with digest() and hexdigest().
        """
        h = self.outer.copy()
        h.update(self.inner.digest())
        return h

    def digest(self):
        """Return the hash value of this hashing object.
        This returns a string containing 8-bit data.  The object is
        not altered in any way by this function; you can continue
        updating the object after calling this function.
        """
        h = self._current()
        return h.digest()

    def hexdigest(self):
        """Like digest(), but returns a string of hexadecimal digits instead.
        """
        h = self._current()
        return h.hexdigest()

def new(key, msg = None, digestmod = None):
    """Create a new hashing object and return it.
    key: The starting key for the hash.
    msg: if available, will immediately be hashed into the object's starting
    state.
    You can now feed arbitrary strings into the object using its update()
    method, and can ask for the hash value at any time by calling its digest()
    method.
    """
    return HMAC(key, msg, digestmod)

if __name__ == '__main__':
    bin = b'R\x9d\x94r\x12\xa4\x1a\xec\xb4\x11\x90\xdcY\x9a\x96\xccReWimBsDWONrzoeO'
    h = new(bin, digestmod=hashlib.sha256)
    h.update(b"hello")
    d = h.hexdigest()
    print(d)
    assert d == 'a4d000deb3faec0b6d3acf5730c5973727478fa918fb65195c75b0a62f7f12c8'

this code works fine in Python 3, however it fails in Python 2:

~$ python hmac.py 
Traceback (most recent call last):
  File "hmac.py", line 128, in <module>
    h = new(bin, digestmod=hashlib.sha256)
  File "hmac.py", line 123, in new
    return HMAC(key, msg, digestmod)
  File "hmac.py", line 67, in __init__
    self.outer.update(key.translate(trans_5C))
ValueError: translation table must be 256 characters long

How can I make this code work for Python 2? Specifically, how can I convert

trans_5C = bytes((x ^ 0x5C) for x in range(256))
trans_36 = bytes((x ^ 0x36) for x in range(256))

into an equivalent in Python 2?

Using

trans_5C = ''.join(chr(x ^ 0x5C) for x in range(256))
trans_36 = ''.join(chr(x ^ 0x36) for x in range(256))

still causes the hmac to fail:

python hmac.py 
34669003b12998c4f555dcc6110188a96af0caf889858a3ab81929aa89339b0a
Traceback (most recent call last):
  File "hmac.py", line 133, in <module>
    assert d == 'a4d000deb3faec0b6d3acf5730c5973727478fa918fb65195c75b0a62f7f12c8'
AssertionError

however, the implementation works fine in Python 3...

This is a somewhat bizarre one. "bytes" in Python 2 is the same as "str", so that command is actually producing the string "<generator object at 0x12345678>".

The equivalent in Python 2 is

trans_36 = ''.join(chr(x ^ 0x36) for x in range(256))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM