You are here

function _genpass_genpass_substring in Generate Password 7.2

Picks random characters from a given string.

Parameters

$allowable_characters: A set of characters from which to choose.

$length: The desired length of the returned random string.

Return value

string The random string.

See also

user_password().

1 call to _genpass_genpass_substring()
genpass_genpass in ./genpass.module
Generates a pretty, pretty, pretty good password.

File

./genpass.module, line 122
Genpass module: automatically sets strong passwords.

Code

function _genpass_genpass_substring($allowable_characters, $length) {

  // Zero-based count of characters in the allowable list:
  $len = strlen($allowable_characters) - 1;

  // Declare the password as a blank string.
  $pass = '';

  // Loop the number of times specified by $length.
  for ($i = 0; $i < $length; $i++) {
    do {

      // Find a secure random number within the range needed.
      $index = ord(drupal_random_bytes(1));
    } while ($index > $len);

    // Each iteration, pick a random character from the
    // allowable string and append it to the password:
    $pass .= $allowable_characters[$index];
  }
  return $pass;
}