Jump to content

What's causing the lag? C# Password generation

AlTech
Go to solution Solved by Mr_KoKa,

Do you increment currentLength anywhere? I think you forgot about that, your loop is infinite because of that.

So I recently decided I wanted to make my own password generation program.

 

I've noticed that I'm getting extremely high CPU usage and it takes forever.

 

Anybody know what the problem is?

 

 

The code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PasswordGenLib
{
   public class PasswordGenerator
    {
        Random random = new Random();

        string outputPassword = "";

        public string returnPassword()
        {
            return outputPassword;
        }

        public void generatePassword(int length, bool includeSpecialChars)
        {
            if(length < 8)
            {
                MessageBox.Show("Warning: Your password is not long enough! Please make it 8 characters or longer", "Password Difficulty Risk");
            }
            int currentLength = 0;
            while(currentLength < length)
            {
                Thread.Sleep(50);
                chooseCharacter(includeSpecialChars);
            }
        }

        private void chooseCharacter(bool includeSpecialChars)
        {
                if(includeSpecialChars == true)
            {
                int i = random.Next(4);

                if(i == 0)
                {
                    chooseCapital();
                }
                if (i == 1)
                {
                    chooseLowerCase();
                }
                if (i == 2)
                {
                    chooseNumber();
                }
                if(i == 3)
                {
                    chooseSpecialCharacter();
                }
            }

            else if(includeSpecialChars == false)
            {
                int i = random.Next(3);

                if (i == 0)
                {
                    chooseCapital();
                }
                if (i == 1)
                {
                    chooseLowerCase();
                }
                if (i == 2)
                {
                    chooseNumber();
                }
            }
        }

        private void chooseCapital()
        {
            int i = random.Next(26);

            if (i == 0)
            {
                outputPassword += "A";
            }
            if (i == 1)
            {
                outputPassword += "B";
            }
            if (i == 2)
            {
                outputPassword += "C";
            }
            if (i == 3)
            {
                outputPassword += "D";
            }
            if (i == 4)
            {
                outputPassword += "E";
            }
            if (i == 5)
            {
                outputPassword += "F";
            }
            if (i == 6)
            {
                outputPassword += "G";
            }
            if (i == 7)
            {
                outputPassword += "H";
            }
            if (i == 8)
            {
                outputPassword += "I";
            }
            if (i == 9)
            {
                outputPassword += "J";
            }
            if (i == 10)
            {
                outputPassword += "K";
            }
            if (i == 11)
            {
                outputPassword += "L";
            }
            if (i == 12)
            {
                outputPassword += "M";
            }
            if (i == 13)
            {
                outputPassword += "N";
            }
            if (i == 14)
            {
                outputPassword += "O";
            }
            if (i == 15)
            {
                outputPassword += "P";
            }
            if (i == 16)
            {
                outputPassword += "Q";
            }
            if (i == 17)
            {
                outputPassword += "R";
            }
            if (i ==18)
            {
                outputPassword += "X";
            }
            if (i == 19)
            {
                outputPassword += "T";
            }
            if (i == 20)
            {
                outputPassword += "U";
            }
            if (i == 21)
            {
                outputPassword += "V";
            }
            if (i == 22)
            {
                outputPassword += "W";
            }
            if (i == 23)
            {
                outputPassword += "X";
            }
            if (i == 24)
            {
                outputPassword += "Y";
            }
            if (i == 25)
            {
                outputPassword += "Z";
            }
        }

        private void chooseLowerCase()
        {
            int i = random.Next(26);

            if (i == 0)
            {
                outputPassword += "a";
            }
            if (i == 1)
            {
                outputPassword += "b";
            }
            if (i == 2)
            {
                outputPassword += "c";
            }
            if (i == 3)
            {
                outputPassword += "d";
            }
            if (i == 4)
            {
                outputPassword += "e";
            }
            if (i == 5)
            {
                outputPassword += "f";
            }
            if (i == 6)
            {
                outputPassword += "g";
            }
            if (i == 7)
            {
                outputPassword += "e";
            }
            if (i == 8)
            {
                outputPassword += "i";
            }
            if (i == 9)
            {
                outputPassword += "j";
            }
            if (i == 10)
            {
                outputPassword += "k";
            }
            if (i == 11)
            {
                outputPassword += "l";
            }
            if (i == 12)
            {
                outputPassword += "m";
            }
            if (i == 13)
            {
                outputPassword += "n";
            }
            if (i == 14)
            {
                outputPassword += "o";
            }
            if (i == 15)
            {
                outputPassword += "p";
            }
            if (i == 16)
            {
                outputPassword += "q";
            }
            if (i == 17)
            {
                outputPassword += "r";
            }
            if (i == 18)
            {
                outputPassword += "x";
            }
            if (i == 19)
            {
                outputPassword += "t";
            }
            if (i == 20)
            {
                outputPassword += "u";
            }
            if (i == 21)
            {
                outputPassword += "v";
            }
            if (i == 22)
            {
                outputPassword += "w";
            }
            if (i == 23)
            {
                outputPassword += "x";
            }
            if (i == 24)
            {
                outputPassword += "y";
            }
            if (i == 25)
            {
                outputPassword += "z";
            }
        }

        private void chooseNumber()
        {
            int i = random.Next(26);

            if (i == 0)
            {
                outputPassword += "0";
            }
            if (i == 1)
            {
                outputPassword += "1";
            }
            if (i == 2)
            {
                outputPassword += "2";
            }
            if (i == 3)
            {
                outputPassword += "3";
            }
            if (i == 4)
            {
                outputPassword += "4";
            }
            if (i == 5)
            {
                outputPassword += "5";
            }
            if (i == 6)
            {
                outputPassword += "6";
            }
            if (i == 7)
            {
                outputPassword += "7";
            }
            if (i == 8)
            {
                outputPassword += "8";
            }
            if (i == 9)
            {
                outputPassword += "9";
            }
        }

        private void chooseSpecialCharacter()
        {
            int i = random.Next(7);

            if (i == 0)
            {
                outputPassword += "$";
            }
            if (i == 1)
            {
                outputPassword += "#";
            }
            if (i == 2)
            {
                outputPassword += "%";
            }
            if (i == 3)
            {
                outputPassword += "&";
            }
            if (i == 4)
            {
                outputPassword += "+";
            }
            if (i == 5)
            {
                outputPassword += "-";
            }
            if(i == 6)
            {
                outputPassword += "~";
            }
        }
    }
}

 

