def generate_k(order, secexp, hash_func, data):
    '''
        order - order of the DSA generator used in the signature
        secexp - secure exponent (private key) in numeric form
        hash_func - reference to the same hash function used for generating hash
        data - hash in binary form of the signing data
    '''

    qlen = bit_length(order)
    holen = hash_func().digest_size
    rolen = (qlen + 7) / 8
    bx = number_to_string(secexp, order) + bits2octets(data, order)

    # Step B
    v = b('\x01') * holen

    # Step C
    k = b('\x00') * holen

    # Step D

    k = hmac.new(k, v+b('\x00')+bx, hash_func).digest()

    # Step E
    v = hmac.new(k, v, hash_func).digest()

    # Step F
    k = hmac.new(k, v+b('\x01')+bx, hash_func).digest()

    # Step G
    v = hmac.new(k, v, hash_func).digest()

    # Step H
    while True:
        # Step H1
        t = b('')

        # Step H2
        while len(t) < rolen:
            v = hmac.new(k, v, hash_func).digest()
            t += v

        # Step H3
        secret = bits2int(t, qlen)

        if secret >= 1 and secret < order:
            return secret

        k = hmac.new(k, v+b('\x00'), hash_func).digest()
        v = hmac.new(k, v, hash_func).digest()
def generate_k ( order , secexp , hash_func , data ) : qlen = bit_length ( order ) holen = hash_func ( ) . digest_size rolen = ( qlen + 7 ) / 8 bx = number_to_string ( secexp , order ) + bits2octets ( data , order ) v = b ( '\x01' ) * holen k = b ( '\x00' ) * holen k = hmac . new ( k , v + b ( '\x00' ) + bx , hash_func ) . digest ( ) v = hmac . new ( k , v , hash_func ) . digest ( ) k = hmac . new ( k , v + b ( '\x01' ) + bx , hash_func ) . digest ( ) v = hmac . new ( k , v , hash_func ) . digest ( ) while True : t = b ( '' ) while len ( t ) < rolen : v = hmac . new ( k , v , hash_func ) . digest ( ) t += v secret = bits2int ( t , qlen ) if secret >= 1 and secret < order : return secret k = hmac . new ( k , v + b ( '\x00' ) , hash_func ) . digest ( ) v = hmac . new ( k , v , hash_func ) . digest ( )
order - order of the DSA generator used in the signature
        secexp - secure exponent (private key) in numeric form
        hash_func - reference to the same hash function used for generating hash
        data - hash in binary form of the signing data