<s> """<STR_LIT>""" <EOL> import threading <EOL> import struct <EOL> import uuid <EOL> import random <EOL> import time <EOL> import os <EOL> import re <EOL> import sys <EOL> import logging <EOL> import socket <EOL> import base64 <EOL> import zlib <EOL> _struct_2_long_long = struct . Struct ( '<STR_LIT>' ) <EOL> python_version = sys . version_info [ <NUM_LIT:0> ] <EOL> if python_version == <NUM_LIT:2> : <EOL> import cPickle as pickle <EOL> else : <EOL> import pickle <EOL> from hashlib import md5 , sha1 , sha224 , sha256 , sha384 , sha512 <EOL> try : <EOL> from Crypto . Cipher import AES <EOL> except ImportError : <EOL> import gluon . contrib . aes as AES <EOL> import hmac <EOL> try : <EOL> try : <EOL> from gluon . contrib . pbkdf2_ctypes import pbkdf2_hex <EOL> except ( ImportError , AttributeError ) : <EOL> from gluon . contrib . pbkdf2 import pbkdf2_hex <EOL> HAVE_PBKDF2 = True <EOL> except ImportError : <EOL> try : <EOL> from . pbkdf2 import pbkdf2_hex <EOL> HAVE_PBKDF2 = True <EOL> except ( ImportError , ValueError ) : <EOL> HAVE_PBKDF2 = False <EOL> logger = logging . getLogger ( "<STR_LIT>" ) <EOL> def AES_new ( key , IV = None ) : <EOL> """<STR_LIT>""" <EOL> if IV is None : <EOL> IV = fast_urandom16 ( ) <EOL> return AES . new ( key , AES . MODE_CBC , IV ) , IV <EOL> def compare ( a , b ) : <EOL> """<STR_LIT>""" <EOL> if len ( a ) != len ( b ) : <EOL> return False <EOL> result = <NUM_LIT:0> <EOL> for x , y in zip ( a , b ) : <EOL> result |= ord ( x ) ^ ord ( y ) <EOL> return result == <NUM_LIT:0> <EOL> def md5_hash ( text ) : <EOL> """<STR_LIT>""" <EOL> return md5 ( text ) . hexdigest ( ) <EOL> def simple_hash ( text , key = '<STR_LIT>' , salt = '<STR_LIT>' , digest_alg = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not digest_alg : <EOL> raise RuntimeError ( "<STR_LIT>" ) <EOL> elif not isinstance ( digest_alg , str ) : <EOL> h = digest_alg ( text + key + salt ) <EOL> elif digest_alg . startswith ( '<STR_LIT>' ) : <EOL> iterations , keylen , alg = digest_alg [ <NUM_LIT:7> : - <NUM_LIT:1> ] . split ( '<STR_LIT:U+002C>' ) <EOL> return pbkdf2_hex ( text , salt , int ( iterations ) , <EOL> int ( keylen ) , get_digest ( alg ) ) <EOL> elif key : <EOL> digest_alg = get_digest ( digest_alg ) <EOL> h = hmac . new ( key + salt , text , digest_alg ) <EOL> else : <EOL> h = get_digest ( digest_alg ) ( ) <EOL> h . update ( text + salt ) <EOL> return h . hexdigest ( ) <EOL> def get_digest ( value ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( value , str ) : <EOL> return value <EOL> value = value . lower ( ) <EOL> if value == "<STR_LIT>" : <EOL> return md5 <EOL> elif value == "<STR_LIT>" : <EOL> return sha1 <EOL> elif value == "<STR_LIT>" : <EOL> return sha224 <EOL> elif value == "<STR_LIT>" : <EOL> return sha256 <EOL> elif value == "<STR_LIT>" : <EOL> return sha384 <EOL> elif value == "<STR_LIT>" : <EOL> return sha512 <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % value ) <EOL> DIGEST_ALG_BY_SIZE = { <EOL> <NUM_LIT> / <NUM_LIT:4> : '<STR_LIT>' , <EOL> <NUM_LIT> / <NUM_LIT:4> : '<STR_LIT>' , <EOL> <NUM_LIT> / <NUM_LIT:4> : '<STR_LIT>' , <EOL> <NUM_LIT> / <NUM_LIT:4> : '<STR_LIT>' , <EOL> <NUM_LIT> / <NUM_LIT:4> : '<STR_LIT>' , <EOL> <NUM_LIT> / <NUM_LIT:4> : '<STR_LIT>' , <EOL> } <EOL> def pad ( s , n = <NUM_LIT:32> , padchar = '<STR_LIT:U+0020>' ) : <EOL> return s + ( <NUM_LIT:32> - len ( s ) % <NUM_LIT:32> ) * padchar <EOL> def secure_dumps ( data , encryption_key , hash_key = None , compression_level = None ) : <EOL> if not hash_key : <EOL> hash_key = sha1 ( encryption_key ) . hexdigest ( ) <EOL> dump = pickle . dumps ( data ) <EOL> if compression_level : <EOL> dump = zlib . compress ( dump , compression_level ) <EOL> key = pad ( encryption_key [ : <NUM_LIT:32> ] ) <EOL> cipher , IV = AES_new ( key ) <EOL> encrypted_data = base64 . urlsafe_b64encode ( IV + cipher . encrypt ( pad ( dump ) ) ) <EOL> signature = hmac . new ( hash_key , encrypted_data ) . hexdigest ( ) <EOL> return signature + '<STR_LIT::>' + encrypted_data <EOL> def secure_loads ( data , encryption_key , hash_key = None , compression_level = None ) : <EOL> if not '<STR_LIT::>' in data : <EOL> return None <EOL> if not hash_key : <EOL> hash_key = sha1 ( encryption_key ) . hexdigest ( ) <EOL> signature , encrypted_data = data . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> actual_signature = hmac . new ( hash_key , encrypted_data ) . hexdigest ( ) <EOL> if not compare ( signature , actual_signature ) : <EOL> return None <EOL> key = pad ( encryption_key [ : <NUM_LIT:32> ] ) <EOL> encrypted_data = base64 . urlsafe_b64decode ( encrypted_data ) <EOL> IV , encrypted_data = encrypted_data [ : <NUM_LIT:16> ] , encrypted_data [ <NUM_LIT:16> : ] <EOL> cipher , _ = AES_new ( key , IV = IV ) <EOL> try : <EOL> data = cipher . decrypt ( encrypted_data ) <EOL> data = data . rstrip ( '<STR_LIT:U+0020>' ) <EOL> if compression_level : <EOL> data = zlib . decompress ( data ) <EOL> return pickle . loads ( data ) <EOL> except Exception , e : <EOL> return None <EOL> def initialize_urandom ( ) : <EOL> """<STR_LIT>""" <EOL> node_id = uuid . getnode ( ) <EOL> microseconds = int ( time . time ( ) * <NUM_LIT> ) <EOL> ctokens = [ ( ( node_id + microseconds ) >> ( ( i % <NUM_LIT:6> ) * <NUM_LIT:8> ) ) % <EOL> <NUM_LIT> for i in range ( <NUM_LIT:16> ) ] <EOL> random . seed ( node_id + microseconds ) <EOL> try : <EOL> os . urandom ( <NUM_LIT:1> ) <EOL> have_urandom = True <EOL> try : <EOL> frandom = open ( '<STR_LIT>' , '<STR_LIT:wb>' ) <EOL> try : <EOL> if python_version == <NUM_LIT:2> : <EOL> frandom . write ( '<STR_LIT>' . join ( chr ( t ) for t in ctokens ) ) <EOL> else : <EOL> frandom . write ( bytes ( [ ] ) . join ( bytes ( [ t ] ) for t in ctokens ) ) <EOL> finally : <EOL> frandom . close ( ) <EOL> except IOError : <EOL> pass <EOL> except NotImplementedError : <EOL> have_urandom = False <EOL> logger . warning ( <EOL> """<STR_LIT>""" ) <EOL> if python_version == <NUM_LIT:2> : <EOL> packed = '<STR_LIT>' . join ( chr ( x ) for x in ctokens ) <EOL> else : <EOL> packed = bytes ( [ ] ) . join ( bytes ( [ x ] ) for x in ctokens ) <EOL> unpacked_ctokens = _struct_2_long_long . unpack ( packed ) <EOL> return unpacked_ctokens , have_urandom <EOL> UNPACKED_CTOKENS , HAVE_URANDOM = initialize_urandom ( ) <EOL> def fast_urandom16 ( urandom = [ ] , locker = threading . RLock ( ) ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return urandom . pop ( ) <EOL> except IndexError : <EOL> try : <EOL> locker . acquire ( ) <EOL> ur = os . urandom ( <NUM_LIT:16> * <NUM_LIT> ) <EOL> urandom += [ ur [ i : i + <NUM_LIT:16> ] for i in xrange ( <NUM_LIT:16> , <NUM_LIT> * <NUM_LIT:16> , <NUM_LIT:16> ) ] <EOL> return ur [ <NUM_LIT:0> : <NUM_LIT:16> ] <EOL> finally : <EOL> locker . release ( ) <EOL> def web2py_uuid ( ctokens = UNPACKED_CTOKENS ) : <EOL> """<STR_LIT>""" <EOL> rand_longs = ( random . getrandbits ( <NUM_LIT:64> ) , random . getrandbits ( <NUM_LIT:64> ) ) <EOL> if HAVE_URANDOM : <EOL> urand_longs = _struct_2_long_long . unpack ( fast_urandom16 ( ) ) <EOL> byte_s = _struct_2_long_long . pack ( rand_longs [ <NUM_LIT:0> ] ^ urand_longs [ <NUM_LIT:0> ] ^ ctokens [ <NUM_LIT:0> ] , <EOL> rand_longs [ <NUM_LIT:1> ] ^ urand_longs [ <NUM_LIT:1> ] ^ ctokens [ <NUM_LIT:1> ] ) <EOL> else : <EOL> byte_s = _struct_2_long_long . pack ( rand_longs [ <NUM_LIT:0> ] ^ ctokens [ <NUM_LIT:0> ] , <EOL> rand_longs [ <NUM_LIT:1> ] ^ ctokens [ <NUM_LIT:1> ] ) <EOL> return str ( uuid . UUID ( bytes = byte_s , version = <NUM_LIT:4> ) ) <EOL> REGEX_IPv4 = re . compile ( '<STR_LIT>' ) <EOL> def is_valid_ip_address ( address ) : <EOL> """<STR_LIT>""" <EOL> if address . lower ( ) in ( '<STR_LIT:127.0.0.1>' , '<STR_LIT:localhost>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return True <EOL> elif address . lower ( ) in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> return False <EOL> elif address . count ( '<STR_LIT:.>' ) == <NUM_LIT:3> : <EOL> if address . startswith ( '<STR_LIT>' ) : <EOL> address = address [ <NUM_LIT:7> : ] <EOL> if hasattr ( socket , '<STR_LIT>' ) : <EOL> try : <EOL> socket . inet_aton ( address ) <EOL> return True <EOL> except socket . error : <EOL> return False <EOL> else : <EOL> match = REGEX_IPv4 . match ( address ) <EOL> if match and all ( <NUM_LIT:0> <= int ( match . group ( i ) ) < <NUM_LIT> for i in ( <NUM_LIT:1> , <NUM_LIT:2> , <NUM_LIT:3> , <NUM_LIT:4> ) ) : <EOL> return True <EOL> return False <EOL> elif hasattr ( socket , '<STR_LIT>' ) : <EOL> try : <EOL> socket . inet_pton ( socket . AF_INET6 , address ) <EOL> return True <EOL> except socket . error : <EOL> return False <EOL> else : <EOL> return True <EOL> def is_loopback_ip_address ( ip = None , addrinfo = None ) : <EOL> """<STR_LIT>""" <EOL> if addrinfo : <EOL> if addrinfo [ <NUM_LIT:0> ] == socket . AF_INET or addrinfo [ <NUM_LIT:0> ] == socket . AF_INET6 : <EOL> ip = addrinfo [ <NUM_LIT:4> ] <EOL> if not isinstance ( ip , basestring ) : <EOL> return False <EOL> if ip . count ( '<STR_LIT:.>' ) == <NUM_LIT:3> : <EOL> return ip . lower ( ) . startswith ( ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> return ip == '<STR_LIT>' or ip == '<STR_LIT>' <EOL> def getipaddrinfo ( host ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return [ addrinfo for addrinfo in socket . getaddrinfo ( host , None ) <EOL> if ( addrinfo [ <NUM_LIT:0> ] == socket . AF_INET or <EOL> addrinfo [ <NUM_LIT:0> ] == socket . AF_INET6 ) <EOL> and isinstance ( addrinfo [ <NUM_LIT:4> ] [ <NUM_LIT:0> ] , basestring ) ] <EOL> except socket . error : <EOL> return [ ] </s>
