0

I'm rewriting this stopwatch code from C# to C++.

I have rewritten some of the code (I can attach it if you think it's helpful) but confused about the lines after var watch = Stopwatch.StartNew(). Does C++ have similar things like this? What kind of variable type should I put for watch in C++?

namespace BruteForcePasswordGeneration
{
    class Program
    {
        static void Main(string[] args)
        {
            char[] chars = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};

            int passwordLength=0;

            Console.WriteLine("Enter the password length");

            passwordLength = Convert.ToInt32(Console.ReadLine());

            BigInteger iPossibilities = (BigInteger)Math.Pow((double)chars.Length, (double)passwordLength);
            Console.WriteLine("{0} words total. Press enter to continue;", iPossibilities);
            Console.ReadLine();

            var watch = Stopwatch.StartNew();
            for (BigInteger i = 0; i < iPossibilities; i++)
            {
                string theword = "";
                BigInteger val = i;
                for (int j = 0; j < passwordLength; j++)
                {
                    BigInteger ch = val % chars.Length;
                    theword = chars[(int)ch] + theword;
                    val = val / chars.Length;
                }
                Console.WriteLine(theword);
            }
            watch.Stop();
            var elapsedMs = watch.ElapsedMilliseconds;
            Console.WriteLine("It took {0} seconds to generate {1} possible combinations", elapsedMs / 1000, iPossibilities);
            Console.ReadLine();

        }
    }
}
  • Hi, I was going to give my answer, but couldn't so I'll link to a similar answer here https://stackoverflow.com/a/13408517/9136356 – kowsikbabu Feb 07 '19 at 05:58

1 Answers1

1

Although you can write a "stopwatch" class in C++ if you want, the usual is to just use high_resolution_clock::now() to get the start and stop times. You then use duration_cast to get the difference in the form you want.

If you'll forgive me, I don't see any real point in requiring the user to enter the password length after starting the program. At least to me, it seems easier to use something like "gen_passwords 4" to generate all the passwords of length 4.

That gives code something on this general order:

#include <iostream>
#include <chrono>
#include <string>

int main(int argc, char **argv) { 
    static const std::string chars{ "ABCDEFGHIJKLMNOPQRSTUVWXYZ" };

    if (argc != 2) {
        std::cerr << "Usage: generate <password length>\n";
        return EXIT_FAILURE;
    }

    int passwordLength = std::stoi(argv[1]);

    unsigned long long iPossibilities = std::pow(chars.size(), passwordLength);

    using namespace std::chrono;

    auto start = high_resolution_clock::now();
    for (unsigned long long i = 0; i < iPossibilities; i++) {
        std::string theword;
        unsigned long long val = i;
        for (int j = 0; j < passwordLength; j++) {
            size_t ch = val % chars.size();
            theword = chars[ch] + theword;
            val /= chars.size();
        }
        std::cout << theword << '\n';
    }
    auto stop = high_resolution_clock::now();

    double elapsed = duration_cast<milliseconds>(stop - start).count() / 1000.0;
    std::cerr << "It took " << elapsed << " seconds to generate " << iPossibilities << " combinations\n";
}
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111