Utilities/crypto/py3PBKDF2.py

changeset 2997
7f0ef975da9e
parent 2302
f29e9405c851
child 3034
7ce719013078
child 3057
10516539f238
equal deleted inserted replaced
2996:c6f16f1b9958 2997:7f0ef975da9e
38 for i in range(iterations): 38 for i in range(iterations):
39 hash = hmac.new(salt, hash, digestMod).digest() 39 hash = hmac.new(salt, hash, digestMod).digest()
40 return hash 40 return hash
41 41
42 42
43 def hashPasswordTuple(password, digestMod=hashlib.sha512, iterations=10000, saltSize=32): 43 def hashPasswordTuple(password, digestMod=hashlib.sha512, iterations=10000,
44 saltSize=32):
44 """ 45 """
45 Module function to hash a password according to the PBKDF2 specification. 46 Module function to hash a password according to the PBKDF2 specification.
46 47
47 @param password clear text password (string) 48 @param password clear text password (string)
48 @param digestMod hash function 49 @param digestMod hash function
56 hash = pbkdf2(password, salt, iterations, digestMod) 57 hash = pbkdf2(password, salt, iterations, digestMod)
57 digestname = digestMod.__name__.replace("openssl_", "") 58 digestname = digestMod.__name__.replace("openssl_", "")
58 return digestname, iterations, salt, hash 59 return digestname, iterations, salt, hash
59 60
60 61
61 def hashPassword(password, digestMod=hashlib.sha512, iterations=10000, saltSize=32): 62 def hashPassword(password, digestMod=hashlib.sha512, iterations=10000,
63 saltSize=32):
62 """ 64 """
63 Module function to hash a password according to the PBKDF2 specification. 65 Module function to hash a password according to the PBKDF2 specification.
64 66
65 @param password clear text password (string) 67 @param password clear text password (string)
66 @param digestMod hash function 68 @param digestMod hash function
92 """ 94 """
93 try: 95 try:
94 digestname, iterations, salt, pwHash = hash.split(Delimiter) 96 digestname, iterations, salt, pwHash = hash.split(Delimiter)
95 except ValueError: 97 except ValueError:
96 raise ValueError( 98 raise ValueError(
97 "Expected hash encoded password in format "\ 99 "Expected hash encoded password in format "
98 "'digestmod{0}iterations{0}salt{0}hashed_password".format(Delimiter)) 100 "'digestmod{0}iterations{0}salt{0}hashed_password"
101 .format(Delimiter))
99 102
100 if digestname not in Hashes.keys(): 103 if digestname not in Hashes.keys():
101 raise ValueError( 104 raise ValueError(
102 "Unsupported hash algorithm '{0}' for hash encoded password '{1}'.".format( 105 "Unsupported hash algorithm '{0}' for hash encoded password '{1}'."
103 digestname, hash)) 106 .format(digestname, hash))
104 107
105 iterations = int(iterations) 108 iterations = int(iterations)
106 salt = base64.b64decode(salt.encode("ascii")) 109 salt = base64.b64decode(salt.encode("ascii"))
107 pwHash = base64.b64decode(pwHash.encode("ascii")) 110 pwHash = base64.b64decode(pwHash.encode("ascii"))
108 password = password.encode("utf-8") 111 password = password.encode("utf-8")
115 118
116 @param password clear text password (string) 119 @param password clear text password (string)
117 @param hashParameters hash parameters in the form 120 @param hashParameters hash parameters in the form
118 'digestmod$iterations$salt' (string) 121 'digestmod$iterations$salt' (string)
119 @return hashed password (bytes) 122 @return hashed password (bytes)
120 @exception ValueError the hash parameters string is not of the expected format 123 @exception ValueError the hash parameters string is not of the expected
121 or the digest is not one of the known ones 124 format or the digest is not one of the known ones
122 """ 125 """
123 try: 126 try:
124 digestname, iterations, salt = hashParameters.split(Delimiter) 127 digestname, iterations, salt = hashParameters.split(Delimiter)
125 except ValueError: 128 except ValueError:
126 raise ValueError( 129 raise ValueError(
127 "Expected hash parameters string in format "\ 130 "Expected hash parameters string in format "\
128 "'digestmod{0}iterations{0}salt".format(Delimiter)) 131 "'digestmod{0}iterations{0}salt".format(Delimiter))
129 132
130 if digestname not in Hashes.keys(): 133 if digestname not in Hashes.keys():
131 raise ValueError( 134 raise ValueError(
132 "Unsupported hash algorithm '{0}' for hash parameters '{1}'.".format( 135 "Unsupported hash algorithm '{0}' for hash parameters '{1}'."
133 digestname, hash)) 136 .format(digestname, hash))
134 137
135 iterations = int(iterations) 138 iterations = int(iterations)
136 salt = base64.b64decode(salt.encode("ascii")) 139 salt = base64.b64decode(salt.encode("ascii"))
137 password = password.encode("utf-8") 140 password = password.encode("utf-8")
138 return pbkdf2(password, salt, iterations, Hashes[digestname]) 141 return pbkdf2(password, salt, iterations, Hashes[digestname])

eric ide

mercurial