Tutorialspoint.dev

Secrets | Python module to Generate secure random numbers

The secrets module is used for generating random numbers for managing important data such as passwords, account authentication, security tokens, and related secrets, that are cryptographically strong. This module is responsible for providing access to the most secure source of randomness. This module is present in Python 3.6 and above.

Random Numbers: class secrets.SystemRandom

This class uses the os.urandom() function for the generation of random numbers from sources provided by the operating system.

  1. secrets.choice(sequence): This function returns a randomly-chosen element from a non-empty sequence to manage a basic level of security.
    Example 1 : Generate a ten-character alphanumeric password.

    import secrets
    import string
      
    alphabet = string.ascii_letters + string.digits
    password = ''.join(secrets.choice(alphabet) for i in range(10))
      
    print(password)

    Output :



    'tmX47l1uo4'
    

    Example 2 : Generate a ten-character alphanumeric password with at least one lowercase character, at least one uppercase character, and at least three digits.

    import secrets
    import string
      
    alphabet = string.ascii_letters + string.digits
    while True:
        password = ''.join(secrets.choice(alphabet) for i in range(10))
        if (any(c.islower() for c in password) and any(c.isupper() 
        for c in password) and sum(c.isdigit() for c in password) >= 3):
            print(password)
            break

    Output :

    Tx8LppU05Q
    
  2. secrets.randbelow(n): This function returns a random integer in the range [0, n).
    import secrets
      
    passwd = secrets.randbelow(20)
    print(passwd)

    Output :

    2
    
  3. secrets.randbits(k): This function returns an int with k random bits.
    import secrets
      
    passwd = secrets.randbits(7)
    print(passwd)

    Output :

    61
    

Generating tokens

This module provides several functions for generating secure tokens for applications such as password resets, hard-to-guess URLs etc.

  1. secrets.token_bytes([nbytes=None]) : This function is responsible for generating a random byte string containing nbytes number of bytes. If no value is provided, a reasonable default is used.
    import secrets
      
    token1 = secrets.token_bytes()
    token2 = secrets.token_bytes(10)
      
    print(token1)
    print(token2)

    Output :

    b"x86?x85xcfx8ek8udx8ax92x8b>Rxc7x89_xc4xxce'u]x95x0cx05*?HG8xfb"
    b'Dxxe8x7fxc05xdfxe0xf6xe1'
    
  2. secrets.token_hex([nbytes=None]) : This function is responsible for generating a random text string in hexadecimal containing nbytes random bytes. If no value is provided, a reasonable default is used.
    import secrets
      
    token1 = secrets.token_hex(16)
    token2 = secrets.token_hex(9)
      
    print(token1)
    print(token2)

    Output :

    5d894a501c88fbe735c6ff496a6d3e51
    78baed9057e597dce4
    
  3. secrets.token_urlsafe([nbytes=None]) : This function is responsible for generating a random URL-safe text string containing nbytes random bytes. This is suitable for password recovery applications.
    Example : Generate a hard-to-guess temporary URL containing a security token.

    import secrets
      
    url = 'https://mydomain.com/reset=' + secrets.token_urlsafe()
    print(url)

    Output :

    https://mydomain.com/reset=GbOiFIvhMoqWsfaTQKbj8ydbo8G1lsMx1ECa6SXjb1s
    
  4. How many bytes should tokens use?
    At least 32 bytes for tokens should be used to be secure against a brute-force attack.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter