How to Generate Passwords with C++

Generating random passwords is one of the initial exercises for anyone who wants to advance in system security.


How to Generate Passwords with C++


Generating random passwords is one of the initial exercises for anyone who wants to advance in system security. We have already written an article explaining how you can generate passwords, for more details read the full post: 23 Ways to Generate and Save STRONG PASSWORDS; see command explanations.

In this article we will learn about the logic of generating passwords with C++. It will be a simple code using only alphanumeric passwords, but you can improve it by adding special characters to make the code even more complete.

Let’s go step by step!


01. Create a single header file

In this example, we will organize our code so that if we want to add it to a project in the future, we will create a “header-only” (just one file: .hpp), that is, the class and the execution in the same file:

vim genpass.hpp

#pragma once
#include <iostream>
#include <memory> // Optional

class GenPass {};

#02. Adding the content Let’s now add 2 private members to use in our member function, they are:

  • std::string pass - Which will store the string that will be returned to the object;
  • and const std::string charset - The constant that will contain alphanumeric characters, both lowercase and UPPERCASE, in addition to numbers;
#pragma once
#include <iostream>
#include <memory>

class GenPass {
   std::string pass;
   const std::string charset = "abcdefghijklmnopqrstuvwxyz"
     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     "0123456789";
};

#03. Add the member function that will return our password This member function will receive the length of the password as an argument, you could choose a default minimum value, but I will leave any length possible, but it is good to remember that strong passwords have at least 8 characters!

  • It reserves the number of characters informed in the argument;
  • Initializes the timer to generate the capture of charset characters randomly;
  • Increments the character pass member until reaching the maximum specified in the argument;
  • Finally returns the member.
std::string gen_pass(int length){
   this->pass.reserve(length);
   srand(static_cast<unsigned int>(time(nullptr)));
   for (int i = 0; i < length; ++i) {
     this->pass += this->charset[
       rand() % this->charset.length()
     ];
   }
   return pass;
}

#04. Final genpass.hpp file In the end, our complete file will look like this:

#pragma once
#include <iostream>
#include <memory>

class GenPass {
   std::string pass;
   const std::string charset = "abcdefghijklmnopqrstuvwxyz"
     "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
     "0123456789";
   public:
   std::string gen_pass(int length){
     this->pass.reserve(length);
     srand(static_cast<unsigned int>(time(nullptr)));
     for (int i = 0; i < length; ++i) {
       this->pass += this->charset[
         rand() % this->charset.length()
       ];
     }
     return pass;
   }
};

Implementing our Password Generator with C++

Let’s create a new file main.cpp and include our genpass.hpp, and call the member function gen_pass() with the number of characters we want our password to have, in this example: Generating an alphanumeric password with 8 characters!

#include "genpass.hpp"

int main(){
   auto g = std::make_unique<GenPass>();
   std::cout << g->gen_pass(8) << '\n';
   return 0;
}

Compile and run: g++ main.cpp && ./a.out. Probable and similar output:

skkb8Bms

If you want to generate with: 16, 32, 64 and 128 characters, respectively:

std::cout << g->gen_pass(16) << '\n';
std::cout << g->gen_pass(32) << '\n';
std::cout << g->gen_pass(64) << '\n';
std::cout << g->gen_pass(128) << '\n';

Simple and easy, right?! Now you can use this unique header and add it to your projects!


cpp


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles