Random password generator

A random password generator is a 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.[1] 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

# 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));

    /* 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 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.[2] 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.[3]

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[4] 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[5] 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.[6]

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.[7] 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.[8] 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.[9] 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.[10][11]

Bash

Here is a code sample that uses /dev/urandom to generate a password with a simple Bash function. This function takes password length as a parameter, or uses 16 by default:

function mkpw() { LC_ALL=C tr -dc '[:graph:]' < /dev/urandom | head -c ${1:-16}; echo; }

Java

Here is a code sample (adapted from the class PasswordGenerator[12]) that uses SecureRandom to generate a 10 hexadecimal character password:

char[] 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 returns a SecureRandom implementation known to be strong
StringBuilder sb = new StringBuilder(length);
for (int i = 0; i < length; i++) {
    int randomIndex = random.nextInt(symbols.length);
    sb.append(symbols[randomIndex]);
}
String password = sb.toString();

JavaScript

This example uses the Node.js Crypto module to generate cryptographically secure random numbers with uniform distribution.

"use strict";

import { randomInt } from "crypto";

export const secret = (length = 64) => {
  const upperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  const lowerCase = "abcdefghijklmnopqrstuvwxyz";
  const digits = "0123456789";
  const minus = "-";
  const underLine = "_";
  const special = "!\"#$%&'*+,./:;=?@\\^`|~";
  const brackets = "[]{}()<>";
  const alphabet = upperCase + lowerCase + digits + minus + underLine + special + brackets;
  let secret = "";
  for (let index = 0; index < length; index++) {
    secret += alphabet.charAt(randomInt(alphabet.length));
  }
  return secret;
};

Perl

This example uses the Crypt::Random::Source module to find a source of strong random numbers (which is platform dependent).

use Crypt::Random::Source qw(get_strong);

while(length($out) < 15) {
    my $a = get_strong(1);
    $a =~ s/[^[:graph:]]//g;
    $out .= $a;
}
print $out;

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 macOS, while on Windows it uses CryptGenRandom.[13][14] Here is a simple Python script that demonstrates the use of this class:

#!/usr/bin/env python3
import random, string
myrg = random.SystemRandom()
length = 10
alphabet = string.ascii_letters + string.digits # a-z A-Z 0-9
password = "".join(myrg.choice(alphabet) for _ in range(length))
print(password)

PHP

A PHP program can open and read from /dev/urandom, if available, or invoke the Microsoft utilities.[15] A third option, if OpenSSL is available is to employ the function openssl_random_pseudo_bytes'.'[16]

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 fractionation table below.[17] 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'.

123456
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

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. Such policies typically reduce strength slightly below the formula that follows, because symbols are no longer independently produced.

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.[18][19]

Entropy per symbol for different symbol sets
Symbol setSymbol count NEntropy per symbol H
Arabic numerals (0–9) (e.g. PIN)103.32 bits
Hexadecimal numerals (0–9, A–F) (e.g. WEP key)164.00 bits
Case insensitive Latin alphabet (a–z or A–Z)264.70 bits
Case insensitive alphanumeric (a–z or A–Z, 0–9)365.17 bits
Case sensitive Latin alphabet (a–z, A–Z)525.70 bits
Case sensitive alphanumeric (a–z, A–Z, 0–9)625.95 bits
All ASCII printable characters[lower-alpha 1]946.55 bits
Diceware word list777612.9 bits
Lengths L of truly randomly generated passwords required to achieve desired a password entropy H for symbol sets containing N symbols.
Desired password entropy HArabic numeralsHexadecimalCase insensitive Latin alphabetCase insensitive alphanumericCase sensitive Latin alphabetCase sensitive alphanumericAll ASCII printable characters[lower-alpha 1]All extended ASCII printable charactersDiceware word list
32 bits1087766553
40 bits13109887764
64 bits2016141312111095
80 bits25201816151413117
96 bits29242119171715138
128 bits393228252322201710
160 bits494035312927252113
192 bits584841383433302515
224 bits685648444038352918
256 bits786455504543403320
384 bits1169682756865595030
512 bits1551281091009086796640
1024 bits30925621819918017215713280
  1. Excluding the "space" character.

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.

See also

References

  1. Ghazvininejad, Marjan; Knight, Kevin (May–June 2015). "How to Memorize a Random 60-Bit String" (PDF). Proceedings of the 2015 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies. Vol. Proceedings of the 2015 Conference of the North American Chapter of the Association for Computational Linguistics: Human Language Technologies. Denver, Colorado: Association for Computational Linguistics. pp. 1569–1575. doi:10.3115/v1/N15-1180. S2CID 8028691.
  2. "RAND_MAX". docs.microsoft.com.
  3. "Visual Studio 2005 Retired documentation". Microsoft Download Center.
  4. "PHP: microtime – Manual". php.net.
  5. "PHP: rand – Manual". php.net.
  6. "Archived copy". Archived from the original on 2008-10-19. Retrieved 2008-10-17.{{cite web}}: CS1 maint: archived copy as title (link)
  7. "Linux / UNIX: Generating Random Password With mkpasswd / makepasswd / pwgen". cyberciti.biz. November 2007.
  8. "StrongPasswords – Community Help Wiki". help.ubuntu.com. Retrieved 2016-03-25.
  9. NIST. Automated Password Generator standard FIPS 181
  10. Shay, Richard; Kelley, Patrick Gage; Komanduri, Saranga; Mazurek, Michelle L.; Ur, Blase; Vidas, Timothy; Bauer, Lujo; Christin, Nicolas; Cranor, Lorrie Faith (2012). Correct horse battery staple: Exploring the usability of system-assigned passphrases (PDF). SOUPS '12 Proceedings of the Eighth Symposium on Usable Privacy and Security. doi:10.1145/2335356.2335366.
  11. Ganesan, Ravi; Davies, Chris (1994). "A New Attack on Random Pronounceable Password Generators" (PDF). Proceedings of the 17th {NIST}-{NCSC} National Computer Security Conference. NIST: 184–197. Retrieved 2014-12-17.
  12. "Rate my random password generator!". Crypto.
  13. "9.6. random — Generate pseudo-random numbers — Python 3.5.1 documentation". docs.python.org. Retrieved 2016-03-25.
  14. "16.1. os — Miscellaneous operating system interfaces — Python 3.5.1 documentation". docs.python.org. Retrieved 2016-03-25.
  15. ""Secure" Random Numbers in PHP". What the Daily WTF?. April 18, 2010.
  16. "PHP: openssl_random_pseudo_bytes – Manual". php.net. Retrieved 2016-03-25.
  17. Levine, John R., Ed.: Internet Secrets, Second edition, page 831 ff. John Wiley and Sons.
  18. Schneier, B: Applied Cryptography, Second edition, page 233 ff. John Wiley and Sons.
  19. Burr, W. E.; Dodson, D. F.; Polk, W. T. (2006). "Electronic Authentication Guideline" (PDF). NIST. doi:10.6028/NIST.SP.800-63v1.0.2. {{cite journal}}: Cite journal requires |journal= (help)



This article is issued from Wikipedia. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.