Thanks :). I am aware that the code is not as efficient as it COULD be. But I'm focused on fixing this problem first before tackling efficiency.

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

Wow. You code like how I do. But I always get chewed on. Think you can use an array for this. 

 

2 minutes ago, Mr_KoKa said:

Do you increment currentLength anywhere? I think you forgot about that, your loop is infinite because of that.

 

That^ you're most likely stuck in that while loop

i5 2400 | ASUS RTX 4090 TUF OC | Seasonic 1200W Prime Gold | WD Green 120gb | WD Blue 1tb | some ram | a random case

 

Link to comment
Share on other sites

Link to post
Share on other sites

While I don't think it should be causing a lot of problems, having 50+ if statements doesn't do a program any good.

 

When dealing with generating characters, you don't use giant if-then block. You can compress this to basically one line. For example, if you're dealing with ASCII:

asciifull.gif

 

And you were dealing with capital letters, you would do something like

password += (char)(0x41 + randomNumber)

Also you should be using if / else-if unless you really need to check the value again. Or use a switch-case.

Edited by M.Yurizaki
I think saying it as if / else-if is more clear :3
Link to comment
Share on other sites

Link to post
Share on other sites

your code is painful to read, but I'll make some constructive critics.

Apart from everything that has already been said, I don't see the point in making the program sleep after you generate each character. If you get rid of that the program will be faster and more lightweight.

The best way to measure the quality of a piece of code is "Oh F*** "s per line

Link to comment
Share on other sites

Link to post
Share on other sites

I think its because you have many if statements, you can use ascii or just build an array of possible characters to make it way more efficient.

Link to comment
Share on other sites

Link to post
Share on other sites

Jokes aside I'm going to play devils advocate and be rather harsh now...

 

After more than 2 years you are still regurgitating this garbage on here... I gave up a long time ago trying to help you improve after you consistently ignored all of the advice offered to you (not just by myself). What are you doing? I know that I've asked you this question before but when you sit there hammering that crap out does it even begin to occur to you on any level at all that something is not quite right with what you are doing?

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, AluminiumTech said:

The code:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PasswordGenLib
{
   public class PasswordGenerator
    {
        Random random = new Random();

        string outputPassword = "";

        public string returnPassword()
        {
            return outputPassword;
        }

        public void generatePassword(int length, bool includeSpecialChars)
        {
            if(length < 8)
            {
                MessageBox.Show("Warning: Your password is not long enough! Please make it 8 characters or longer", "Password Difficulty Risk");
            }
            int currentLength = 0;
            while(currentLength < length)
            {
                Thread.Sleep(50);
                chooseCharacter(includeSpecialChars);
            }
        }

        private void chooseCharacter(bool includeSpecialChars)
        {
                if(includeSpecialChars == true)
            {
                int i = random.Next(4);

                if(i == 0)
                {
                    chooseCapital();
                }
                if (i == 1)
                {
                    chooseLowerCase();
                }
                if (i == 2)
                {
                    chooseNumber();
                }
                if(i == 3)
                {
                    chooseSpecialCharacter();
                }
            }

            else if(includeSpecialChars == false)
            {
                int i = random.Next(3);

                if (i == 0)
                {
                    chooseCapital();
                }
                if (i == 1)
                {
                    chooseLowerCase();
                }
                if (i == 2)
                {
                    chooseNumber();
                }
            }
        }

        private void chooseCapital()
        {
            int i = random.Next(26);

            if (i == 0)
            {
                outputPassword += "A";
            }
            if (i == 1)
            {
                outputPassword += "B";
            }
            if (i == 2)
            {
                outputPassword += "C";
            }
            if (i == 3)
            {
                outputPassword += "D";
            }
            if (i == 4)
            {
                outputPassword += "E";
            }
            if (i == 5)
            {
                outputPassword += "F";
            }
            if (i == 6)
            {
                outputPassword += "G";
            }
            if (i == 7)
            {
                outputPassword += "H";
            }
            if (i == 8)
            {
                outputPassword += "I";
            }
            if (i == 9)
            {
                outputPassword += "J";
            }
            if (i == 10)
            {
                outputPassword += "K";
            }
            if (i == 11)
            {
                outputPassword += "L";
            }
            if (i == 12)
            {
                outputPassword += "M";
            }
            if (i == 13)
            {
                outputPassword += "N";
            }
            if (i == 14)
            {
                outputPassword += "O";
            }
            if (i == 15)
            {
                outputPassword += "P";
            }
            if (i == 16)
            {
                outputPassword += "Q";
            }
            if (i == 17)
            {
                outputPassword += "R";
            }
            if (i ==18)
            {
                outputPassword += "X";
            }
            if (i == 19)
            {
                outputPassword += "T";
            }
            if (i == 20)
            {
                outputPassword += "U";
            }
            if (i == 21)
            {
                outputPassword += "V";
            }
            if (i == 22)
            {
                outputPassword += "W";
            }
            if (i == 23)
            {
                outputPassword += "X";
            }
            if (i == 24)
            {
                outputPassword += "Y";
            }
            if (i == 25)
            {
                outputPassword += "Z";
            }
        }

        private void chooseLowerCase()
        {
            int i = random.Next(26);

            if (i == 0)
            {
                outputPassword += "a";
            }
            if (i == 1)
            {
                outputPassword += "b";
            }
            if (i == 2)
            {
                outputPassword += "c";
            }
            if (i == 3)
            {
                outputPassword += "d";
            }
            if (i == 4)
            {
                outputPassword += "e";
            }
            if (i == 5)
            {
                outputPassword += "f";
            }
            if (i == 6)
            {
                outputPassword += "g";
            }
            if (i == 7)
            {
                outputPassword += "e";
            }
            if (i == 8)
            {
                outputPassword += "i";
            }
            if (i == 9)
            {
                outputPassword += "j";
            }
            if (i == 10)
            {
                outputPassword += "k";
            }
            if (i == 11)
            {
                outputPassword += "l";
            }
            if (i == 12)
            {
                outputPassword += "m";
            }
            if (i == 13)
            {
                outputPassword += "n";
            }
            if (i == 14)
            {
                outputPassword += "o";
            }
            if (i == 15)
            {
                outputPassword += "p";
            }
            if (i == 16)
            {
                outputPassword += "q";
            }
            if (i == 17)
            {
                outputPassword += "r";
            }
            if (i == 18)
            {
                outputPassword += "x";
            }
            if (i == 19)
            {
                outputPassword += "t";
            }
            if (i == 20)
            {
                outputPassword += "u";
            }
            if (i == 21)
            {
                outputPassword += "v";
            }
            if (i == 22)
            {
                outputPassword += "w";
            }
            if (i == 23)
            {
                outputPassword += "x";
            }
            if (i == 24)
            {
                outputPassword += "y";
            }
            if (i == 25)
            {
                outputPassword += "z";
            }
        }

        private void chooseNumber()
        {
            int i = random.Next(26);

            if (i == 0)
            {
                outputPassword += "0";
            }
            if (i == 1)
            {
                outputPassword += "1";
            }
            if (i == 2)
            {
                outputPassword += "2";
            }
            if (i == 3)
            {
                outputPassword += "3";
            }
            if (i == 4)
            {
                outputPassword += "4";
            }
            if (i == 5)
            {
                outputPassword += "5";
            }
            if (i == 6)
            {
                outputPassword += "6";
            }
            if (i == 7)
            {
                outputPassword += "7";
            }
            if (i == 8)
            {
                outputPassword += "8";
            }
            if (i == 9)
            {
                outputPassword += "9";
            }
        }

        private void chooseSpecialCharacter()
        {
            int i = random.Next(7);

            if (i == 0)
            {
                outputPassword += "$";
            }
            if (i == 1)
            {
                outputPassword += "#";
            }
            if (i == 2)
            {
                outputPassword += "%";
            }
            if (i == 3)
            {
                outputPassword += "&";
            }
            if (i == 4)
            {
                outputPassword += "+";
            }
            if (i == 5)
            {
                outputPassword += "-";
            }
            if(i == 6)
            {
                outputPassword += "~";
            }
        }
    }
}

 

Please learn what an array is. There's no need for 25 if statements. https://msdn.microsoft.com/en-us/library/9b9dty7d.aspx

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, AluminiumTech said:

 

I've noticed that I'm getting extremely high CPU usage and it takes forever.

Also, this is because you are branching constantly and because you are branching on random numbers, you get a bunch of cache misses due to branch prediction failures. USE A LIST TO REPRESENT YOUR ALPHABET

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

I would say a list would be useful, but at the same time, don't save what you can cheaply generate. I just find it silly to redefine something that already exists.

 

Then again I come from the embedded world so maybe having your program eat another 100 bytes wouldn't matter so much.

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, Mr_KoKa said:

Do you increment currentLength anywhere? I think you forgot about that, your loop is infinite because of that.

Oh lol xD. I forgot about that.

 

Thanks.

 

5 hours ago, straight_stewie said:

Please learn what an array is. There's no need for 25 if statements. https://msdn.microsoft.com/en-us/library/9b9dty7d.aspx

I do know what an array is. I will probably re-write a few methods which so that it uses an array instead of this.

 

5 hours ago, Nuluvius said:

Jokes aside I'm going to play devils advocate and be rather harsh now...

 

After more

than 2 years you are still regurgitating this garbage on here... I gave up a long time ago trying to help you improve after you consistently ignored all of the advice offered to you (not just by myself). What are you doing? I know that I've asked you this question before but when you sit there hammering that crap out does it even begin to occur to you on any level at all that something is not quite right with what you are doing?

In this case I know I did something wrong. I just didn't know the extent of it.

 

How have I consistently ignored answers or replies by you or others?

 

and how am I regurgitating the same "garbage"?

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

This code is a mess, cleaning it up would make debugging much easier for you

Link to comment
Share on other sites

Link to post
Share on other sites

10 minutes ago, AluminiumTech said:

I do know what an array is. I will probably re-write a few methods which so that it uses an array instead of this.

Well, If I were you, I would hit ctrl+a and then backspace on everything you have and go ahead and do it with an array. It's less code and it's faster.

The code you have is soooo slow. Literally every time it branches (that is, every time you encounter an if statement that is checked) will result in a cache miss, then a cache flush, then rebuilding the cache. This is extremely slow compared to indexing into an array that's already in the cache. At the very least, replace all your child If statements with else if statements, that will make it so that no statements will be checked after it finds one that evaluates to true. At the very best, have an array with all of the possible characters, and then get your random character for the word by generating a random number in the range (0, lengthOfAlphabet) and indexing into your array.
 

 

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, straight_stewie said:

Well, If I were you, I would hit ctrl+a and then backspace on everything you have and go ahead and do it with an array. It's less code and it's faster.

The code you have is soooo slow. Literally every time it branches (that is, every time you encounter an if statement that is checked) will result in a cache miss, then a cache flush, then rebuilding the cache. This is extremely slow compared to indexing into an array that's already in the cache. At the very least, replace all your child If statements with else if statements, that will make it so that no statements will be checked after it finds one that evaluates to true. At the very best, have an array with all of the possible characters, and then get your random character for the word by generating a random number in the range (0, lengthOfAlphabet) and indexing into your array.
 

 

