Merge pull request #2582 from alex/stdlib-pbkdf2hmac
Use the stdlib's PBKDF2 implementation when available.
This commit is contained in:
commit
ecff136f69
|
@ -117,7 +117,24 @@ def _long_to_bin(x, hex_format_string):
|
||||||
return binascii.unhexlify((hex_format_string % x).encode('ascii'))
|
return binascii.unhexlify((hex_format_string % x).encode('ascii'))
|
||||||
|
|
||||||
|
|
||||||
def pbkdf2(password, salt, iterations, dklen=0, digest=None):
|
if hasattr(hashlib, "pbkdf2_hmac"):
|
||||||
|
def pbkdf2(password, salt, iterations, dklen=0, digest=None):
|
||||||
|
"""
|
||||||
|
Implements PBKDF2 with the same API as Django's existing
|
||||||
|
implementation, using the stdlib.
|
||||||
|
|
||||||
|
This is used in Python 3.4 and up.
|
||||||
|
"""
|
||||||
|
if digest is None:
|
||||||
|
digest = hashlib.sha256
|
||||||
|
if not dklen:
|
||||||
|
dklen = None
|
||||||
|
password = force_bytes(password)
|
||||||
|
salt = force_bytes(salt)
|
||||||
|
return hashlib.pbkdf2_hmac(
|
||||||
|
digest().name, password, salt, iterations, dklen)
|
||||||
|
else:
|
||||||
|
def pbkdf2(password, salt, iterations, dklen=0, digest=None):
|
||||||
"""
|
"""
|
||||||
Implements PBKDF2 as defined in RFC 2898, section 5.2
|
Implements PBKDF2 as defined in RFC 2898, section 5.2
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue