1 # -*- coding: utf-8 -*-
2 u"""Password hashing functions and constants for the mudpy engine."""
4 # Copyright (c) 2004-2010 Jeremy Stanley <fungi@yuggoth.org>. Permission
5 # to use, copy, modify, and distribute this software is granted under
6 # terms provided in the LICENSE file distributed with this software.
8 # convenience constants for indexing the supported hashing algorithms,
9 # guaranteed a stable part of the interface
11 SHA1 = 1 # hashlib.sha1
12 SHA224 = 2 # hashlib.sha224
13 SHA256 = 3 # hashlib.sha256
14 SHA384 = 4 # hashlib.sha385
15 SHA512 = 5 # hashlib.sha512
17 def _pack_bytes(numbers):
19 This is a wrapper around struct.pack, used to turn a list of integers
20 between 0 and 255 into a packed sequence akin to a C-style string.
23 # this will need to be declared as b"" during 2to3 migration
25 for number in numbers:
27 assert 0 <= number <= 255
28 # need to use b"B" during 2to3 migration
29 packed += struct.pack("B", number)
32 def _bytes_to_text(byte_sequence):
34 This is a wrapper around base64.b64encode with preferences
35 appropriate for encoding Unix-style passwd hash strings.
38 return base64.b64encode(
40 u"./".encode(u"ascii")
43 def _generate_salt(salt_len=2):
45 This simply generates a sequence of pseudo-random characters (with
46 6-bits of effective entropy per character). Since it relies on base64
47 encoding (which operates on 6-bit chunks of data), we only generate
48 0.75 times as many bytes (rounded up) as the number of characters we
49 need and discard any excess characters over the specified length.
50 This ensures full distribution over each character of the salt.
54 for i in xrange(int(math.ceil(salt_len*0.75))):
55 salt.append( random.randint(0,255) )
56 return _bytes_to_text( _pack_bytes(salt) )[:salt_len]
58 def upgrade_legacy_hash(legacy_hash, salt, sep=u"$"):
60 This utility function is meant to provide a migration path for users
61 of mudpy's legacy account-name-salted MD5 hexdigest password hashes.
62 By passing the old passhash (as legacy_hash) and name (as salt)
63 facets to this function, a conforming new-style password hash will be
67 assert re.match(u"^[0-9a-f]{32}$", legacy_hash), "Not a valid MD5 hexdigest"
68 # this needs to be declared as b"" in 2to3
71 # this needs to become a byte() call in 2to3
72 collapsed += chr( int(legacy_hash[2*i:2*i+2], 16) )
73 return u"%s%s%s%s%s%s%s%s" % (
77 0, # 2**0 provides one round of hashing
81 _bytes_to_text(collapsed)
93 The meat of the module, this function takes a provided password and
94 generates a Unix-like passwd hash suitable for storage in portable,
95 text-based data files. The password is prepended with a salt (which
96 can also be specified explicitly, if the output needs to be
97 repeatable) and then hashed with the requested algorithm iterated as
98 many times as 2 raised to the power of the rounds parameter.
100 The first character of the text returned by this function denotes the
101 separator character used to identify subsequent fields. The fields in
104 1. the decimal index number indicating which algorithm was used,
105 also mapped as convenience constants at the beginning of this
108 2. the number of times (as an exponent of 2) which the algorithm
109 was iterated, represented by a decimal value between 0 and 16
110 inclusive (0 results in one round, 16 results in 65536 rounds,
111 and anything higher than that is a potential resource
112 consumption denial of service on the application anyway)
114 3. the plain-text salt with which the password was prepended
117 4. the resulting password hash itself, base64-encoded using . and
118 / as the two non-alpha-numeric characters required to reach 64
120 The defaults provided should be safe for everyday use, but something
121 more heavy-duty may be in order for admin users, such as::
123 create(password, algorithm=SHA256, rounds=12, salt_len=16)
127 # if a specific salt wasn't specified, we need to generate one
129 salt = _generate_salt(salt_len=salt_len)
131 # make sure the algorithm index number is coerced into integer form,
132 # since it could also be passed as text (in decimal) for convenience
133 algorithm = int(algorithm)
135 # the list of algorithms supported by this function corresponds to
136 # the convenience constants defined at the beginning of the module
140 SHA224: hashlib.sha224,
141 SHA256: hashlib.sha256,
142 SHA384: hashlib.sha384,
143 SHA512: hashlib.sha512,
146 # make sure the rounds exponent is coerced into integer form, since
147 # it could also be passed as text (in decimal) for convenience
150 # to avoid a potential resource consumption denial of service attack,
151 # only consider values in the range of 0-16
152 assert 0 <= rounds <= 16
154 # here is where the salt is prepended to the provided password text
155 hashed = salt+password
157 # iterate the hashing algorithm over its own digest the specified
159 for i in xrange(2**rounds):
160 hashed = algorithms[algorithm](hashed).digest()
162 # concatenate the output fields, coercing into text form as needed
163 return u"%s%s%s%s%s%s%s%s" % (
164 sep, algorithm, sep, rounds, sep, salt, sep, _bytes_to_text(hashed)
167 def verify(password, encoded_hash):
169 This simple function requires a text password and a mudpy-format
170 password hash (as generated by the create function). It returns True
171 if the password, hashed with the parameters from the encoded_hash,
172 comes out the same as the encoded_hash.
174 sep = encoded_hash[0]
175 algorithm, rounds, salt, hashed = encoded_hash[1:].split(sep)
176 if encoded_hash == create(