Previous topic

securitylib.advanced_crypto — Advanced cryptographic functions library

Next topic

securitylib.passwords — Creation and validation of user passwords.

This Page

securitylib.random — Secure generation of random numbers and strings

securitylib.random.get_random_bytes(length)

This function will generate strong random data for cryptographic usage.

Parameters:length (int) – The length of the generated string of random bytes.
Returns:str – The generated random bytes as a binary string.
securitylib.random.get_random_integer(min_result=0, max_result=65535)

Returns a random integer.

Parameters:
  • min_result (int) – The minimum number that can be generated.
  • max_result (int) – The maximum number that can be generated.
Returns:

int – The generated random number.

securitylib.random.get_random_token(length=20)

Generate a random token that satisfies 2 properties: unique and unpredictable.

Parameters:length (int) – The length of the token to be generated in bytes.
Returns:str – The generated token (hex).

Example:

>>> random.get_random_token() 
'0f280bd84a4c6ae15c2deddec28c8e2e94b00dba'
securitylib.random.get_random_boolean()

Returns a random boolean value.

Returns:bool – True or False.
securitylib.random.get_random_string(length, charset='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')

Returns a random string based on the given length and the character set.

Parameters:
  • length (int) – The length of the random string to be generated.
  • charset (str) – The char set to be used (default is a-zA-Z0-9).
Returns:

str – The generated random string.

securitylib.random.get_random_filename(length=12, extension=None, charset='abcdefghijklmnopqrstuvwxyz0123456789')

Returns a random filename based on the given length and extension. The dot between the filename and the extension is added automatically if an extension is given.

Parameters:
  • length (int) – The length of the random filename to be generated.
  • extension (str) – The extension to be appended to the filename.
  • charset (str) – The char set to be used (default is a-z0-9).
Returns:

str – The generated random filename.

Example:

>>> random.get_random_filename() 
'x7152s2lzbu5'
>>> random.get_random_filename(8) 
'b2exn8ah'
>>> random.get_random_filename(8, 'txt') 
'6fcldehx.txt'
securitylib.random.get_random_GUID()

Returns a random GUID.

Returns:str – The generated GUID.

Example:

>>> random.get_random_GUID() 
'A7093430-468C-BBB6-ED70-DFF7B609B7A7'