basic password generator

This commit is contained in:
Christoph J. Scherr 2023-09-05 10:59:21 +02:00
parent 25ddac10df
commit 90535e3237
2 changed files with 39 additions and 3 deletions

View File

@ -166,3 +166,36 @@ Take a look at the provided Code Example.
[Code Example](src/miniweb.py)
</details>
## Random Password generator
Difficulty: 2/5
1. Generate a string of 16 random alphanumeric characters.
2. When starting your script, take a number for a CLI Argument. Generate a random string of this
length.
Example:
```bash
$ python ./randomString.py 60
n51uxDLu3BnxZ1D00gYKYRcG2jh1Y6uulHgrJ0TK3w5FtWl6wm8U0azNtxw0
# ^^^^ the above is 60 characters ^^^^
```
<details>
<summary>Hints</summary>
- Use `random.choice` to generate a random character
- build your own alphabet string
- Use `sys.argv` to access the CLI Arguments
</details>
<details>
<summary>Solution</summary>
Take a look at the provided Code Example.
[Code Example](src/randomString.py)
</details>

View File

@ -1,11 +1,14 @@
#!/usr/bin/env python3
import random
import string
import sys
def get_random_string(length):
# choose from all lowercase letter
letters = string.ascii_lowercase
result_str = ''.join(random.choice(letters) for i in range(length))
alphabet = string.ascii_lowercase
alphabet += string.ascii_uppercase
alphabet += "0123456789"
result_str = ''.join(random.choice(alphabet) for i in range(length))
return result_str
print(get_random_string(20))
print(get_random_string(int(sys.argv[1])))