Random password generator
|
|
This article may contain original research. Please improve it by verifying the claims made and adding references. Statements consisting only of original research may be removed. More details may be available on the talk page. (August 2008) |
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.
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.
Contents |
[edit] 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:
[edit] C
#include <time.h> #include <stdio.h> #include <stdlib.h> int main(void) { /* Length of the password */ unsigned short int length = 8; /* Seed number for rand() */ srand((unsigned int) time(0) + getpid()); /* ASCII characters 33 to 126 */ while(length--) { putchar(rand() % 94 + 33); srand(rand()); } 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 and getpid, but later iterations use rand instead. According to the ANSI C standard, time returns a value of ftype 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), and getpid returns a pid t. 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 2n 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. [1] 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. [2]
[edit] PHP
function pass_gen($length = 8) { $pass = ''; srand((float) microtime() * 10000000); for ($i = 0; $i < $length; $i++) { $pass .= chr(rand(32, 126)); } return $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. [3]
[edit] 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 [1]. The Java programming language includes a class called SecureRandom. Windows programmers can use the Cryptographic Application Programming Interface function CryptGenRandom. Another possibility is to derive randomness by measuring some external phenomenon, such as timing user keyboard input.
These methods should prove adequate for most password generation needs, but their suitability will vary depending on the specific situation.
[edit] Bash
Here is a code sample that uses /dev/urandom to generate a password with a simple Bash function[2]. This function takes password length as a parameter, or uses 8 by default:
function mkpw() { head /dev/urandom | uuencode -m - | sed -n 2p | cut -c1-${1:-8}; }
Note that while /dev/urandom should be appropriate for most needs, it is not suitable for long term cryptographic keys or where an especially high level of security is required. In the case of the latter, a method employing /dev/random should be used [3].
[edit] Python
The language Python includes a SystemRandom class that obtains cryptographic grade random bits from /dev/urandom on a Unix-like system, including Linux and Mac OS X, while on Windows it uses CryptGenRandom.[4][5] Here is a simple Python 2 script that demonstrates the use of this class:
#!/usr/bin/python import random, string myrg = random.SystemRandom() length = 10 # If you want non-English characters, remove the [0:52] alphabet = string.letters[0:52] + string.digits pw = str().join(myrg.choice(alphabet) for _ in range(length)) print pw
[edit] PHP
A PHP program can open and read from /dev/urandom, if available, or invoke the Microsoft utilities.[6] A third option, if OpenSSL is available is to employ the function openssl_random_pseudo_bytes'.'[7]
[edit] Mechanical methods
Yet another method is to use physical devices such as dice to generate the randomness. One simple way to do this uses a 6 by 6 table of characters. The first die roll selects a row in the table and the second a column. So, for example, a roll of 2 followed by a roll of 4 would select the letter "j" from the table below.[8] To generate upper/lower case characters or some symbols a coin flip can be used, heads capital, tails lower case. If a digit was selected in the dice rolls, a heads coin flip might select the symbol above it on a standard keyboard, such as the '$' above the '4' instead of '4'.
-
1 2 3 4 5 6 1 a b c d e f 2 g h i j k l 3 m n o p q r 4 s t u v w x 5 y z 0 1 2 3 6 4 5 6 7 8 9
[edit] Type and strength of password generated
Random password generators normally output a string of symbols of specified length. These can be individual characters from some character set, syllables designed to form pronounceable passwords, or words from some word list to form a passphrase. The program can be customized to ensure the resulting password complies with the local password policy, say by always producing a mix of letters, numbers and special characters.
The Password strength of a random password against a particular attack (brute-force search), can be calculated by computing the information entropy of the random process that produced it. If each symbol in the password is produced independently and with uniform probability, the entropy in bits is given by the formula
where N is the number of possible symbols and L is the number of symbols in the password. The function log2 is the base-2 logarithm. H is typically measured in bits.[9][10]
-
Entropy per symbol for different symbol sets Symbol set Symbol count N Entropy per symbol H Arabic numerals (0-9) (e.g. PIN) 10 3.32 bits Hexadecimal numerals (0-9, A-F) (e.g. WEP key) 16 4.00 bits Case insensitive Latin alphabet (a-z or A-Z) 26 4.70 bits Case insensitive alphanumeric (a-z or A-Z, 0-9) 36 5.17 bits Case sensitive Latin alphabet (a-z, A-Z) 52 5.70 bits Case sensitive alphanumeric (a-z, A-Z, 0-9) 62 5.95 bits All ASCII printable characters 94 6.55 bits Diceware word list 7776 12.9 bits
| Desired password entropy H | Arabic numerals | Case insensitive Latin alphabet | Case insensitive alphanumeric | Case sensitive Latin alphabet | Case sensitive alphanumeric | All ASCII printable characters |
|---|---|---|---|---|---|---|
| 32 bits | 10 | 7 | 7 | 6 | 6 | 5 |
| 40 bits | 13 | 9 | 8 | 8 | 7 | 7 |
| 64 bits | 20 | 14 | 13 | 12 | 11 | 10 |
| 96 bits | 29 | 21 | 19 | 17 | 17 | 15 |
| 128 bits | 39 | 28 | 25 | 23 | 22 | 20 |
| 160 bits | 49 | 35 | 31 | 29 | 27 | 25 |
| 192 bits | 58 | 41 | 38 | 34 | 33 | 30 |
| 224 bits | 68 | 48 | 44 | 40 | 38 | 35 |
| 256 bits | 78 | 55 | 50 | 45 | 43 | 39 |
| 384 bits | 116 | 82 | 75 | 68 | 65 | 59 |
| 512 bits | 155 | 109 | 100 | 90 | 86 | 78 |
| 1024 bits | 309 | 218 | 199 | 180 | 172 | 156 |
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.
[edit] 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.
[edit] See also
- Cryptographically secure pseudorandom number generator
- Diceware
- Hardware random number generator
- Key size
- Password length parameter
- Password manager
[edit] References
- ^ http://www.cyberciti.biz/faq/generating-random-password/
- ^ http://mlawire.blogspot.com/2009/07/linux-password-generator.html
- ^ https://www.kernel.org/doc/man-pages/online/pages/man4/random.4.html
- ^ http://docs.python.org/py3k/library/random.html
- ^ http://docs.python.org/py3k/library/os.html#os.urandom
- ^ a sample PHP secure random program
- ^ http://php.net/manual/en/function.openssl-random-pseudo-bytes.php
- ^ Levine, John R., Ed.: Internet Secrets, Second edition, page 831 ff. John Wiley and Sons.
- ^ Schneier, B: Applied Cryptography, Second edition, page 233 ff. John Wiley and Sons.
- ^ "Electronic Authentication Guideline" (PDF). NIST. http://csrc.nist.gov/publications/nistpubs/800-63/SP800-63V1_0_2.pdf. Retrieved March 27, 2008.
