comparison roundup/password.py @ 5416:56c9bcdea47f

Python 3 preparation: unicode. This patch introduces roundup/anypy/strings.py, which has a comment explaining the string representations generally used and common functions to handle the required conversions. Places in the code that explicitly reference the "unicode" type / built-in function are generally changed to use the new functions (or, in a few places where those new functions don't seem to fit well, other approaches such as references to type(u'') or use of the codecs module). This patch does not generally attempt to address text conversions in any places not currently referencing the "unicode" type (although scripts/import_sf.py is made to use binary I/O in places as fixing the "unicode" reference didn't seem coherent otherwise).
author Joseph Myers <jsm@polyomino.org.uk>
date Wed, 25 Jul 2018 09:05:58 +0000
parents 2d6a92c3e212
children 1f1899658115
comparison
equal deleted inserted replaced
5415:2d6a92c3e212 5416:56c9bcdea47f
22 import re, string, random 22 import re, string, random
23 import os 23 import os
24 from base64 import b64encode, b64decode 24 from base64 import b64encode, b64decode
25 from hashlib import md5, sha1 25 from hashlib import md5, sha1
26 26
27 from roundup.anypy.strings import us2s, s2b
28
27 try: 29 try:
28 import crypt 30 import crypt
29 except ImportError: 31 except ImportError:
30 crypt = None 32 crypt = None
31 33
103 If M2Crypto is present, uses it's implementation as backend. 105 If M2Crypto is present, uses it's implementation as backend.
104 106
105 :returns: 107 :returns:
106 raw bytes of generated key 108 raw bytes of generated key
107 """ 109 """
108 if isinstance(password, unicode): 110 password = s2b(us2s(password))
109 password = password.encode("utf-8") 111 salt = s2b(us2s(salt))
110 if isinstance(salt, unicode):
111 salt = salt.encode("utf-8")
112 if keylen > 40: 112 if keylen > 40:
113 #NOTE: pbkdf2 allows up to (2**31-1)*20 bytes, 113 #NOTE: pbkdf2 allows up to (2**31-1)*20 bytes,
114 # but m2crypto has issues on some platforms above 40, 114 # but m2crypto has issues on some platforms above 40,
115 # and such sizes aren't needed for a password hash anyways... 115 # and such sizes aren't needed for a password hash anyways...
116 raise ValueError("key length too large") 116 raise ValueError("key length too large")
124 124
125 def pbkdf2_unpack(pbkdf2): 125 def pbkdf2_unpack(pbkdf2):
126 """ unpack pbkdf2 encrypted password into parts, 126 """ unpack pbkdf2 encrypted password into parts,
127 assume it has format "{rounds}${salt}${digest} 127 assume it has format "{rounds}${salt}${digest}
128 """ 128 """
129 if isinstance(pbkdf2, unicode): 129 pbkdf2 = us2s(pbkdf2)
130 pbkdf2 = pbkdf2.encode("ascii")
131 try: 130 try:
132 rounds, salt, digest = pbkdf2.split("$") 131 rounds, salt, digest = pbkdf2.split("$")
133 except ValueError: 132 except ValueError:
134 raise PasswordValueError("invalid PBKDF2 hash (wrong number of separators)") 133 raise PasswordValueError("invalid PBKDF2 hash (wrong number of separators)")
135 if rounds.startswith("0"): 134 if rounds.startswith("0"):

Roundup Issue Tracker: http://roundup-tracker.org/