Ticket #17481: 17481-2.diff

File 17481-2.diff, 1.8 KB (added by Claude Paroz, 6 years ago)

Same patch with test

  • django/utils/crypto.py

    diff --git a/django/utils/crypto.py b/django/utils/crypto.py
    index ff6096c..208788c 100644
    a b def bin_to_long(x): 
    7575    return long(x.encode('hex'), 16)
    7676
    7777
    78 def long_to_bin(x):
     78def long_to_bin(x, length=0):
    7979    """
    8080    Convert a long integer into a binary string
    8181    """
    8282    hex = "%x" % (x)
    8383    if len(hex) % 2 == 1:
    8484        hex = '0' + hex
    85     return binascii.unhexlify(hex)
     85    bin = binascii.unhexlify(hex)
     86    return chr(0)*(length-len(bin)) + bin
    8687
    8788
    8889def fast_hmac(key, msg, digest):
    def pbkdf2(password, salt, iterations, dklen=0, digest=None): 
    129130            for j in xrange(int(iterations)):
    130131                u = fast_hmac(password, u, digest).digest()
    131132                yield bin_to_long(u)
    132         return long_to_bin(reduce(operator.xor, U()))
     133        return long_to_bin(reduce(operator.xor, U()), hlen)
    133134
    134135    T = [F(x) for x in range(1, l + 1)]
    135136    return ''.join(T[:-1]) + T[-1][:r]
  • tests/regressiontests/utils/crypto.py

    diff --git a/tests/regressiontests/utils/crypto.py b/tests/regressiontests/utils/crypto.py
    index e791e0a..913674f 100644
    a b class TestUtilsCryptoPBKDF2(unittest.TestCase): 
    108108                       "c4007d5298f9033c0241d5ab69305e7b64eceeb8d"
    109109                       "834cfec"),
    110110        },
     111        # Check leading zeros are not stripped (#17481)
     112        {
     113            "args": {
     114                "password": chr(186),
     115                "salt": "salt",
     116                "iterations": 1,
     117                "dklen": 20,
     118                "digest": hashlib.sha1,
     119            },
     120            "result": (
     121                '0053d3b91a7f1e54effebd6d68771e8a6e0b2c5b'
     122            ),
     123        }
    111124    ]
    112125
    113126    def test_public_vectors(self):
Back to Top