31 |
31 |
32 def pwEncode(pw): |
32 def pwEncode(pw): |
33 """ |
33 """ |
34 Module function to encode a password. |
34 Module function to encode a password. |
35 |
35 |
36 @param pw password to encode (string) |
36 @param pw password to encode |
37 @return encoded password (string) |
37 @type str |
|
38 @return encoded password |
|
39 @rtype str |
38 """ |
40 """ |
39 pop = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,;:-_!$?*+#" |
41 pop = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.,;:-_!$?*+#" |
40 rpw = "".join(random.sample(pop, 32)) + pw + "".join(random.sample(pop, 32)) |
42 rpw = "".join(random.sample(pop, 32)) + pw + "".join(random.sample(pop, 32)) |
41 return EncodeMarker + base64.b64encode(rpw.encode("utf-8")).decode("ascii") |
43 return EncodeMarker + base64.b64encode(rpw.encode("utf-8")).decode("ascii") |
42 |
44 |
43 |
45 |
44 def pwDecode(epw): |
46 def pwDecode(epw): |
45 """ |
47 """ |
46 Module function to decode a password. |
48 Module function to decode a password. |
47 |
49 |
48 @param epw encoded password to decode (string) |
50 @param epw encoded password to decode |
49 @return decoded password (string) |
51 @type str |
|
52 @return decoded password |
|
53 @rtype str |
50 """ |
54 """ |
51 if not epw.startswith(EncodeMarker): |
55 if not epw.startswith(EncodeMarker): |
52 return epw # it was not encoded using pwEncode |
56 return epw # it was not encoded using pwEncode |
53 |
57 |
54 return base64.b64decode(epw[3:].encode("ascii"))[32:-32].decode("utf-8") |
58 return base64.b64decode(epw[3:].encode("ascii"))[32:-32].decode("utf-8") |
104 |
108 |
105 def pwEncrypt(pw, mainPW=None): |
109 def pwEncrypt(pw, mainPW=None): |
106 """ |
110 """ |
107 Module function to encrypt a password. |
111 Module function to encrypt a password. |
108 |
112 |
109 @param pw password to encrypt (string) |
113 @param pw password to encrypt |
110 @param mainPW password to be used for encryption (string) |
114 @type str |
111 @return encrypted password (string) and flag indicating |
115 @param mainPW password to be used for encryption |
112 success (boolean) |
116 @type str |
|
117 @return encrypted password (string) and flag indicating success |
|
118 @rtype bool |
113 """ |
119 """ |
114 from .py3AES import encryptData |
120 from .py3AES import encryptData |
115 from .py3PBKDF2 import hashPasswordTuple |
121 from .py3PBKDF2 import hashPasswordTuple |
116 |
122 |
117 if mainPW is None: |
123 if mainPW is None: |
144 |
150 |
145 def pwDecrypt(epw, mainPW=None): |
151 def pwDecrypt(epw, mainPW=None): |
146 """ |
152 """ |
147 Module function to decrypt a password. |
153 Module function to decrypt a password. |
148 |
154 |
149 @param epw hashed password to decrypt (string) |
155 @param epw hashed password to decrypt |
150 @param mainPW password to be used for decryption (string) |
156 @type str |
151 @return decrypted password (string) and flag indicating |
157 @param mainPW password to be used for decryption |
152 success (boolean) |
158 @type str |
|
159 @return decrypted password (string) and flag indicating success |
|
160 @rtype bool |
153 """ |
161 """ |
154 from .py3AES import decryptData |
162 from .py3AES import decryptData |
155 from .py3PBKDF2 import rehashPassword |
163 from .py3PBKDF2 import rehashPassword |
156 |
164 |
157 if not epw.startswith(CryptoMarker): |
165 if not epw.startswith(CryptoMarker): |
177 |
185 |
178 def pwReencrypt(epw, oldPassword, newPassword): |
186 def pwReencrypt(epw, oldPassword, newPassword): |
179 """ |
187 """ |
180 Module function to re-encrypt a password. |
188 Module function to re-encrypt a password. |
181 |
189 |
182 @param epw hashed password to re-encrypt (string) |
190 @param epw hashed password to re-encrypt |
183 @param oldPassword password used to encrypt (string) |
191 @type str |
184 @param newPassword new password to be used (string) |
192 @param oldPassword password used to encrypt |
185 @return encrypted password (string) and flag indicating |
193 @type str |
186 success (boolean) |
194 @param newPassword new password to be used |
|
195 @type str |
|
196 @return encrypted password (string) and flag indicating success |
|
197 @rtype bool |
187 """ |
198 """ |
188 plaintext, ok = pwDecrypt(epw, oldPassword) |
199 plaintext, ok = pwDecrypt(epw, oldPassword) |
189 if ok: |
200 if ok: |
190 return pwEncrypt(plaintext, newPassword) |
201 return pwEncrypt(plaintext, newPassword) |
191 else: |
202 else: |
196 """ |
207 """ |
197 Module function to re-encode a password. |
208 Module function to re-encode a password. |
198 |
209 |
199 In case of an error the encoded password is returned unchanged. |
210 In case of an error the encoded password is returned unchanged. |
200 |
211 |
201 @param epw encoded password to re-encode (string) |
212 @param epw encoded password to re-encode |
202 @param oldPassword password used to encode (string) |
213 @type str |
203 @param newPassword new password to be used (string) |
214 @param oldPassword password used to encode |
204 @return encoded password (string) |
215 @type str |
|
216 @param newPassword new password to be used |
|
217 @type str |
|
218 @return encoded password |
|
219 @rtype str |
205 """ |
220 """ |
206 if epw == "": |
221 if epw == "": |
207 return epw |
222 return epw |
208 |
223 |
209 if newPassword == "": |
224 if newPassword == "": |
225 vice versa. |
240 vice versa. |
226 |
241 |
227 If there is an error, an empty code is returned for the encode function |
242 If there is an error, an empty code is returned for the encode function |
228 or the given encoded password for the decode function. |
243 or the given encoded password for the decode function. |
229 |
244 |
230 @param pw password to encode (string) |
245 @param pw password to encode |
231 @param encode flag indicating an encode or decode function (boolean) |
246 @type str |
232 @return encoded or decoded password (string) |
247 @param encode flag indicating an encode or decode function |
|
248 @type bool |
|
249 @return encoded or decoded password |
|
250 @rtype str |
233 """ |
251 """ |
234 if pw == "": |
252 if pw == "": |
235 return pw |
253 return pw |
236 |
254 |
237 if encode: |
255 if encode: |
252 |
270 |
253 def changeRememberedMain(newPassword): |
271 def changeRememberedMain(newPassword): |
254 """ |
272 """ |
255 Module function to change the remembered main password. |
273 Module function to change the remembered main password. |
256 |
274 |
257 @param newPassword new password to be used (string) |
275 @param newPassword new password to be used |
|
276 @type str |
258 """ |
277 """ |
259 global MainPassword |
278 global MainPassword |
260 MainPassword = pwEncode(newPassword) if newPassword else None |
279 MainPassword = pwEncode(newPassword) if newPassword else None |
261 |
280 |
262 |
281 |
263 def dataEncrypt(data, password, keyLength=32, hashIterations=10000): |
282 def dataEncrypt(data, password, keyLength=32, hashIterations=10000): |
264 """ |
283 """ |
265 Module function to encrypt a password. |
284 Module function to encrypt a password. |
266 |
285 |
267 @param data data to encrypt (bytes) |
286 @param data data to encrypt |
268 @param password password to be used for encryption (string) |
287 @type bytes |
269 @param keyLength length of the key to be generated for encryption |
288 @param password password to be used for encryption |
270 (16, 24 or 32) |
289 @type str |
|
290 @param keyLength length of the key to be generated for encryption (16, 24 or 32) |
|
291 @type int |
271 @param hashIterations number of hashes to be applied to the password for |
292 @param hashIterations number of hashes to be applied to the password for |
272 generating the encryption key (integer) |
293 generating the encryption key |
273 @return encrypted data (bytes) and flag indicating |
294 @type int |
274 success (boolean) |
295 @return encrypted data (bytes) and flag indicating success |
|
296 @rtype bool |
275 """ |
297 """ |
276 from .py3AES import encryptData |
298 from .py3AES import encryptData |
277 from .py3PBKDF2 import hashPasswordTuple |
299 from .py3PBKDF2 import hashPasswordTuple |
278 |
300 |
279 digestname, iterations, salt, pwHash = hashPasswordTuple( |
301 digestname, iterations, salt, pwHash = hashPasswordTuple( |
300 |
322 |
301 def dataDecrypt(edata, password, keyLength=32): |
323 def dataDecrypt(edata, password, keyLength=32): |
302 """ |
324 """ |
303 Module function to decrypt a password. |
325 Module function to decrypt a password. |
304 |
326 |
305 @param edata hashed data to decrypt (string) |
327 @param edata hashed data to decrypt |
306 @param password password to be used for decryption (string) |
328 @type str |
307 @param keyLength length of the key to be generated for decryption |
329 @param password password to be used for decryption |
308 (16, 24 or 32) |
330 @type str |
309 @return decrypted data (bytes) and flag indicating |
331 @param keyLength length of the key to be generated for decryption (16, 24 or 32) |
310 success (boolean) |
332 @type int |
|
333 @return decrypted data (bytes) and flag indicating success |
|
334 @rtype bool |
311 """ |
335 """ |
312 from .py3AES import decryptData |
336 from .py3AES import decryptData |
313 from .py3PBKDF2 import rehashPassword |
337 from .py3PBKDF2 import rehashPassword |
314 |
338 |
315 if not edata.startswith(CryptoMarker.encode("utf-8")): |
339 if not edata.startswith(CryptoMarker.encode("utf-8")): |