<s> """<STR_LIT>""" <EOL> from utils . compat import * <EOL> from utils . cryptomath import * <EOL> import hmac <EOL> import md5 <EOL> import sha <EOL> goodGroupParameters = [ ( <NUM_LIT:2> , <NUM_LIT> ) , ( <NUM_LIT:2> , <NUM_LIT> ) , ( <NUM_LIT:2> , <NUM_LIT> ) , ( <NUM_LIT:2> , <NUM_LIT> ) , ( <NUM_LIT:5> , <NUM_LIT> ) , ( <NUM_LIT:5> , <NUM_LIT> ) , ( <NUM_LIT:5> , <NUM_LIT> ) ] <EOL> def P_hash ( hashModule , secret , seed , length ) : <EOL> bytes = createByteArrayZeros ( length ) <EOL> secret = bytesToString ( secret ) <EOL> seed = bytesToString ( seed ) <EOL> A = seed <EOL> index = <NUM_LIT:0> <EOL> while <NUM_LIT:1> : <EOL> A = hmac . HMAC ( secret , A , hashModule ) . digest ( ) <EOL> output = hmac . HMAC ( secret , A + seed , hashModule ) . digest ( ) <EOL> for c in output : <EOL> if index >= length : <EOL> return bytes <EOL> bytes [ index ] = ord ( c ) <EOL> index += <NUM_LIT:1> <EOL> return bytes <EOL> def PRF ( secret , label , seed , length ) : <EOL> S1 = secret [ : int ( math . ceil ( len ( secret ) / <NUM_LIT> ) ) ] <EOL> S2 = secret [ int ( math . floor ( len ( secret ) / <NUM_LIT> ) ) : ] <EOL> p_md5 = P_hash ( md5 , S1 , concatArrays ( stringToBytes ( label ) , seed ) , length ) <EOL> p_sha1 = P_hash ( sha , S2 , concatArrays ( stringToBytes ( label ) , seed ) , length ) <EOL> for x in range ( length ) : <EOL> p_md5 [ x ] ^= p_sha1 [ x ] <EOL> return p_md5 <EOL> def PRF_SSL ( secret , seed , length ) : <EOL> secretStr = bytesToString ( secret ) <EOL> seedStr = bytesToString ( seed ) <EOL> bytes = createByteArrayZeros ( length ) <EOL> index = <NUM_LIT:0> <EOL> for x in range ( <NUM_LIT> ) : <EOL> A = chr ( ord ( '<STR_LIT:A>' ) + x ) * ( x + <NUM_LIT:1> ) <EOL> input = secretStr + sha . sha ( A + secretStr + seedStr ) . digest ( ) <EOL> output = md5 . md5 ( input ) . digest ( ) <EOL> for c in output : <EOL> if index >= length : <EOL> return bytes <EOL> bytes [ index ] = ord ( c ) <EOL> index += <NUM_LIT:1> <EOL> return bytes <EOL> def makeX ( salt , username , password ) : <EOL> if len ( username ) >= <NUM_LIT> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> if len ( salt ) >= <NUM_LIT> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return stringToNumber ( sha . sha ( salt + sha . sha ( username + "<STR_LIT::>" + password ) . digest ( ) ) . digest ( ) ) <EOL> def makeVerifier ( username , password , bits ) : <EOL> bitsIndex = { <NUM_LIT> : <NUM_LIT:0> , <NUM_LIT> : <NUM_LIT:1> , <NUM_LIT> : <NUM_LIT:2> , <NUM_LIT> : <NUM_LIT:3> , <NUM_LIT> : <NUM_LIT:4> , <NUM_LIT> : <NUM_LIT:5> , <NUM_LIT> : <NUM_LIT:6> } [ bits ] <EOL> g , N = goodGroupParameters [ bitsIndex ] <EOL> salt = bytesToString ( getRandomBytes ( <NUM_LIT:16> ) ) <EOL> x = makeX ( salt , username , password ) <EOL> verifier = powMod ( g , x , N ) <EOL> return N , g , salt , verifier <EOL> def PAD ( n , x ) : <EOL> nLength = len ( numberToString ( n ) ) <EOL> s = numberToString ( x ) <EOL> if len ( s ) < nLength : <EOL> s = ( "<STR_LIT>" * ( nLength - len ( s ) ) ) + s <EOL> return s <EOL> def makeU ( N , A , B ) : <EOL> return stringToNumber ( sha . sha ( PAD ( N , A ) + PAD ( N , B ) ) . digest ( ) ) <EOL> def makeK ( N , g ) : <EOL> return stringToNumber ( sha . sha ( numberToString ( N ) + PAD ( N , g ) ) . digest ( ) ) <EOL> """<STR_LIT>""" <EOL> class MAC_SSL : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , key , msg = None , digestmod = None ) : <EOL> """<STR_LIT>""" <EOL> if digestmod is None : <EOL> import md5 <EOL> digestmod = md5 <EOL> if key == None : <EOL> return <EOL> self . digestmod = digestmod <EOL> self . outer = digestmod . new ( ) <EOL> self . inner = digestmod . new ( ) <EOL> self . digest_size = digestmod . digest_size <EOL> ipad = "<STR_LIT>" * <NUM_LIT> <EOL> opad = "<STR_LIT>" * <NUM_LIT> <EOL> self . inner . update ( key ) <EOL> self . inner . update ( ipad ) <EOL> self . outer . update ( key ) <EOL> self . outer . update ( opad ) <EOL> if msg is not None : <EOL> self . update ( msg ) <EOL> def update ( self , msg ) : <EOL> """<STR_LIT>""" <EOL> self . inner . update ( msg ) <EOL> def copy ( self ) : <EOL> """<STR_LIT>""" <EOL> other = MAC_SSL ( None ) <EOL> other . digest_size = self . digest_size <EOL> other . digestmod = self . digestmod <EOL> other . inner = self . inner . copy ( ) <EOL> other . outer = self . outer . copy ( ) <EOL> return other <EOL> def digest ( self ) : <EOL> """<STR_LIT>""" <EOL> h = self . outer . copy ( ) <EOL> h . update ( self . inner . digest ( ) ) <EOL> return h . digest ( ) <EOL> def hexdigest ( self ) : <EOL> """<STR_LIT>""" <EOL> return "<STR_LIT>" . join ( [ hex ( ord ( x ) ) [ <NUM_LIT:2> : ] . zfill ( <NUM_LIT:2> ) <EOL> for x in tuple ( self . digest ( ) ) ] ) </s>
