HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonModerate

Selective password generator in C++ and Python

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
selectivegeneratorpasswordpythonand

Problem

I have written an extremely simple program that has rudimentary password generation with selective character types (symbols, numbers, uppercase, and lowercase). Here is the C++ code:

#include 
#include 
#include 

inline bool getOptions(const std::string & option);
inline long long unsigned getLength();

int main(){
    bool syms   = getOptions("Symbols"),
        nums    = getOptions("Numbers"),
        lower   = getOptions("Lowercase Chars"),
        upper   = getOptions("Uppercase Chars");

    if(!(syms || nums || lower || upper)){
        std::cout /\?",
                str_nums = "1234567890",
                str_lower = "abcdefghijklmnopqrstuvwxyz",
                str_upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    std::string allowedChars;

    if(syms) allowedChars += str_syms;
    if(nums) allowedChars += str_nums;
    if(lower) allowedChars += str_lower;
    if(upper) allowedChars += str_upper;

    long long unsigned passLength = getLength();

    std::string password;
    srand(time(NULL));
    for(long long unsigned i = 0; i  0){
            return passLength;
        }
    }
}


Then, I recreated the program in Python3, just for practice since I am learning the syntax of Python.

```
#!/usr/bin/python

from random import randint

def getOptions(option):
choice = ""
while True:

print("{} (y/N): ".format(option), end="")
choice = input()

if choice[0].lower() == 'y':
return True
elif choice[0].lower() == 'n':
return False

def getLength():
str_passLength = ""
passlength = 0
while True:

str_passLength = input("Password Length: ")

try:
passlength = int(str_passLength)
except:
print("\n\tERR: Invalid.\n")

if passlength > 0:
return passlength

def main():
syms = getOptions("Symbols")
nums = getOptions("Numbers")
lower = getOptions("Lowercase Chars")
upper = getOptions("Uppercase Cha

Solution

In both languages:

  • Why does your syms contain '%' twice and not contain an escaped '\\' at all? (Notice that '\?' is equivalent to '\\?' in Python, but I would hate to ship code relying on that.)



In Python:

-
Look up argparse and use it. This will cut down your "option parsing" code to near zero lines. (There's no standard equivalent in C++; but it'll be good if you can adopt similar control flow in C++. Mixing I/O with business logic is never a good thing.)

-
allowedChars[randint(0, len(allowedChars) - 1)] is a verbose way of saying random.choice(allowedChars).

-
At the bottom of your program, you should insert if __name__ == '__main__': in front of the call to main(), so that you can import this module from elsewhere without accidentally calling main() in the process.

In C++:

-
You repeat the keyword inline as if it means something in this context. Don't. Save inline for contexts where it actually has a meaningful effect on linkage (as in, where the program wouldn't compile without it).

-
Don't declare multiple variables in a single statement; e.g. replace bool x = a, y = b, z = c; with bool x = a; bool y = b; bool z = c;.

-
std::rand() is not a blessed way to get random bits in C++. Look up std::random_device and std::uniform_int_distribution. Admittedly the currently blessed way to get random bits is a bit verbose, but it's probably worth learning at some point.

-
Nit: You're missing at least #include and #include .

Context

StackExchange Code Review Q#161563, answer score: 10

Revisions (0)

No revisions yet.