Is this any better?

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PasswordGenLib
{
   public class PasswordGenerator
    {
        Random random = new Random();

        string outputPassword = "";

        public string returnPassword()
        {
            return outputPassword;
        }

        public void generatePassword(int length, bool includeSpecialChars)
        {
            if(length < 8)
            {
                MessageBox.Show("Warning: Your password is not long enough! Please make it 8 characters or longer", "Password Difficulty Risk");
            }
            int currentLength = 0;
            while(currentLength < length)
            {
                Thread.Sleep(50);
                chooseCharacter(includeSpecialChars);
                currentLength++;
            }
        }

        private void chooseCharacter(bool includeSpecialChars)
        {
                if(includeSpecialChars == true)
            {
                int i = random.Next(4);

                if(i == 0)
                {
                    chooseCapital();
                }
                else if (i == 1)
                {
                    chooseLowerCase();
                }
               else if (i == 2)
                {
                    chooseNumber();
                }
               else if(i == 3)
                {
                    chooseSpecialCharacter();
                }
            }

            else if(includeSpecialChars == false)
            {
                int i = random.Next(3);

                if (i == 0)
                {
                    chooseCapital();
                }
               else if (i == 1)
                {
                    chooseLowerCase();
                }
               else if (i == 2)
                {
                    chooseNumber();
                }
            }
        }

        private void chooseCapital()
        {
            string[] capitals = new string[26];

            capitals[0] = "A";
            capitals[1] = "B";
            capitals[2] = "C";
            capitals[3] = "D";
            capitals[4] = "E";
            capitals[5] = "F";
            capitals[6] = "G";
            capitals[7] = "H";
            capitals[8] = "I";
            capitals[9] = "J";
            capitals[10] = "K";
            capitals[11] = "L";
            capitals[12] = "M";
            capitals[13] = "N";
            capitals[14] = "O";
            capitals[15] = "P";
            capitals[16] = "Q";
            capitals[17] = "R";
            capitals[18] = "S";
            capitals[19] = "T";
            capitals[20] = "U";
            capitals[21] = "V";
            capitals[22] = "W";
            capitals[23] = "X";
            capitals[24] = "Y";
            capitals[25] = "Z";

            int i = random.Next(26);

           outputPassword += capitals[i];
        }

        private void chooseLowerCase()
        {
            string[] lowercase = new string[26];       
                
            lowercase[0] = "a";
            lowercase[1] = "b";
            lowercase[2] = "c";
            lowercase[3] = "d";
            lowercase[4] = "e";
            lowercase[5] = "F";
            lowercase[6] = "g";
            lowercase[7] = "h";
            lowercase[8] = "i";
            lowercase[9] = "j";
            lowercase[10] = "k";
            lowercase[11] = "l";
            lowercase[12] = "m";
            lowercase[13] = "n";
            lowercase[14] = "o";
            lowercase[15] = "p";
            lowercase[16] = "q";
            lowercase[17] = "r";
            lowercase[18] = "s";
            lowercase[19] = "t";
            lowercase[20] = "u";
            lowercase[21] = "V";
            lowercase[22] = "w";
            lowercase[23] = "x";
            lowercase[24] = "y";
            lowercase[25] = "z";

            int i = random.Next(26);

            outputPassword += lowercase[i];
        }

        private void chooseNumber()
        {
            string[] numbers = new string[10];

            numbers[0] = "0";
            numbers[1] = "1";
            numbers[2] = "2";
            numbers[3] = "3";
            numbers[4] = "4";
            numbers[5] = "5";
            numbers[6] = "6";
            numbers[7] = "7";
            numbers[8] = "8";
            numbers[9] = "9";

            int i = random.Next(10);

            outputPassword += numbers[i];
        }

        private void chooseSpecialCharacter()
        {
            string[] specialCharacters = new string[10];

            specialCharacters[0] = "#";
            specialCharacters[1] = "$";
            specialCharacters[2] = "%";
            specialCharacters[3] = "&";
            specialCharacters[4] = "+";
            specialCharacters[5] = "=";
            specialCharacters[6] = "~";

            int i = random.Next(7);

            outputPassword += specialCharacters[i];
        }
    }
}

 

Judge a product on its own merits AND the company that made it.

How to setup MSI Afterburner OSD | How to make your AMD Radeon GPU more efficient with Radeon Chill | (Probably) Why LMG Merch shipping to the EU is expensive

Oneplus 6 (Early 2023 to present) | HP Envy 15" x360 R7 5700U (Mid 2021 to present) | Steam Deck (Late 2022 to present)

 

