projects
/
mudpy.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Fix reload to use a copy of datafile keys
[mudpy.git]
/
lib
/
mudpy
/
password.py
diff --git
a/lib/mudpy/password.py
b/lib/mudpy/password.py
index
68e1a5f
..
f6c1abc
100644
(file)
--- a/
lib/mudpy/password.py
+++ b/
lib/mudpy/password.py
@@
-1,10
+1,17
@@
# -*- coding: utf-8 -*-
"""Password hashing functions and constants for the mudpy engine."""
# -*- coding: utf-8 -*-
"""Password hashing functions and constants for the mudpy engine."""
-# Copyright (c) 2004-201
3
Jeremy Stanley <fungi@yuggoth.org>. Permission
+# Copyright (c) 2004-201
4
Jeremy Stanley <fungi@yuggoth.org>. Permission
# to use, copy, modify, and distribute this software is granted under
# terms provided in the LICENSE file distributed with this software.
# to use, copy, modify, and distribute this software is granted under
# terms provided in the LICENSE file distributed with this software.
+import base64
+import hashlib
+import math
+import random
+import re
+import struct
+
# convenience constants for indexing the supported hashing algorithms,
# guaranteed a stable part of the interface
MD5 = 0 # hashlib.md5
# convenience constants for indexing the supported hashing algorithms,
# guaranteed a stable part of the interface
MD5 = 0 # hashlib.md5
@@
-20,7
+27,6
@@
def _pack_bytes(numbers):
This is a wrapper around struct.pack, used to turn a list of integers
between 0 and 255 into a packed sequence akin to a C-style string.
"""
This is a wrapper around struct.pack, used to turn a list of integers
between 0 and 255 into a packed sequence akin to a C-style string.
"""
- import struct
packed = b""
for number in numbers:
number = int(number)
packed = b""
for number in numbers:
number = int(number)
@@
-34,7
+40,6
@@
def _bytes_to_text(byte_sequence):
This is a wrapper around base64.b64encode with preferences
appropriate for encoding Unix-style passwd hash strings.
"""
This is a wrapper around base64.b64encode with preferences
appropriate for encoding Unix-style passwd hash strings.
"""
- import base64
return base64.b64encode(
byte_sequence,
b"./"
return base64.b64encode(
byte_sequence,
b"./"
@@
-50,8
+55,6
@@
def _generate_salt(salt_len=2):
need and discard any excess characters over the specified length.
This ensures full distribution over each character of the salt.
"""
need and discard any excess characters over the specified length.
This ensures full distribution over each character of the salt.
"""
- import math
- import random
salt = []
for i in range(int(math.ceil(salt_len * 0.75))):
salt.append(random.randint(0, 255))
salt = []
for i in range(int(math.ceil(salt_len * 0.75))):
salt.append(random.randint(0, 255))
@@
-66,7
+69,6
@@
def upgrade_legacy_hash(legacy_hash, salt, sep="$"):
facets to this function, a conforming new-style password hash will be
returned.
"""
facets to this function, a conforming new-style password hash will be
returned.
"""
- import re
assert re.match("^[0-9a-f]{32}$",
legacy_hash), "Not a valid MD5 hexdigest"
collapsed = b""
assert re.match("^[0-9a-f]{32}$",
legacy_hash), "Not a valid MD5 hexdigest"
collapsed = b""
@@
-126,7
+128,6
@@
def create(
create(password, algorithm=SHA256, rounds=12, salt_len=16)
"""
create(password, algorithm=SHA256, rounds=12, salt_len=16)
"""
- import hashlib
# if a specific salt wasn't specified, we need to generate one
if not salt:
# if a specific salt wasn't specified, we need to generate one
if not salt:
@@
-162,11
+163,7
@@
def create(
# number of times
for i in range(2 ** rounds):
hashed = algorithms[algorithm](hashed.encode("utf-8")).digest()
# number of times
for i in range(2 ** rounds):
hashed = algorithms[algorithm](hashed.encode("utf-8")).digest()
- # TODO: remove this exception trap after the switch to py2k
- try:
- hashed = "".join(format(x, "02x") for x in bytes(hashed))
- except ValueError:
- hashed = "".join(format(ord(x), "02x") for x in bytes(hashed))
+ hashed = "".join(format(x, "02x") for x in bytes(hashed))
# concatenate the output fields, coercing into text form as needed
return "%s%s%s%s%s%s%s%s" % (
# concatenate the output fields, coercing into text form as needed
return "%s%s%s%s%s%s%s%s" % (
@@
-183,7
+180,6
@@
def verify(password, encoded_hash):
comes out the same as the encoded_hash.
"""
sep = encoded_hash[0]
comes out the same as the encoded_hash.
"""
sep = encoded_hash[0]
- import mudpy.misc
algorithm, rounds, salt, hashed = encoded_hash.split(sep)[1:]
if encoded_hash == create(
password=password,
algorithm, rounds, salt, hashed = encoded_hash.split(sep)[1:]
if encoded_hash == create(
password=password,