Skip to content

Instantly share code, notes, and snippets.

@fhferreira
Last active December 17, 2015 17:08
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fhferreira/5643325 to your computer and use it in GitHub Desktop.
Save fhferreira/5643325 to your computer and use it in GitHub Desktop.
Generator for randomic passwords
<?php
class PasswordGenerator{
/**
* @param integer $length
* @return string $pass
* @author Flávio Henrique Ferreira <flaviometalvale@gmail.com>
**/
static function randomPassword($length = 8)
{
$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789#!@$%";
$password = array();
$alphaLength = strlen($alphabet) - 1;
for ($i = 0; $i < $length; $i++)
{
$n = rand(0, $alphaLength);
$password[] = $alphabet[$n];
}
/*
* turn the array into a string
*/
return implode($password);
}
}
/*Usage*/
$pass = PasswordGenerator::randomPassword(24); //24 characters
$pass = PasswordGenerator::randomPassword(); //8 characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment