C++ Random Password Generator

T

Tuğberk Kaan Duman

I've created this C++ random password generator. You can set length, you can enable custom symbols. Then I went to The Password Meter and started checking some generated passwords of mine.

Based on the formula here, I've created a simplified version of their grading system, and also created a function for it. Now I can just do password_score(generated_password) and set a limit for the security of my password.

Here are some examples and how my program performs:

Code:
###
Generated password strength: 100/100
Password has been copied to clipboard!
### 1a,pA!T0c0&7 (PasswordMeter score: 100%)

###
Generated password strength: 100/100
Password has been copied to clipboard!
### 82F^Vh11Gl}1 (PasswordMeter score: 100%)

###
Generated password strength: 98/100
Password has been copied to clipboard!
### !3V44'w1 (PasswordMeter score: 100%)

###
Generated password strength: 64/100
Password has been copied to clipboard!
### $]1V9q (PasswordMeter score: 70%)

Am I doing it right? I know that there's always some room for improvement. I'll take any advice to make it better. Thanks!

Code:
string password_generator(const int length_of_password = 12, bool enable_symbols = false, bool copy_to_clipboard = false)
{
    vector<char> password;
    srand (static_cast<unsigned int>(time(nullptr)));

    //generates lowercase letters
    for(auto c = 1; c <= length_of_password; c = c + 4)
    {
        const auto v1 = rand() % 26;
        password.push_back(v1 + 'a');
    }

    //generates uppercase letters
    for(auto g = 3; g <= length_of_password; g = g + 4)
    {
        const auto v2 = rand() % 26;
        password.push_back(v2 + 'A');
    }

    //generates numbers
    for(auto k = 0; k <= length_of_password; k = k + 2)
    {
        const auto v3 = rand() % 10;
        password.push_back(v3 + '0');
    }

    if(enable_symbols)
    {
        //generates symbols
        for(auto g = 1; g <= length_of_password; g = g + 4)
        {
            const auto choice = rand() % 3;
            
            if(choice == 0)
            {
                const auto v4 = rand() % 14;
                password.push_back(v4 + '!');
            }

            if(choice == 1)
            {
                const auto v5 = rand() % 5;
                password.push_back(v5 + '[');
            }

            if(choice == 2)
            {
                const auto v6 = rand() % 4;
                password.push_back(v6 + '{');
            }
        }
    }

    random_device r;
    shuffle(password.begin(), password.end(), default_random_engine(r()));

    string returning_password;

    for(auto i = 0; i < length_of_password; i++)
    {
        returning_password.push_back(password[i]);
    }

    if(copy_to_clipboard)
        to_clipboard(returning_password);

    return returning_password;
}

Here's a main file: (I've just started learning to work with argvs so there might be some mistakes. Most of the functions in this program are something I've created -made easier-, they're custom. I strongly recommend checking bottom GitHub link first.)

Code:
#include <duman.h>
using namespace std;

