diff options
Diffstat (limited to 'alphanum/alphanum.py')
| -rw-r--r-- | alphanum/alphanum.py | 43 |
1 files changed, 41 insertions, 2 deletions
diff --git a/alphanum/alphanum.py b/alphanum/alphanum.py index 3272f6c..7049830 100644 --- a/alphanum/alphanum.py +++ b/alphanum/alphanum.py @@ -1,7 +1,46 @@ import random +import secrets +import string -POP = 'abcdefghijklmnopqrstuvwxyz0123456789' +POP = string.ascii_letters + string.digits -def generate(length=1) -> str: +def generate(length: int = 1) -> str: + """Generates a pseudo-random string of alphanumeric characters of the given + length. If no length is specified, a single character is returned. + + Args: + length (:obj:`int`, optional): Desired string length. Defaults to 1. + + Returns: + str: A pseudo-random alphanumeric string. + + Examples: + >>> print(generate()) + 'G' + >>> print(generate(10)) + 'a93jfDjdA0' + + """ return ''.join(random.SystemRandom().choices(POP, k=length)) + + +def generate_s(length: int = 1) -> str: + """Generates a cryptographically strong random string of alphanumeric + characters of the given length. If no length is specified, a single + character is returned. + + Args: + length (:obj:`int`, optional): Desired string length. Defaults to 1. + + Returns: + str: A random alphanumeric string. + + Examples: + >>> print(generate_s()) + '5' + >>> print(generate_s(10)) + 't3g0Gh9Naj' + + """ + return ''.join(secrets.SystemRandom().choice(POP) for i in range(length)) |
