From 90535e3237f5c87c0f5a2f75566c7766c988c64d Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Tue, 5 Sep 2023 10:59:21 +0200 Subject: [PATCH] basic password generator --- Tasks.md | 33 +++++++++++++++++++++++++++++++++ src/randomString.py | 9 ++++++--- 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/Tasks.md b/Tasks.md index 541bac0..2c791f8 100644 --- a/Tasks.md +++ b/Tasks.md @@ -166,3 +166,36 @@ Take a look at the provided Code Example. [Code Example](src/miniweb.py) + +## 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 ^^^^ +``` + +
+Hints + +- Use `random.choice` to generate a random character +- build your own alphabet string +- Use `sys.argv` to access the CLI Arguments + +
+
+Solution + +Take a look at the provided Code Example. + +[Code Example](src/randomString.py) + +
diff --git a/src/randomString.py b/src/randomString.py index 1c79d4a..da76e67 100755 --- a/src/randomString.py +++ b/src/randomString.py @@ -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])))