<s> from hashlib import sha256 <EOL> from prob18 import raw_xor <EOL> from prob33 import group5_p , intToBytes , mypow <EOL> from random import randrange <EOL> def myhmac ( hash_function , message , key ) : <EOL> blocksize = hash_function ( ) . block_size ; <EOL> if ( len ( key ) > blocksize ) : <EOL> key = hash_function ( key ) . digest ( ) <EOL> if ( len ( key ) < blocksize ) : <EOL> key += ( b'<STR_LIT:\x00>' * ( blocksize - len ( key ) ) ) ; <EOL> opad = raw_xor ( b'<STR_LIT>' * blocksize , key ) ; <EOL> ipad = raw_xor ( b'<STR_LIT>' * blocksize , key ) ; <EOL> return hash_function ( opad + hash_function ( ipad + message ) . digest ( ) ) . digest ( ) ; <EOL> SRP_p = group5_p <EOL> SRP_g = <NUM_LIT:2> ; <EOL> SRP_k = <NUM_LIT:3> ; <EOL> I = b'<STR_LIT>' <EOL> P = b'<STR_LIT>' <EOL> def SRP_init ( ) : <EOL> state = { "<STR_LIT:p>" : SRP_p , <EOL> "<STR_LIT:g>" : SRP_g , <EOL> "<STR_LIT:k>" : SRP_k , <EOL> "<STR_LIT:I>" : I , <EOL> "<STR_LIT:P>" : P } <EOL> return state ; <EOL> def SRP_step1 ( state ) : <EOL> salt = randrange ( <NUM_LIT:2> , state [ "<STR_LIT:p>" ] - <NUM_LIT:2> ) ; <EOL> xH = sha256 ( intToBytes ( salt ) + state [ "<STR_LIT:P>" ] ) . hexdigest ( ) ; <EOL> x = int ( xH , <NUM_LIT:16> ) ; <EOL> v = mypow ( state [ "<STR_LIT:g>" ] , x , state [ "<STR_LIT:p>" ] ) ; <EOL> state [ "<STR_LIT:v>" ] = v ; <EOL> state [ "<STR_LIT>" ] = salt ; <EOL> return state ; <EOL> def SRP_step2 ( state ) : <EOL> state [ "<STR_LIT:a>" ] = randrange ( <NUM_LIT:2> , state [ "<STR_LIT:p>" ] - <NUM_LIT:2> ) ; <EOL> state [ "<STR_LIT:A>" ] = mypow ( state [ "<STR_LIT:g>" ] , state [ "<STR_LIT:a>" ] , state [ "<STR_LIT:p>" ] ) ; <EOL> return state ; <EOL> def SRP_step3 ( state ) : <EOL> state [ "<STR_LIT:b>" ] = randrange ( <NUM_LIT:2> , state [ "<STR_LIT:p>" ] - <NUM_LIT:2> ) ; <EOL> state [ "<STR_LIT:B>" ] = ( state [ "<STR_LIT:k>" ] * state [ "<STR_LIT:v>" ] + mypow ( state [ "<STR_LIT:g>" ] , state [ "<STR_LIT:b>" ] , state [ "<STR_LIT:p>" ] ) ) ; <EOL> return state ; <EOL> def SRP_step4 ( state ) : <EOL> uH = sha256 ( intToBytes ( state [ "<STR_LIT:A>" ] ) + intToBytes ( state [ "<STR_LIT:B>" ] ) ) . hexdigest ( ) ; <EOL> state [ "<STR_LIT:u>" ] = int ( uH , <NUM_LIT:16> ) ; <EOL> return state ; <EOL> def SRP_step5 ( state ) : <EOL> xH = sha256 ( intToBytes ( state [ "<STR_LIT>" ] ) + state [ "<STR_LIT:P>" ] ) . hexdigest ( ) ; <EOL> x = int ( xH , <NUM_LIT:16> ) ; <EOL> S = mypow ( ( state [ "<STR_LIT:B>" ] - state [ "<STR_LIT:k>" ] * mypow ( state [ "<STR_LIT:g>" ] , x , state [ "<STR_LIT:p>" ] ) ) , ( state [ "<STR_LIT:a>" ] + state [ "<STR_LIT:u>" ] * x ) , state [ "<STR_LIT:p>" ] ) ; <EOL> state [ "<STR_LIT>" ] = sha256 ( intToBytes ( S ) ) . digest ( ) ; <EOL> return state ; <EOL> def SRP_step6 ( state ) : <EOL> S = mypow ( state [ "<STR_LIT:A>" ] * mypow ( state [ "<STR_LIT:v>" ] , state [ "<STR_LIT:u>" ] , state [ "<STR_LIT:p>" ] ) , state [ "<STR_LIT:b>" ] , state [ "<STR_LIT:p>" ] ) ; <EOL> state [ "<STR_LIT>" ] = sha256 ( intToBytes ( S ) ) . digest ( ) ; <EOL> return state ; <EOL> def SRP_step7 ( state ) : <EOL> state [ "<STR_LIT>" ] = myhmac ( sha256 , state [ "<STR_LIT>" ] , intToBytes ( state [ "<STR_LIT>" ] ) ) ; <EOL> return state ; <EOL> def SRP_validate ( state ) : <EOL> expected = myhmac ( sha256 , state [ "<STR_LIT>" ] , intToBytes ( state [ "<STR_LIT>" ] ) ) ; <EOL> return expected == state [ "<STR_LIT>" ] ; <EOL> def test_srp ( ) : <EOL> state = SRP_init ( ) ; <EOL> state = SRP_step1 ( state ) ; <EOL> state = SRP_step2 ( state ) ; <EOL> state = SRP_step3 ( state ) ; <EOL> state = SRP_step4 ( state ) ; <EOL> state = SRP_step5 ( state ) ; <EOL> state = SRP_step6 ( state ) ; <EOL> state = SRP_step7 ( state ) ; <EOL> return SRP_validate ( state ) ; <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> if ( test_srp ( ) ) : <EOL> print ( "<STR_LIT>" ) ; <EOL> else : <EOL> print ( "<STR_LIT>" ) ; <EOL> '''<STR_LIT>''' </s>
