How to Create a Password Generator in C++

0 min read 71 words

You can easily create a password generator in C++ with the following simple method.

How to create the Password Generator in C++

#include <iostream>
#include <string>
#include <algorithm>
#include <random>

std::string generate_password(int length = 16) {
    std::string seed = string("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ") + string("0123456789") + string("!@#$%^&*()_+=-{}[]\\|:;<>,.?/");

    std::string password_string = seed;
    std::shuffle(password_string.begin(), password_string.end(), std::mt19937{std::random_device{}()});
    return password_string.substr(0, length);
}

int main() {
    std::cout << generate_password() << std::endl;
    std::cout << generate_password(28) << std::endl;
    return 0;
}
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags