Hack Forums
[C#] Secure Password Generator [Re-vamped] [Updated 12/29/2016] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] Secure Password Generator [Re-vamped] [Updated 12/29/2016] (/showthread.php?tid=5499149)



Secure Password Generator [Re-vamped] [Updated 12/29/2016] - Malum™ - Dec 20, 2016

I've had this project laying around awhile I made for a friend to further understand something (I don't recall anymore). Feel free to use this for account creators, login systems, etc. Ask any questions! I'm sorry I do not really comment my code.

GUI

[Image: adef7d6d87.png]

Password Generator Class
Spoiler (Click to View)

Secure Random Class
Spoiler (Click to View)

Usage
Spoiler (Click to View)

Project Source Download

Changed (12/29/2016)
  • Changed While Loop to For Loop.

  • Used StringBuilder.

  • Removed Math.Floor.

  • Removed issue with byte buffer being created every time.

  • Updated download link to new project source.





* - Chickendipper - Dec 20, 2016

Your code is very nice, however I do have one gripe with it.
You use a while loop to select characters randomly based off of your RNG. This is a lot more hassle and uses more resources, and is a messier solution.
Instead, what you could do is:
Code
StringBuilder charPoolBuilder = new StringBuilder();
if (UpperCase) charPoolBuilder.Append("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
// ... etc
StringBuilder myPassword = new StringBuilder();
for (int i = 0; i < passwordLength; i++) myPassword.Append(charPoolBuilder[RND.Next(charPoolBuilder.Length)]);
return myPassword.ToString();

This way, instead of doing twice the selection & having a messy case selection, you have a "one size fits all" solution. Now, if you wanted to add European characters (eg éíúóá), you could do so by doing
Code
if (European) charPoolBuilder.Append("éíúóá");
rather than having an extra case statement. Otherwise, your code is great and a great learning point to others about CSPRNGs.


* - Zilog80 - Dec 20, 2016

Pull Request for PasswordGenerator class

PHP Code

<?php 
while (password.Length < 12) 

Should be:

PHP Code

<?php 
while (password.Length < Length) 

If you change this you can do this:

PHP Code

<?php 
string password 
= pGenerator.Generate(12) //12 being the length
string password2 = pGenerator.Generate(15) //15 being the length
string password3 = pGenerator.Generate(25) //25 being the length 



* - Mr Whiskey - Dec 20, 2016

I'm confused, you have a box to choose the length but it's hard-coded to 12?


* - Kaspah - Dec 20, 2016

I mean, nice contribution but what's the point when I can use a web-based generator?


* - Chickendipper - Dec 20, 2016

(Dec 20, 2016 01:12 PM)Kaspah Wrote:  I mean, nice contribution but what's the point when I can use a web-based generator?

Are you retarded? He's making a contribution so that others can learn from it.


* - Mr Whiskey - Dec 20, 2016

(Dec 20, 2016 01:12 PM)Kaspah Wrote:  I mean, nice contribution but what's the point when I can use a web-based generator?

I think he gave a pretty good example in the OP. "Feel free to use this for account creators"


* - Malum™ - Dec 21, 2016

(Dec 20, 2016 01:06 PM)Zilog80 Wrote:  Pull Request for PasswordGenerator class

PHP Code

<?php 
while (password.Length < 12) 

Should be:

PHP Code

<?php 
while (password.Length < Length) 

If you change this you can do this:

PHP Code

<?php 
string password 
= pGenerator.Generate(12) //12 being the length
string password2 = pGenerator.Generate(15) //15 being the length
string password3 = pGenerator.Generate(25) //25 being the length 

Oh shit! My bad. I forgot to remove that. I was testing and forgot to remove that part. I'll fix it now.


* - passwords - Dec 22, 2016

Very nice, this "Friend" of yours must have been something really special.
Anyways, Good contribution to the community!


* - Malum™ - Dec 23, 2016

(Dec 22, 2016 12:40 PM)DOT1 Wrote:  Very nice, this "Friend" of yours must have been something really special.
Anyways, Good contribution to the community!

Why the hostility? It was someone on Skype wanting to learn about ListView/ListBox and Random String generation. So I threw this project together and added a little more to it to post on here.

EDIT: God damn it. DOT1 was the friend I made it for. Lmfao. You dick. Haha.


* - BahNahNah - Dec 23, 2016

Just some things to think about:

  1. Empty constructors can be omitted.

  2. StringBuilder is more efficient when... building a string. (Click to View)

  3. Decimal/float/double to int cast will drop the decimals, so no need for the extra Math.Floor.

  4. Creating a new byte buffer for every random number is quite costly. (Click to View)





* - Malum™ - Dec 23, 2016

  • Empty constructors can be omitted. - This seems extraordinarily picky..more like one of your pet-peeves.

  • StringBuilder is more efficient when... building a string. (Click to View) - Yea, that was my bad. I was having issues (brain fart) when using StringBuilder and resorted to concatenations. Never bothered to revert back.

  • Decimal/float/double to int cast will drop the decimals, so no need for the extra Math.Floor. - Didn't know that. Thank you!

  • Creating a new byte buffer for every random number is quite costly. (Click to View)



I would love and greatly appreciate whatever you use to benchmark. It would come in handy for future projects.


* - sinfulshotx - Jan 14, 2017

I just made one of these myself using very similar methods, great work. All improvements are done with practice and time.