int main(const int argc, char* argv[])
{
    cerr << "###\n";
    if(argc > 1 && argc <= 2)
    {
        if(string(argv[1]) == "-h")
        {
            cerr << "Usage  : " << get_file_name(argv[0]) << " -<Password_Length> -<Minimum_Password_Security_Score out of 100>\n";
            cerr << "Example: " << get_file_name(argv[0]) << " -12 -80\n\n";
            cerr << "Password will be automatically copied to your clipboard!\n";
            cerr << "###\n";
            return 1;
        }
        if(string(argv[1]) == "-v")
        {
            cerr << "Version: 1.3.0\n";
            cerr << "###\n";
            return 2;
        }
        if(string(argv[1]) == "-length" || string(argv[1]) == "-l")
        {
            cerr << "Try to use the command as:\n";
            cerr << get_file_name(argv[0]) << " -8 -98 # for a password that has length of 8 and security score of minimum 98\n";
            cerr << "###\n";
            return 2;
        }
    }
    if(argc > 2 && argc <= 3)
    {
        string raw_argument1 = argv[1];
        string raw_argument2 = argv[2];
        if(isalpha(raw_argument1[1]) || isalpha(raw_argument2[1]))
        {
            cerr << "Invalid parameters! Use -h to see how to use this program.\n";
            cerr << "###\n";
            return 1;
        }
        if(raw_argument1[0] != '-')
        {
            cerr << "Did you mean: " << get_file_name(argv[0]) << " -" << argv[1] << " " << raw_argument2 << " # missing dash before an argument!\n";
            cerr << "###\n";
            return 1;
        }
        if(raw_argument2[0] != '-')
        {
            cerr << "Did you mean: " << get_file_name(argv[0]) << " " << raw_argument1 << " -" << argv[2] << " # missing dash before an argument!\n";
            cerr << "###\n";
            return 1;
        }
        if(string(argv[1]).empty() || string(argv[2]).empty())
        {
            cerr << "Missing parameters! Use -h to see how to use this program!\n";
            cerr << "###\n";
            return 1;
        }
        raw_argument1.erase(raw_argument1.begin());
        raw_argument2.erase(raw_argument2.begin());
        const auto first_argument = stoi(raw_argument1);
        const auto second_argument = stoi(raw_argument2);
        auto seconds_since_start = 0;

        const auto start = time(nullptr);
        auto generated_password = password_generator(first_argument, true, true);
    
        while(password_score(generated_password) < second_argument)
        {
            if(password_score(generated_password) >= second_argument)
            {
                break;
            }
            generated_password = password_generator(first_argument, true, true);
            seconds_since_start += static_cast<int>(difftime(time(nullptr), start));
            if(seconds_since_start > 50)
            {
                cerr << "Request timed out. Couldn't generate a password with the given parameters.\n";
                cerr << "###\n";
                return 1;
            }
        }
        cerr << "Generated password strength: " << password_score(generated_password) << "/100\n";
        cerr << "Password has been copied to clipboard!\n";
        cerr << "###\n";
        return 0;
    }
    if(argc > 3)
    {
        cerr << "Unsupported number of parameters!\n";
        return 1;
    }
    cerr << "Commands:\n";
    cerr << "1. " << get_file_name(argv[0]) << " -length -security_level # Security level is XYZ out of 100\n";
    cerr << "2. " << get_file_name(argv[0]) << " -h\n";
    cerr << "3. " << get_file_name(argv[0]) << " -v\n";
    cerr << "###\n";
}

duman.h is a header file I've created for myself. So, whenever I solve a problem in some way I turn it into a function and save it in that header. You can reach it here: https://github.com/duman/duman.h
 

Unreplied Threads

What common proverb does this string refer to?

What common proverb is referenced by this three-character string?

ᚷᛖᚨ

Chromatic number of distance graphs on the integers with restricted prime divisors

  • MistressMadeline
  • Mathematics
  • Replies: 0
Let $P$ be a finite set of prime numbers. Define the distance graph $G(P, n)$ on the vertex set $V = \{1, 2, ..., n\}$ where two vertices $a$ and $b$ are connected by an edge if and only if $|a-b|$ is a product of primes in $P$. Denote the chromatic number of $G(P, n)$ by $\chi(G(P, n))$.

Can we find a function $f(k)$ such that $$ \chi(G(P, n)) \leq f(k) $$ for all $n$ and all sets $P$ with $|P| = k$?

Minimal bipartite graph density in the log-edge-density version

