<s> """<STR_LIT>""" <EOL> import hmac <EOL> import struct <EOL> import hashlib <EOL> import binascii <EOL> import operator <EOL> import time <EOL> from functools import reduce <EOL> import random <EOL> try : <EOL> random = random . SystemRandom ( ) <EOL> using_sysrandom = True <EOL> except NotImplementedError : <EOL> import warnings <EOL> warnings . warn ( '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> using_sysrandom = False <EOL> from webtools . utils . encoding import smart_bytes <EOL> _trans_5c = bytearray ( [ ( x ^ <NUM_LIT> ) for x in range ( <NUM_LIT> ) ] ) <EOL> _trans_36 = bytearray ( [ ( x ^ <NUM_LIT> ) for x in range ( <NUM_LIT> ) ] ) <EOL> def salted_hmac ( key_salt , value , secret = None ) : <EOL> """<STR_LIT>""" <EOL> if secret is None : <EOL> secret = settings . SECRET_KEY <EOL> key = hashlib . sha1 ( ( key_salt + secret ) . encode ( '<STR_LIT:utf-8>' ) ) . digest ( ) <EOL> return hmac . new ( key , msg = smart_bytes ( value ) , digestmod = hashlib . sha1 ) <EOL> def get_random_string ( length = <NUM_LIT:12> , <EOL> allowed_chars = '<STR_LIT>' <EOL> '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> if not using_sysrandom : <EOL> random . seed ( <EOL> hashlib . sha256 ( <EOL> "<STR_LIT>" % ( <EOL> random . getstate ( ) , <EOL> time . time ( ) , <EOL> settings . SECRET_KEY ) <EOL> ) . digest ( ) ) <EOL> return '<STR_LIT>' . join ( [ random . choice ( allowed_chars ) for i in range ( length ) ] ) <EOL> def constant_time_compare ( val1 , val2 ) : <EOL> """<STR_LIT>""" <EOL> if len ( val1 ) != len ( val2 ) : <EOL> return False <EOL> result = <NUM_LIT:0> <EOL> for x , y in zip ( val1 , val2 ) : <EOL> result |= ord ( x ) ^ ord ( y ) <EOL> return result == <NUM_LIT:0> <EOL> def _bin_to_long ( x ) : <EOL> """<STR_LIT>""" <EOL> return int ( binascii . hexlify ( x ) , <NUM_LIT:16> ) <EOL> def _long_to_bin ( x , hex_format_string ) : <EOL> """<STR_LIT>""" <EOL> return binascii . unhexlify ( ( hex_format_string % x ) . encode ( '<STR_LIT:ascii>' ) ) <EOL> def _fast_hmac ( key , msg , digest ) : <EOL> """<STR_LIT>""" <EOL> dig1 , dig2 = digest ( ) , digest ( ) <EOL> if len ( key ) > dig1 . block_size : <EOL> key = digest ( key ) . digest ( ) <EOL> key += b'<STR_LIT:\x00>' * ( dig1 . block_size - len ( key ) ) <EOL> dig1 . update ( key . translate ( _trans_36 ) ) <EOL> dig1 . update ( msg ) <EOL> dig2 . update ( key . translate ( _trans_5c ) ) <EOL> dig2 . update ( dig1 . digest ( ) ) <EOL> return dig2 <EOL> def pbkdf2 ( password , salt , iterations , dklen = <NUM_LIT:0> , digest = None ) : <EOL> """<STR_LIT>""" <EOL> assert iterations > <NUM_LIT:0> <EOL> if not digest : <EOL> digest = hashlib . sha256 <EOL> password = smart_bytes ( password ) <EOL> salt = smart_bytes ( salt ) <EOL> hlen = digest ( ) . digest_size <EOL> if not dklen : <EOL> dklen = hlen <EOL> if dklen > ( <NUM_LIT:2> ** <NUM_LIT:32> - <NUM_LIT:1> ) * hlen : <EOL> raise OverflowError ( '<STR_LIT>' ) <EOL> l = - ( - dklen // hlen ) <EOL> r = dklen - ( l - <NUM_LIT:1> ) * hlen <EOL> hex_format_string = "<STR_LIT>" % ( hlen * <NUM_LIT:2> ) <EOL> def F ( i ) : <EOL> def U ( ) : <EOL> u = salt + struct . pack ( b'<STR_LIT>' , i ) <EOL> for j in range ( int ( iterations ) ) : <EOL> u = _fast_hmac ( password , u , digest ) . digest ( ) <EOL> yield _bin_to_long ( u ) <EOL> return _long_to_bin ( reduce ( operator . xor , U ( ) ) , hex_format_string ) <EOL> T = [ F ( x ) for x in range ( <NUM_LIT:1> , l + <NUM_LIT:1> ) ] <EOL> return b'<STR_LIT>' . join ( T [ : - <NUM_LIT:1> ] ) + T [ - <NUM_LIT:1> ] [ : r ] </s>
