You are here

function genpass_password in Generate Password 7

Same name and namespace in other branches
  1. 8 genpass.module \genpass_password()
  2. 6 genpass.module \genpass_password()

Generates random password.

Return value

string The random string.

See also

user_password()

File

./genpass.module, line 36

Code

function genpass_password() {
  $pass = '';
  $length = variable_get('genpass_length', 12);
  $allowable_characters = variable_get('genpass_entropy', _GENPASS_REQUIRED_entropy());

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

  // 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;
}