A graphon is a bounded measurable symmetric function $W:[0,1]^2\to [0,1]$. Let $\mathcal{W}$ denote the set of all graphons. For any $p\in [0,1]$, let $\mathcal{W}_p$ denote the set of all graphons $W$ such that $\int W=p$. For any graphon $W$ and any bipartite graph $H$ with bipartition $(A,B)$ where $|A|=v_1,\ |B|=v_2$, the density of $H$ in $W$ is defined as: $$t(H,W)=\int_{[0,1]^{v(H)}}\prod_{ij\in E(H)}W(x_i,y_j)\prod_{i\in A} dx_i \prod_{i\in B} dy_j .$$ Define $h(H,W)=\log_{\int W}t(H,W).$ For any $p\in (0,1)$ does the following hold? $$\max_{W\in\mathcal{W}}h(H,W)=\max_{W\in\mathcal{W}_p}h(H,W).$$

Positivity for the mild solution of a heat equation on the torus

Consider the following linear parabolic equation in one spatial dimension for $u=u(x,t)$ on the one-dimensional torus $\mathbb{T}^1,$ meaning $x \in \mathbb{T}^1$ and $t \in (0, T]:$ $$ \partial_t u- a \cdot\partial_{x}^2u +g(x,t)\cdot u= f(x,t),$$ with the constant $a > 0,$ and the given functions $g,f \geq 0,$ subjected to the strictly positive initial condition $u_0(x) \in C^1(\mathbb{T}^1).$ Let $f,g \in L^{\infty}((0, T); L^1(\mathbb{T}^1)).$

I need a well-posedness result (existence, uniqueness and positivity of the solution in the mild sense) for this equation on the torus.

A mild formulation is given by (representing the flat torus by $[0,1]$ and periodic boundary conditions): $$ u(x,t) = \int_{0}^{1} G(x-y,t) u_0(y) \, \mathrm{d}y + \int_{0}^{t} \int_{0}^{1} G(x-y,t-s) (f - g \cdot u)(y,s) \, \mathrm{d}y\mathrm{d}s, $$ where $G = G(x,t) > 0$ is the heat kernel on the one-dimensional torus. The existence of a local solution $u \in L^{\infty}(\mathbb{T}^1 \times (0,T))$ would follow from a Picard-Iteration, at least for small enough times. Since the right-hand side of the mild formulation would define a contraction from $L^{\infty}(\mathbb{T}^1 \times (0,T))$ to $L^{\infty}(\mathbb{T}^1 \times (0,T)).$ (I supposed semigroup arguments would fail here since $g$ depends on time.)

But how can I show positivity for the local solution? (The problem is I don't have enough regularity for the solution to use a maximum principle.)

Would be very grateful for help!

Subspaces of $\ell_\infty^3$

  • A beginner mathmatician
  • Mathematics
  • Replies: 0

Soldering Iron - How long to heat the pad and component leads?

  • Dr Negative
  • Physics
  • Replies: 0
I'm fairly new to soldering and am curious as to how long it's supposed to take to heat the component contacts and copper pads on a PCB to get to solder-melting temperature.

I'm using a YIHUA 939D+ III EVO Digital Soldering Iron Station set for 350 Celsius which I bought new just a few months ago; the tip is clean and tinned, and it's placed so it's touching both the pad and the component, but it seems to take much too long to actually heat anything. Usually around 45-60 seconds if I'm lucky.

I'm really stumped here as to whether this is normal, I just suck at soldering, or the iron is defective. (I have a feeling it's that I suck, though.)

Models have a weird dotted-like texture when viewing in Material Preview & Rendered, how do I fix this issue?

  • Vin Solo2000
  • Technology
  • Replies: 0
I recently got Blender v4.2.1 and already I'm starting to have a render issue when viewing in Material Preview and Rendered on EEVEE Render engine but when I go into Cycles Engine, it looks normal only in Rendered viewport. I decided to check if it was something with the nodes I use by loading a new General and turns out it has nothing to do with the nodes I use as the white dot texture issue still persisted, even for a model that doesn't have nodes. And it's definitely not any textures I give models as the cube doesn't have any textures added either.

Is there a way I could fix this issue so I could see the renders without any weird texture bug?

Render Comparison
Top