Utilities/crypto/__init__.py

changeset 1130
3e9f0330f833
parent 1127
b1802ebe0066
child 1509
c0b5e693b0eb
equal deleted inserted replaced
1128:b8ab8eac9dde 1130:3e9f0330f833
5 5
6 """ 6 """
7 Package implementing cryptography related functionality. 7 Package implementing cryptography related functionality.
8 """ 8 """
9 9
10 ##import os
11 ##import sys
12 ##sys.path.insert(1, os.path.join(os.path.dirname(__file__), "../.."))
13 ##
14 import random 10 import random
15 import base64 11 import base64
16 12
17 from PyQt4.QtCore import QCoreApplication 13 from PyQt4.QtCore import QCoreApplication
18 from PyQt4.QtGui import QLineEdit, QInputDialog 14 from PyQt4.QtGui import QLineEdit, QInputDialog
19 15
20 from E5Gui import E5MessageBox 16 from E5Gui import E5MessageBox
21 17
22 from .py3AES import encryptData, decryptData 18 from .py3AES import encryptData, decryptData
23 from .py3PBKDF2 import verifyPassword, hashPasswordTuple, rehashPassword 19 from .py3PBKDF2 import verifyPassword, hashPasswordTuple, rehashPassword
24 ##from py3AES import encryptData, decryptData
25 ##from py3PBKDF2 import verifyPassword, hashPasswordTuple, rehashPassword
26 20
27 import Preferences 21 import Preferences
28 22
29 ################################################################################ 23 ################################################################################
30 ## password handling functions below 24 ## password handling functions below
60 """ 54 """
61 if not epw.startswith(EncodeMarker): 55 if not epw.startswith(EncodeMarker):
62 return epw # it was not encoded using pwEncode 56 return epw # it was not encoded using pwEncode
63 57
64 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")
65
66
67 ##def passwordHash(pw):
68 ## """
69 ## Module function to calculate the hash for the given password.
70 ##
71 ## This is done by hashing it 65.000 times with SHA1 in order to make brute force
72 ## attacks a bit harder.
73 ##
74 ## @param pw password to be hashed (string)
75 ## @return password hash (string)
76 ## """
77 ## hash = QCryptographicHash.hash(QByteArray(pw.encode("utf-8")),
78 ## QCryptographicHash.Sha1)
79 ## for i in range(65000):
80 ## hash = QCryptographicHash.hash(hash, QCryptographicHash.Sha1)
81 ## return base64.b64encode(bytes(hash)).decode("ascii")
82 ##
83 ##
84 ##def generateCryptoKey(pw, keyLength=32):
85 ## """
86 ## Module function to calculate a crypto key given a password.
87 ##
88 ## This is done by hashing the password 32.000 times MD5 and 32.000 times with MD4.
89 ## These hashes are concatenated and and the first bytes are taken depending on the
90 ## desired key length.
91 ##
92 ## @param pw password to be used (string)
93 ## @param keyLength length of the desired key (16, 24 or 32) (default is
94 ## 32 bytes suitable for AES256 encryption)
95 ## @return crypto key (bytes)
96 ## """
97 ## if keyLength not in [16, 24, 32]:
98 ## raise ValueError(QCoreApplication.translate(
99 ## "Crypto", "Illegal key length ({0}) given.").format(keyLength))
100 ##
101 ## hash1 = QCryptographicHash.hash(QByteArray(pw.encode("utf-8")),
102 ## QCryptographicHash.Md5)
103 ## hash2 = QCryptographicHash.hash(QByteArray(pw.encode("utf-8")),
104 ## QCryptographicHash.Md4)
105 ## for i in range(32000):
106 ## hash1 = QCryptographicHash.hash(hash1, QCryptographicHash.Md5)
107 ## hash2 = QCryptographicHash.hash(hash2, QCryptographicHash.Md4)
108 ## hash = (hash1 + hash2)[:keyLength]
109 ## return bytes(hash)
110 59
111 60
112 def __getMasterPassword(): 61 def __getMasterPassword():
113 """ 62 """
114 Private module function to get the password from the user. 63 Private module function to get the password from the user.
127 if verifyPassword(pw, masterPassword): 76 if verifyPassword(pw, masterPassword):
128 MasterPassword = pwEncode(pw) 77 MasterPassword = pwEncode(pw)
129 else: 78 else:
130 E5MessageBox.warning(None, 79 E5MessageBox.warning(None,
131 QCoreApplication.translate("Crypto", "Master Password"), 80 QCoreApplication.translate("Crypto", "Master Password"),
132 QCoreApplication.translate("Crypto", 81 QCoreApplication.translate("Crypto",
133 """The given password is incorrect.""")) 82 """The given password is incorrect."""))
134 else: 83 else:
135 E5MessageBox.critical(None, 84 E5MessageBox.critical(None,
136 QCoreApplication.translate("Crypto", "Master Password"), 85 QCoreApplication.translate("Crypto", "Master Password"),
137 QCoreApplication.translate("Crypto", 86 QCoreApplication.translate("Crypto",
138 """There is no master password registered.""")) 87 """There is no master password registered."""))
139 except ValueError as why: 88 except ValueError as why:
140 E5MessageBox.warning(None, 89 E5MessageBox.warning(None,
141 QCoreApplication.translate("Crypto", "Master Password"), 90 QCoreApplication.translate("Crypto", "Master Password"),
142 QCoreApplication.translate("Crypto", 91 QCoreApplication.translate("Crypto",

eric ide

mercurial