Random password generator

A random password generator is software program or hardware device that takes input from a random or pseudo-random number generator and automatically generates a password. Random passwords can be generated manually, using simple sources of randomness such as dice or coins, or they can be generated using a computer.
While there are many examples of "random" password generator programs available on the Internet, generating randomness can be tricky and many programs do not generate random characters in a way that ensures strong security. A common recommendation is to use open source security tools where possible since they allow independent checks on the quality of the methods used. Note that simply generating a password at random does not ensure the password is a strong password, because it is possible, although highly unlikely, to generate an easily guessed or cracked password. In fact, there is no need at all for a password to have been produced by a perfectly random process: it just needs to be sufficiently difficult to guess.
A password generator can be part of a password manager. When a password policy enforces complex rules, it can be easier to use a password generator based on that set of rules than to manually create passwords.
Long strings of random characters are difficult for most people to memorize. Mnemonic hashes, which reversibly convert random strings into more memorable passwords, can substantially improve the ease of memorization. As the hash can be processed by a computer to recover the original 60-bit string, it has at least as much information content as the original string. Similar techniques are used in memory sport.
The naive approach.
Here are two code samples that a programmer who is not familiar with the limitations of the random number generators in standard programming libraries might implement:
C.
int
main(void)
 /* Length of the password */
 unsigned short int length = 8;
 /* Seed number for rand() */
 srand((unsigned int) time(0));
 /* ASCII characters 33 to 126 */
 while (length--) {
 putchar(rand() % 94 + 33);
 printf("\n");
 return EXIT_SUCCESS;
In this case, the standard C function "rand", which is a pseudo-random number generator, is initially seeded using the C functions "time", but later iterations use "rand" instead. According to the ANSI C standard, "time" returns a value of type "time t", which is implementation-defined, but most commonly a 32-bit integer containing the current number of seconds since January 1, 1970 ("see:" Unix time). There are about 31 million seconds in a year, so an attacker who knows the year (a simple matter in situations where frequent password changes are mandated by password policy) and the process ID that the password was generated with, faces a relatively small number, by cryptographic standards, of choices to test. If the attacker knows more accurately when the password was generated, he faces an even smaller number of candidates to test – a serious flaw in this implementation.
In situations where the attacker can obtain an encrypted version of the password, such testing can be performed rapidly enough so that a few million trial passwords can be checked in a matter of seconds. "See:" password cracking.
The function "rand" presents another problem. All pseudo-random number generators have an internal memory or state. The size of that state determines the maximum number of different values it can produce: an "n"-bit state can produce at most formula_1 different values. On many systems "rand" has a 31 or 32-bit state, which is already a significant security limitation. Microsoft documentation does not describe the internal state of the Visual C++ implementation of the C standard library "rand," but it has only 32767 possible outputs (15 bits) per call. Microsoft recommends a different, more secure function, rand_s, be used instead. The output of rand_s is cryptographically secure, according to Microsoft, and it does not use the seed loaded by the srand function. However its programming interface differs from rand.
PHP.
function pass_gen(int $length = 8): string
 $pass = array();
 for ($i = 0; $i < $length; $i++) {
 $pass[] = chr(mt_rand(32, 126));
 return implode($pass);
In the second case, the PHP function "microtime" is used, which returns the current Unix timestamp with microseconds. This increases the number of possibilities, but someone with a good guess of when the password was generated, for example, the date an employee started work, still has a reasonably small search space. Also, some operating systems do not provide time to microsecond resolution, sharply reducing the number of choices. Finally, the "rand" function usually uses the underlying C "rand" function, and may have a small state space, depending on how it is implemented. An alternative random number generator, mt_rand, which is based on the Mersenne Twister pseudorandom number generator, is available in PHP, but it also has a 32-bit state. There are proposals for adding strong random number generation to PHP.
Stronger methods.
A variety of methods exist for generating strong, cryptographically secure random passwords. On Unix platforms /dev/random and /dev/urandom are commonly used, either programmatically or in conjunction with a program such as makepasswd. Windows programmers can use the Cryptographic Application Programming Interface function CryptGenRandom. The Java programming language includes a class called "SecureRandom". Another possibility is to derive randomness by measuring some external phenomenon, such as timing user keyboard input.
Many computer systems already have an application (typically named "apg") to implement FIPS 181. FIPS 181—Automated Password Generator—describes a standard process for converting random bits (from a hardware random number generator) into somewhat pronounceable "words" suitable for a passphrase. However, in 1994 an attack on the FIPS 181 algorithm was discovered, such that an attacker can expect, on average, to break into 1% of accounts that have passwords based on the algorithm, after searching just 1.6 million passwords. This is due to the non-uniformity in the distribution of passwords generated, which can be addressed by using longer passwords or by modifying the algorithm.
Bash.
Here is a code sample that uses [[/dev/urandom]] to generate a password with a simple [[Bash (Unix shell)|Bash]] function. This function takes password length as a parameter, or uses 16 by default:
Java.
Here is a code sample (adapted from the class PasswordGenerator) that uses [[SecureRandom]] to generate a 10 hexadecimal character password:
String[] symbols = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};
int length = 10;
Random random = SecureRandom.getInstanceStrong(); // as of JDK 8, this should return the strongest algorithm available to the JVM
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
 int indexRandom = random.nextInt(symbols.length);
 sb.append(symbols[indexRandom]);
String password = sb.toString();
TypeScript.
An example of using the "crypto" module built into [[Node.js]]
<syntaxhighlight lang="typescript">
import * as crypto from "crypto";
function generate(length = 16) {
 const uppercase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 const lowercase = "abcdefghijklmnopqrstuvwxyz";
 const numbers = "0123456789";
 const symbols = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`
Any password generator is limited by the state space of the pseudo-random number generator used if it is based on one. Thus a password generated using a 32-bit generator is limited to 32 bits entropy, regardless of the number of characters the password contains.
Note, however, that a different type of attack might succeed against a password evaluated as 'very strong' by the above calculation.
Password generator programs and websites.
A large number of password generator programs and websites are available on the Internet. Their quality varies and can be hard to assess if there is no clear description of the source of randomness that is used and if source code is not provided to allow claims to be checked. Furthermore, and probably most importantly, transmitting candidate passwords over the Internet raises obvious security concerns, particularly if the connection to the password generation site's program is not properly secured or if the site is compromised in some way. Without a [[secure channel]], it is not possible to prevent eavesdropping, especially over public networks such as the [[Internet]]. A possible solution to this issue is to generate the password using a client-side programming language such as JavaScript. The advantage of this approach is that the generated password stays in the client computer and is not transmitted to or from an external server.
External links.
[[Category:Password authentication]]
[[Category:Applications of randomness]]
[[Category:Cryptographic algorithms]]