127 def pwDecrypt(epw, masterPW=None): |
127 def pwDecrypt(epw, masterPW=None): |
128 """ |
128 """ |
129 Module function to decrypt a password. |
129 Module function to decrypt a password. |
130 |
130 |
131 @param epw hashed password to decrypt (string) |
131 @param epw hashed password to decrypt (string) |
132 @param masterPW password to be used for encryption (string) |
132 @param masterPW password to be used for decryption (string) |
133 @return decrypted password (string) and flag indicating |
133 @return decrypted password (string) and flag indicating |
134 success (boolean) |
134 success (boolean) |
135 """ |
135 """ |
136 if not epw.startswith(CryptoMarker): |
136 if not epw.startswith(CryptoMarker): |
137 return epw, False # it was not encoded using pwEncrypt |
137 return epw, False # it was not encoded using pwEncrypt |
239 |
239 |
240 if newPassword == "": |
240 if newPassword == "": |
241 MasterPassword = None |
241 MasterPassword = None |
242 else: |
242 else: |
243 MasterPassword = pwEncode(newPassword) |
243 MasterPassword = pwEncode(newPassword) |
|
244 |
|
245 |
|
246 def dataEncrypt(data, password): |
|
247 """ |
|
248 Module function to encrypt a password. |
|
249 |
|
250 @param data data to encrypt (bytes) |
|
251 @param password password to be used for encryption (string) |
|
252 @return encrypted data (bytes) and flag indicating |
|
253 success (boolean) |
|
254 """ |
|
255 digestname, iterations, salt, hash = hashPasswordTuple(password) |
|
256 key = hash[:32] |
|
257 try: |
|
258 cipher = encryptData(key, data) |
|
259 except ValueError: |
|
260 return b"", False |
|
261 return CryptoMarker.encode() + Delimiter.encode().join([ |
|
262 digestname.encode(), |
|
263 str(iterations).encode(), |
|
264 base64.b64encode(salt), |
|
265 base64.b64encode(cipher) |
|
266 ]), True |
|
267 |
|
268 |
|
269 def dataDecrypt(edata, password): |
|
270 """ |
|
271 Module function to decrypt a password. |
|
272 |
|
273 @param edata hashed data to decrypt (string) |
|
274 @param password password to be used for decryption (string) |
|
275 @return decrypted data (bytes) and flag indicating |
|
276 success (boolean) |
|
277 """ |
|
278 if not edata.startswith(CryptoMarker.encode()): |
|
279 return edata, False # it was not encoded using dataEncrypt |
|
280 |
|
281 hashParametersBytes, edata = edata[3:].rsplit(Delimiter.encode(), 1) |
|
282 hashParameters = hashParametersBytes.decode() |
|
283 try: |
|
284 # recreate the key used to encrypt |
|
285 key = rehashPassword(password, hashParameters)[:32] |
|
286 plaintext = decryptData(key, base64.b64decode(edata)) |
|
287 except ValueError: |
|
288 return "", False |
|
289 return plaintext, True |
244 |
290 |
245 if __name__ == "__main__": |
291 if __name__ == "__main__": |
246 import sys |
292 import sys |
247 from PyQt4.QtGui import QApplication |
293 from PyQt4.QtGui import QApplication |
248 |
294 |