Ticket #17481: pbkdf2.diff

File pbkdf2.diff, 1.0 KB (added by bhuztez, 13 years ago)
  • ./django/utils/crypto.py

    diff -up ./django/utils/crypto.py.orig ./django/utils/crypto.py
    old new 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, d  
    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]
Back to Top