Mid 2023 AlTech Desktop Refresh - AMD R7 5800X (Mid 2023), XFX Radeon RX 6700XT MBA (Mid 2021), MSI X370 Gaming Pro Carbon (Early 2018), 32GB DDR4-3200 (16GB x2) (Mid 2022

Noctua NH-D15 (Early 2021), Corsair MP510 1.92TB NVMe SSD (Mid 2020), beQuiet Pure Wings 2 140mm x2 & 120mm x1 (Mid 2023),

Link to comment
Share on other sites

Link to post
Share on other sites

No, it's not any better. There's no reason why you'd have to manually type all the entries of the arrays. 

Learn about the ASCII table: http://www.asciitable.com/

Then you could use something like  Convert.ToChar  : https://msdn.microsoft.com/en-us/library/7hcy5390(v=vs.110).aspx

And learn to use  FOR , WHILE and so on.. they're very useful.

You could also just put all the characters in a string and just get a random number, then pick the n-th number from the string variable. No need for arrays for such a simple tool.

 

I don't have c# installed, or I would have come up with something in a few minutes.

Link to comment
Share on other sites

Link to post
Share on other sites

1 hour ago, AluminiumTech said:

Is this any better?

It is better, but there's still a lot that can be improved. Using the ASCII table/strings like mariushm mentioned is one way to improve it. Aside from the special characters, which can be a little more scattered depending on what is allowed, uppercase, lowercase, and numbers are all in neat groups on the ASCII table.

// Just an example. Clean it up as you see fit.
string specialCharacters = "#$%&+=~";
var rng = new Random();
var lowercase = (char) rng.Next(97, 123); // ASCII a - z
var uppercase = (char) rng.Next(65, 91); // ASCII A - Z
var numbers = (char) rng.Next(48, 58); // ASCII 0 - 9
var special = specialCharacters[rng.Next(specialCharacters.Length)];

How you want to combine them into a string is up to you. Normally StringBuilder is recommended when you're dealing with string concatenation in a loop.

// Again, just an example to show how to combine characters with StringBuilder.
var sb = new StringBuilder();
sb.Append(lowercase);
sb.Append(uppercase);
sb.Append(numbers);
sb.Append(special);
var password = sb.ToString();

Of course this still leaves the issue that Random shouldn't be used for anything that is meant to be secure. That's what System.Security.Cryptography.RNGCryptoServiceProvider is for. It makes the code a bit more complicated, as you have to work in byte arrays, but it's a secure random number generator.

var bytes = new byte[length];
using (var crng = new RNGCryptoServiceProvider())
{
    crng.GetBytes(bytes); // fill bytes array with random byte values
}
// Do something with bytes

// Some options include
//     - Converting to a Base64 string (characters include, A-Za-z0-9+/ and = for padding when the length isn't a multiple of 3)
//     - Converting byte groupings into integers and then using them (Int16 = 2 bytes, Int32 = 4 bytes, Int64 = 8 bytes)
//     - Convert each byte to an ASCII value
//     - etc
// Note: You can always ignore generated values and continue to generate more bytes if you aren't getting what you want.

Here is an example of it being used to create a secure Random class.

Link to comment
Share on other sites

Link to post
Share on other sites

8 hours ago, AluminiumTech said:

Is this any better?

For a very basic, insecure implementation, just to learn the idea, yes. It's better. But its still not quite there. You are still branching on whether a number should be a capital, a lower case, or a special character. What you should do, is have all of the possible characters in one array, and then generate a random number to index into that array. What you want is a truly random string as output, and the randomness will not be changed by what ChooseCharacter() does, but ChooseCharacter() is still significantly slowing your code. If it makes you feel more comfortable, you can even shuffle the array first, and then index it, but that still doesn't affect the randomness of the output.

ENCRYPTION IS NOT A CRIME

Link to comment
Share on other sites

Link to post
Share on other sites

You should probably use ASCII, and just have 2 if statemets with the llower case or upper case bla bla bla yada yada yada and just have them change the max random number and the min random number according to what the user wants and the ASCII table.

Link to comment
Share on other sites

Link to post
Share on other sites

Why are you using while loops? you have a length so this is clearly a use for a for loop.

 

for (int i = 0; i < length; i++)
	{
	    //code
	}

And way are you not you putting all the chars in an array and then using that to get the character? This would remove all of your if's and this would remove you putting a random function to call and as a final note you really should be using a string builder. 

 

  1. create an array holding all possible characters
  2. ask the user for a password length
  3. for loop from 0 up to user length
  4. pick a number from 0 to the length of the array passChars[randomNumber]
  5. add that to the password string
  6. return to user.

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×