utils compiles with python 3

This commit is contained in:
mdipierro
2012-10-23 10:17:23 -05:00
parent 36518ecbeb
commit 49b40a1cf3
2 changed files with 31 additions and 10 deletions
+1 -1
View File
@@ -1 +1 @@
Version 2.2.1 (2012-10-23 10:13:13) stable Version 2.2.1 (2012-10-23 10:17:19) stable
+30 -9
View File
@@ -19,22 +19,37 @@ import random
import time import time
import os import os
import re import re
import sys
import logging import logging
import socket import socket
import cPickle
import base64 import base64
import zlib import zlib
python_version = sys.version_info[0]
if python_version == 2:
import cPickle as pickle
else:
import pickle
try: try:
from Crypto.Cipher import AES from Crypto.Cipher import AES
except ImportError: except ImportError:
from contrib import aes as AES try:
from .aes import AES
except ImportError:
from contrib.aes import AES
try: try:
from contrib.pbkdf2 import pbkdf2_hex from contrib.pbkdf2 import pbkdf2_hex
HAVE_PBKDF2 = True HAVE_PBKDF2 = True
except ImportError: except ImportError:
HAVE_PBKDF2 = False try:
from .pbkdf2 import pbkdf2_hex
HAVE_PBKDF2 = True
except (ImportError, ValueError):
HAVE_PBKDF2 = False
logger = logging.getLogger("web2py") logger = logging.getLogger("web2py")
@@ -115,7 +130,7 @@ def pad(s, n=32, padchar='.'):
def secure_dumps(data, encryption_key, hash_key=None, compression_level=None): def secure_dumps(data, encryption_key, hash_key=None, compression_level=None):
if not hash_key: if not hash_key:
hash_key = hashlib.sha1(encryption_key).hexdigest() hash_key = hashlib.sha1(encryption_key).hexdigest()
dump = cPickle.dumps(data) dump = pickle.dumps(data)
if compression_level: if compression_level:
dump = zlib.compress(dump, compression_level) dump = zlib.compress(dump, compression_level)
key = pad(encryption_key[:32]) key = pad(encryption_key[:32])
@@ -141,8 +156,8 @@ def secure_loads(data, encryption_key, hash_key=None, compression_level=None):
data = data.rstrip(' ') data = data.rstrip(' ')
if compression_level: if compression_level:
data = zlib.decompress(data) data = zlib.decompress(data)
return cPickle.loads(data) return pickle.loads(data)
except (TypeError, cPickle.UnpicklingError): except (TypeError, pickle.UnpicklingError):
return None return None
### compute constant CTOKENS ### compute constant CTOKENS
@@ -173,7 +188,10 @@ def initialize_urandom():
# try to add process-specific entropy # try to add process-specific entropy
frandom = open('/dev/urandom', 'wb') frandom = open('/dev/urandom', 'wb')
try: try:
frandom.write(''.join(chr(t) for t in ctokens)) if python_version == 2:
frandom.write(''.join(chr(t) for t in ctokens)) # python 2
else:
frandom.write(bytes([]).join(bytes([t]) for t in ctokens)) # python 3
finally: finally:
frandom.close() frandom.close()
except IOError: except IOError:
@@ -185,8 +203,11 @@ def initialize_urandom():
"""Cryptographically secure session management is not possible on your system because """Cryptographically secure session management is not possible on your system because
your system does not provide a cryptographically secure entropy source. your system does not provide a cryptographically secure entropy source.
This is not specific to web2py; consider deploying on a different operating system.""") This is not specific to web2py; consider deploying on a different operating system.""")
unpacked_ctokens = struct.unpack('=QQ', string.join( if python_version == 2:
(chr(x) for x in ctokens), '')) packed = ''.join(chr(x) for x in ctokens) # python 2
else:
packed = bytes([]).join(bytes([x]) for x in ctokens) # python 3
unpacked_ctokens = struct.unpack('=QQ', packed)
return unpacked_ctokens, have_urandom return unpacked_ctokens, have_urandom
UNPACKED_CTOKENS, HAVE_URANDOM = initialize_urandom() UNPACKED_CTOKENS, HAVE_URANDOM = initialize_urandom()