globe: Zufällige Passwörter generieren mit md5?

Beitrag lesen

n'abend,

Vielen Dank für die Bereicherung der Scriptsammlung :-)

Vier PHP-Doku-Seiten und 10 Min. Tipperei bedürfen keines Dankes.

Das mit der static-Variable für das Array finde ich gut.
Auf diese Idee war ich noch gar nicht gekommen.

Vorweg: Es macht niemals Sinn unveränderlichen Inhalt andauernd neu zu berechnen. Einmal eine (so genannte) Look-Up-Table bauen und dann die vorteile einer Hash-Map nutzen...

Die static Variable ist eigentlich nur ein Behelfsmittel. Wir leben im Jahr 2009, können OOP und sollten es eigentlich besser hinbekommen. Um das mal kurz zu demonstrieren, habe ich weitere 15 Minuten aufgebracht.

RandomStringGenerator::generator() took 0.263427019119 seconds and generated 10000 unique strings in 10000 runs
UniqueRandomStringGenerator::generator() took 0.33697104454 seconds and generated 10000 unique strings in 10000 runs
randomString() took 0.812494039536 seconds and generated 10000 unique strings in 10000 runs
get_random_str() took 0.871380090714 seconds and generated 10000 unique strings in 10000 runs

<?php  
  
/**  
 * Random String Generation  
 * @package examples  
 * @author Rodney Rehm  
 */  
class RandomStringGenerator  
{  
  /**  
   * language for random string  
   * @var array  
   */  
  protected $characters = array();  
  
  /**  
   * Create a new RandomStringGenerator instance  
   * @param boolean $alphanumeric add alphanumeric characters to base  
   * @return void  
   * @uses addAlphanumericCharacters() if $alphanumeric was set to true  
   */  
  public function __construct( $alphanumeric=true )  
  {  
    if( $alphanumeric )  
      $this->addAlphanumericCharacters();  
  }  
  
  /**  
   * add alphanumeric characters ( a-z, A-Z, 0-9 ) to base  
   * @return void  
   * @uses addCharacters() to merge new characters into base  
   */  
  public function addAlphanumericCharacters()  
  {  
    $characters = array_merge(  
      range( 'a', 'z' ),  
      range( 'A', 'Z' ),  
      range( '0', '9' )  
    );  
  
    $this->addCharacters( $characters );  
  }  
  
  /**  
   * Add characters to generator's character base.  
   * Merges an array into the character base.  
   * Adds a single character to the base.  
   * Splits a string into single characters and adds them to the base  
   * @param string|array $characters characters to add  
   * @return void  
   * @uses updateCharacters() to run checks on the character base  
   */  
  public function addCharacters( $characters )  
  {  
    switch( true )  
    {  
      // arrays can simply be merged into the base  
      case is_array( $characters ):  
        $this->characters = array_merge( $this->characters, $characters );  
      break;  
  
      case is_string( $characters ):  
        // simply add a single character to the base  
        if( strlen( $characters ) === 1 ) // strlen is not multibyte safe  
          $this->characters[] = $characters;  
  
        else  
        {  
          // split string into array of characters  
          $characters = preg_split('//', $characters, -1, PREG_SPLIT_NO_EMPTY);  
          $this->addCharacters( $characters );  
          return;  
        }  
      break;  
    }  
  
    // ensure the character base satisfies the requirements  
    $this->updateCharacters();  
  }  
  
  /**  
   * ensure the character base satisfies the requirements,  
   * like each character occuring only once.  
   * @return void  
   */  
  protected function updateCharacters()  
  {  
    $this->characters = array_unique( $this->characters );  
  }  
  
  /**  
   * Generate a random string  
   *  
   * @param integer $min minimum length of generated string  
   * @param integer $max maximum length of string, defaults to $min if omitted  
   * @return string random string  
   */  
  public function generate( $min, $max=null )  
  {  
    // ermittle länge des zufallstrings  
    $length = is_numeric($max) && $min != $max ? mt_rand( $min, $max ) : $min;  
  
    // ermittle zufällige indexe aus dem character-array  
    $indexes = array_rand( $this->characters, $length );  
  
    // ziehe zeichen zu jeweiligem index aus dem character-array  
    $t = array();  
    foreach( $indexes as $i )  
      $t[] = $this->characters[ $i ];  
  
    return join( $t );  
  }  
}  
  
/**  
 * Unique Random String Generation  
 * @package examples  
 * @author Rodney Rehm  
 */  
class UniqueRandomStringGenerator extends RandomStringGenerator  
{  
  /**  
   * list of already generated strings  
   * @var array  
   */  
  protected $previous = array();  
  
  /**  
   * Generate a random string  
   *  
   * @param integer $min minimum length of generated string  
   * @param integer $max maximum length of string, defaults to $min if omitted  
   * @return string random string  
   * @uses $previous to remember generated strings and check for duplicates  
   */  
  public function generate( $min, $max=null )  
  {  
    // generate random string  
    $t = parent::generate( $min, $max );  
  
    // check if generated string is unqiue  
    if( array_key_exists( $t, $this->previous ) )  
      return $this->generate( $min, $max );  
  
    $this->previous[ $t ] = 1;  
  
    return $t;  
  }  
}  
  
echo '<pre>';  
  
$runs = 10000;  
  
$start = microtime(true);  
  
$generator = new RandomStringGenerator();  
$generator->addCharacters( 'ä' );  
$generator->addCharacters( array( 'ö','ü','ß','Ä','Ö','Ü','-','+','_','&' ) );  
$generator->addCharacters( '~.,!' );  
  
$t = array();  
for( $i=0; $i < $runs; $i++ )  
	$t[] = $generator->generate( 10 );  
  
$end = microtime(true);  
echo 'RandomStringGenerator::generator() took ', $end - $start, ' seconds and generated ', count( array_unique( $t ) ), ' unique strings in ', $runs, ' runs', "\n";  
  
$start = microtime(true);  
  
$generator = new UniqueRandomStringGenerator();  
$generator->addCharacters( 'ä' );  
$generator->addCharacters( array( 'ö','ü','ß','Ä','Ö','Ü','-','+','_','&' ) );  
$generator->addCharacters( '~.,!' );  
  
$t = array();  
for( $i=0; $i < $runs; $i++ )  
	$t[] = $generator->generate( 10 );  
  
$end = microtime(true);  
echo 'UniqueRandomStringGenerator::generator() took ', $end - $start, ' seconds and generated ', count( array_unique( $t ) ), ' unique strings in ', $runs, ' runs', "\n";  
  
echo '</pre>';  
  
?>  

weiterhin schönen abend...

--
#selfhtml hat ein Forum?
sh:( fo:# ch:# rl:| br:> n4:& ie:{ mo:} va:) de:] zu:} fl:( ss:? ls:[ js:|