<s> '''<STR_LIT>''' <EOL> import hmac <EOL> from binascii import hexlify <EOL> from . util import number_to_string , number_to_string_crop <EOL> from . six import b <EOL> try : <EOL> bin ( <NUM_LIT:0> ) <EOL> except NameError : <EOL> binmap = { "<STR_LIT:0>" : "<STR_LIT>" , "<STR_LIT:1>" : "<STR_LIT>" , "<STR_LIT:2>" : "<STR_LIT>" , "<STR_LIT:3>" : "<STR_LIT>" , <EOL> "<STR_LIT:4>" : "<STR_LIT>" , "<STR_LIT:5>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT>" : "<STR_LIT>" , "<STR_LIT:a>" : "<STR_LIT>" , "<STR_LIT:b>" : "<STR_LIT>" , <EOL> "<STR_LIT:c>" : "<STR_LIT>" , "<STR_LIT:d>" : "<STR_LIT>" , "<STR_LIT:e>" : "<STR_LIT>" , "<STR_LIT:f>" : "<STR_LIT>" } <EOL> def bin ( value ) : <EOL> v = "<STR_LIT>" . join ( binmap [ x ] for x in "<STR_LIT>" % abs ( value ) ) . lstrip ( "<STR_LIT:0>" ) <EOL> if value < <NUM_LIT:0> : <EOL> return "<STR_LIT>" + v <EOL> return "<STR_LIT>" + v <EOL> def bit_length ( num ) : <EOL> s = bin ( num ) <EOL> s = s . lstrip ( '<STR_LIT>' ) <EOL> return len ( s ) <EOL> def bits2int ( data , qlen ) : <EOL> x = int ( hexlify ( data ) , <NUM_LIT:16> ) <EOL> l = len ( data ) * <NUM_LIT:8> <EOL> if l > qlen : <EOL> return x >> ( l - qlen ) <EOL> return x <EOL> def bits2octets ( data , order ) : <EOL> z1 = bits2int ( data , bit_length ( order ) ) <EOL> z2 = z1 - order <EOL> if z2 < <NUM_LIT:0> : <EOL> z2 = z1 <EOL> return number_to_string_crop ( z2 , order ) <EOL> def generate_k ( generator , secexp , hash_func , data ) : <EOL> '''<STR_LIT>''' <EOL> qlen = bit_length ( generator . order ( ) ) <EOL> holen = hash_func ( ) . digest_size <EOL> rolen = ( qlen + <NUM_LIT:7> ) / <NUM_LIT:8> <EOL> bx = number_to_string ( secexp , generator . order ( ) ) + bits2octets ( data , generator . order ( ) ) <EOL> v = b ( '<STR_LIT>' ) * holen <EOL> k = b ( '<STR_LIT:\x00>' ) * holen <EOL> k = hmac . new ( k , v + b ( '<STR_LIT:\x00>' ) + bx , hash_func ) . digest ( ) <EOL> v = hmac . new ( k , v , hash_func ) . digest ( ) <EOL> k = hmac . new ( k , v + b ( '<STR_LIT>' ) + bx , hash_func ) . digest ( ) <EOL> v = hmac . new ( k , v , hash_func ) . digest ( ) <EOL> while True : <EOL> t = b ( '<STR_LIT>' ) <EOL> while len ( t ) < rolen : <EOL> v = hmac . new ( k , v , hash_func ) . digest ( ) <EOL> t += v <EOL> secret = bits2int ( t , qlen ) <EOL> if secret >= <NUM_LIT:1> and secret < generator . order ( ) : <EOL> return secret <EOL> k = hmac . new ( k , v + b ( '<STR_LIT:\x00>' ) , hash_func ) . digest ( ) <EOL> v = hmac . new ( k , v , hash_func ) . digest ( ) </s>
