Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Random password generation with conditions

I'm working on a random password generator to create passwords that meet certain conditions including, but not necessarily limited to:

  • minimum length: has to contain at least 8 characters
  • lower case letters: has to contain lower case letters (chosen from a set to avoid problems of having characters that can be mistaken as numbers)
  • upper case letters: has to contain upper case letters (again, chosen from a set)
  • digits: has to contain numbers

What would be the best algorithmic approach to ensure that the generated password meets all these?

I'm not looking for a complete solution, I only need a few good ideas and guidelines.

like image 769
Wabbitseason Avatar asked Mar 19 '11 12:03

Wabbitseason


People also ask

Is 12 characters a strong password?

Long: The longer a password, the more secure it is. A strong password should be at least 12 characters long. Random: Strong passwords use a combination of letters, numbers, cases, and symbols to form an unpredictable string of characters that doesn't resemble words or names.

Is there a safe password generator?

LastPass offers a powerful, secure password generator that's 100% free and backed by a range of additional features. It's available both online through the LastPass website and within the LastPass app. With it, you can specify exactly what sort of password you want to create.


2 Answers

1) randomly generate number L which will be the exact length of your password. Namely, generate is so that it is greater than 8
2) randomly generate a number LL which will be the number of lowercase letters. LC must be in range [1..L-2]
3) randomly generate number LU for uppercase. Must be in range [1..L-LL-1]
4) LD = L-LL-LU number of uppercase digits
5) randomly generate LL lowercase letters, LU uppercase letters, and LD digits and keep them in a list(array)
6) Shuffle the array randomly

hth

like image 88
Armen Tsirunyan Avatar answered Oct 01 '22 03:10

Armen Tsirunyan


  • create a character array containing a - z, A - Z, 0 - 9 (minus any characters that might be confusing per the question)
  • concatenate 8 randomly chosen characters from the array
  • test the result to see if it satisfies the requirements
  • if the requirements are not satisfied, start over

The algorithm should usually succeed on the first few iterations, and saves you from having to implement a shuffle algorithm.

like image 31
dana Avatar answered Oct 01 '22 02:10

dana