26 |
26 |
27 def pbkdf2(password, salt, iterations, digestMod): |
27 def pbkdf2(password, salt, iterations, digestMod): |
28 """ |
28 """ |
29 Module function to hash a password according to the PBKDF2 specification. |
29 Module function to hash a password according to the PBKDF2 specification. |
30 |
30 |
31 @param password clear text password (bytes) |
31 @param password clear text password |
32 @param salt salt value (bytes) |
32 @type bytes |
33 @param iterations number of times hash function should be applied (integer) |
33 @param salt salt value |
|
34 @type bytes |
|
35 @param iterations number of times hash function should be applied |
|
36 @type int |
34 @param digestMod hash function |
37 @param digestMod hash function |
35 @return hashed password (bytes) |
38 @type function |
|
39 @return hashed password |
|
40 @rtype bytes |
36 """ |
41 """ |
37 pwHash = password |
42 pwHash = password |
38 for _ in range(iterations): |
43 for _ in range(iterations): |
39 pwHash = hmac.new(salt, pwHash, digestMod).digest() |
44 pwHash = hmac.new(salt, pwHash, digestMod).digest() |
40 return pwHash |
45 return pwHash |
44 password, digestMod=hashlib.sha512, iterations=10000, saltSize=32 |
49 password, digestMod=hashlib.sha512, iterations=10000, saltSize=32 |
45 ): |
50 ): |
46 """ |
51 """ |
47 Module function to hash a password according to the PBKDF2 specification. |
52 Module function to hash a password according to the PBKDF2 specification. |
48 |
53 |
49 @param password clear text password (string) |
54 @param password clear text password |
|
55 @type str |
50 @param digestMod hash function |
56 @param digestMod hash function |
51 @param iterations number of times hash function should be applied (integer) |
57 @type function |
52 @param saltSize size of the salt (integer) |
58 @param iterations number of times hash function should be applied |
53 @return tuple of digestname (string), number of iterations (integer), |
59 @type int |
54 salt (bytes) and hashed password (bytes) |
60 @param saltSize size of the salt |
|
61 @type int |
|
62 @return tuple of digestname, number of iterations, salt and hashed password |
|
63 @rtype tuple of (str, int, bytes, bytes) |
55 """ |
64 """ |
56 salt = os.urandom(saltSize) |
65 salt = os.urandom(saltSize) |
57 password = password.encode("utf-8") |
66 password = password.encode("utf-8") |
58 pwHash = pbkdf2(password, salt, iterations, digestMod) |
67 pwHash = pbkdf2(password, salt, iterations, digestMod) |
59 digestname = digestMod.__name__.replace("openssl_", "") |
68 digestname = digestMod.__name__.replace("openssl_", "") |
62 |
71 |
63 def hashPassword(password, digestMod=hashlib.sha512, iterations=10000, saltSize=32): |
72 def hashPassword(password, digestMod=hashlib.sha512, iterations=10000, saltSize=32): |
64 """ |
73 """ |
65 Module function to hash a password according to the PBKDF2 specification. |
74 Module function to hash a password according to the PBKDF2 specification. |
66 |
75 |
67 @param password clear text password (string) |
76 @param password clear text password |
|
77 @type str |
68 @param digestMod hash function |
78 @param digestMod hash function |
69 @param iterations number of times hash function should be applied (integer) |
79 @type function |
70 @param saltSize size of the salt (integer) |
80 @param iterations number of times hash function should be applied |
71 @return hashed password entry according to PBKDF2 specification (string) |
81 @type int |
|
82 @param saltSize size of the salt |
|
83 @type int |
|
84 @return hashed password entry according to PBKDF2 specification |
|
85 @rtype str |
72 """ |
86 """ |
73 digestname, iterations, salt, pwHash = hashPasswordTuple( |
87 digestname, iterations, salt, pwHash = hashPasswordTuple( |
74 password, digestMod, iterations, saltSize |
88 password, digestMod, iterations, saltSize |
75 ) |
89 ) |
76 return Delimiter.join( |
90 return Delimiter.join( |
85 |
99 |
86 def verifyPassword(password, pwHash): |
100 def verifyPassword(password, pwHash): |
87 """ |
101 """ |
88 Module function to verify a password against a hash encoded password. |
102 Module function to verify a password against a hash encoded password. |
89 |
103 |
90 @param password clear text password (string) |
104 @param password clear text password |
|
105 @type str |
91 @param pwHash hash encoded password in the form |
106 @param pwHash hash encoded password in the form |
92 'digestmod$iterations$salt$hashed_password' as produced by the |
107 'digestmod$iterations$salt$hashed_password' as produced by the |
93 hashPassword function (string) |
108 hashPassword function |
94 @return flag indicating a successfull verification (boolean) |
109 @type str |
|
110 @return flag indicating a successfull verification |
|
111 @rtype bool |
95 @exception ValueError the hash is not of the expected format or the |
112 @exception ValueError the hash is not of the expected format or the |
96 digest is not one of the known ones |
113 digest is not one of the known ones |
97 """ |
114 """ |
98 try: |
115 try: |
99 digestname, iterations, salt, pwHash = pwHash.split(Delimiter) |
116 digestname, iterations, salt, pwHash = pwHash.split(Delimiter) |
119 |
136 |
120 def rehashPassword(password, hashParameters): |
137 def rehashPassword(password, hashParameters): |
121 """ |
138 """ |
122 Module function to recreate a password hash given the hash parameters. |
139 Module function to recreate a password hash given the hash parameters. |
123 |
140 |
124 @param password clear text password (string) |
141 @param password clear text password |
|
142 @type str |
125 @param hashParameters hash parameters in the form |
143 @param hashParameters hash parameters in the form |
126 'digestmod$iterations$salt' (string) |
144 'digestmod$iterations$salt' |
127 @return hashed password (bytes) |
145 @type str |
|
146 @return hashed password |
|
147 @rtype bytes |
128 @exception ValueError the hash parameters string is not of the expected |
148 @exception ValueError the hash parameters string is not of the expected |
129 format or the digest is not one of the known ones |
149 format or the digest is not one of the known ones |
130 """ |
150 """ |
131 try: |
151 try: |
132 digestname, iterations, salt = hashParameters.split(Delimiter) |
152 digestname, iterations, salt = hashParameters.split(Delimiter) |