reverted changs to utils

This commit is contained in:
mdipierro
2012-10-23 10:13:17 -05:00
parent 96ee377279
commit a4cef60c49
3 changed files with 62 additions and 91 deletions
+1 -1
View File
@@ -1 +1 @@
Version 2.2.1 (2012-10-23 10:11:28) stable Version 2.2.1 (2012-10-23 10:13:13) stable
+9 -20
View File
@@ -45,16 +45,8 @@ there clears it up.
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE. # THE SOFTWARE.
#
# Ported to python 3 by Massimo Di Pierro
#
#### ####
try:
import binascii # python 3
str2hex = lambda s: binascii.b2a_hex(s.encode('utf8'))
except ImportError: # python 2
str2hex = lambda s: s.decode('hex')
from array import array from array import array
@@ -444,9 +436,8 @@ gf_mul_by_14 = array('B', [galois_multiply(x, 14) for x in range(256)])
# #
# More information: http://en.wikipedia.org/wiki/Rijndael_S-box # More information: http://en.wikipedia.org/wiki/Rijndael_S-box
aes_sbox = array( aes_sbox = array('B',
'B', '637c777bf26b6fc53001672bfed7ab76'
str2hex('637c777bf26b6fc53001672bfed7ab76'
'ca82c97dfa5947f0add4a2af9ca472c0' 'ca82c97dfa5947f0add4a2af9ca472c0'
'b7fd9326363ff7cc34a5e5f171d83115' 'b7fd9326363ff7cc34a5e5f171d83115'
'04c723c31896059a071280e2eb27b275' '04c723c31896059a071280e2eb27b275'
@@ -461,15 +452,14 @@ aes_sbox = array(
'ba78252e1ca6b4c6e8dd741f4bbd8b8a' 'ba78252e1ca6b4c6e8dd741f4bbd8b8a'
'703eb5664803f60e613557b986c11d9e' '703eb5664803f60e613557b986c11d9e'
'e1f8981169d98e949b1e87e9ce5528df' 'e1f8981169d98e949b1e87e9ce5528df'
'8ca1890dbfe6426841992d0fb054bb16') '8ca1890dbfe6426841992d0fb054bb16'.decode('hex')
) )
# This is the inverse of the above. In other words: # This is the inverse of the above. In other words:
# aes_inv_sbox[aes_sbox[val]] == val # aes_inv_sbox[aes_sbox[val]] == val
aes_inv_sbox = array( aes_inv_sbox = array('B',
'B', '52096ad53036a538bf40a39e81f3d7fb'
str2hex('52096ad53036a538bf40a39e81f3d7fb'
'7ce339829b2fff87348e4344c4dee9cb' '7ce339829b2fff87348e4344c4dee9cb'
'547b9432a6c2233dee4c950b42fac34e' '547b9432a6c2233dee4c950b42fac34e'
'082ea16628d924b2765ba2496d8bd125' '082ea16628d924b2765ba2496d8bd125'
@@ -484,7 +474,7 @@ aes_inv_sbox = array(
'1fdda8338807c731b11210592780ec5f' '1fdda8338807c731b11210592780ec5f'
'60517fa919b54a0d2de57a9f93c99cef' '60517fa919b54a0d2de57a9f93c99cef'
'a0e03b4dae2af5b0c8ebbb3c83539961' 'a0e03b4dae2af5b0c8ebbb3c83539961'
'172b047eba77d626e169146355210c7d') '172b047eba77d626e169146355210c7d'.decode('hex')
) )
# The Rcon table is used in AES's key schedule (key expansion) # The Rcon table is used in AES's key schedule (key expansion)
@@ -492,9 +482,8 @@ aes_inv_sbox = array(
# #
# More information: http://en.wikipedia.org/wiki/Rijndael_key_schedule # More information: http://en.wikipedia.org/wiki/Rijndael_key_schedule
aes_Rcon = array( aes_Rcon = array('B',
'B', '8d01020408102040801b366cd8ab4d9a'
str2hex('8d01020408102040801b366cd8ab4d9a'
'2f5ebc63c697356ad4b37dfaefc59139' '2f5ebc63c697356ad4b37dfaefc59139'
'72e4d3bd61c29f254a943366cc831d3a' '72e4d3bd61c29f254a943366cc831d3a'
'74e8cb8d01020408102040801b366cd8' '74e8cb8d01020408102040801b366cd8'
@@ -509,5 +498,5 @@ aes_Rcon = array(
'254a943366cc831d3a74e8cb8d010204' '254a943366cc831d3a74e8cb8d010204'
'08102040801b366cd8ab4d9a2f5ebc63' '08102040801b366cd8ab4d9a2f5ebc63'
'c697356ad4b37dfaefc5913972e4d3bd' 'c697356ad4b37dfaefc5913972e4d3bd'
'61c29f254a943366cc831d3a74e8cb') '61c29f254a943366cc831d3a74e8cb'.decode('hex')
) )
+8 -26
View File
@@ -21,31 +21,19 @@ import os
import re import re
import logging import logging
import socket import socket
import cPickle
import base64 import base64
import zlib import zlib
try:
import cPickle as pickle # python 2
except ImportError:
import pickle # python 3
try: try:
from Crypto.Cipher import AES from Crypto.Cipher import AES
except ImportError: except ImportError:
try: from contrib import aes as AES
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:
try:
from .pbkdf2 import pbkdf2_hex
HAVE_PBKDF2 = True
except (ImportError, ValueError):
HAVE_PBKDF2 = False HAVE_PBKDF2 = False
logger = logging.getLogger("web2py") logger = logging.getLogger("web2py")
@@ -127,7 +115,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 = pickle.dumps(data) dump = cPickle.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])
@@ -153,8 +141,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 pickle.loads(data) return cPickle.loads(data)
except (TypeError, pickle.UnpicklingError): except (TypeError, cPickle.UnpicklingError):
return None return None
### compute constant CTOKENS ### compute constant CTOKENS
@@ -185,10 +173,7 @@ 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:
try: frandom.write(''.join(chr(t) for t in ctokens))
frandom.write(''.join(chr(t) for t in ctokens)) # python 2
except:
frandom.write(bytes([]).join(bytes([t]) for t in ctokens)) # python 3
finally: finally:
frandom.close() frandom.close()
except IOError: except IOError:
@@ -200,11 +185,8 @@ 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.""")
try: unpacked_ctokens = struct.unpack('=QQ', string.join(
packed = ''.join(chr(x) for x in ctokens) # python 2 (chr(x) for x in ctokens), ''))
except:
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()