这个随机字符串生成器有什么问题?

时间:2014-02-24 16:48:46

标签: php arrays string random generator

我编辑了一些我在网上发现的代码,用PHP生成一个随机字符串,我将用它作为ID。

<!DOCTYPE html>
<html class="no-js">
<head>
    <meta charset="utf-8">
    <title>ID Generator</title>
    <link rel="stylesheet" href="idgen.css">
</head>
<body>
<?php 

class randomPassword {

    function __construct( $passType='alphaNumeric', $length=64, $rangeLength=9 ) {
        $this->passType = $this->setPassType( $passType );
        $this->setLength( $length );
        $this->setRangeLength( $rangeLength );
    }

    function setRangeLength( $rangeLength ) {
        $this->rangeLength=$rangeLength;
    }

    // set the length of the password
    private function setLength( $length ) {
        $this->length=$length;
    }

    // set the type of password
    private function setPassType( $passType ) {
        return $passType.'Chars';
    }

    // return an array of numbers
    private function numericChars() {
        return range( 0, $this->rangeLength );
    }

    // return an array of lowercase chars
    private function lcAlphaChars() {
        return range( 'a', 'z' );
    }

    //return an array of uppercase chars
    private function ucAlphaChars() {
        return range( 'A', 'Z' );
    }

    // return an array of all alpha chars
    private function alphaChars() {
        return array_merge( $this->lcAlphaChars(), $this->ucAlphaChars() );
    }

    // return an array of alphanumeric chars
    private function alphaNumericChars() {
        return array_merge( $this->alphaChars(), $this->numericChars() );
    }

    // return array of lowercase characters and numbers
    private function lcAlphaNumericChars() {
        return array_merge( $this->lcAlphaChars(), $this->numericChars() );
    }

    // return array of uppercase characters and numbers
    private function ucAlphaNumericChars() {
        return array_merge( $this->ucAlphaChars(), $this->numericChars() );
    }

    // return a string of chars
    private function makeString() {
        // set the function to call based on the password type
        $funcName = $this->passType;
        return implode( $this->$funcName() );
    }

    // shuffle the chars and return $length of chars
    public function makePassword() {
        return substr( str_shuffle( $this->makeString() ), 1, $this->length );
    }

} // end class

?>

<h3>ID Generator</h3>
<h4>LC Alpha Numeric</h4>
<?php
    for ($i=0;$i<5;$i++)
    {
        try
        {
            $obj = new randomPassword( 'lcAlphaNumeric', 32, 9 );
            echo $obj->makePassword().'<br />';
        }
        catch( Exception $ex )
        {
            echo $ex->getMessage();
        }
    }
?>
</body>
</html>

以下是一些示例输出:

9hx2qwuvjbyn18tceosp75gkf30mrli4
tzqujok5xnr783wem6sv2afcgi9h1d0y
87bxc9vr6sewaoltkni24jdgh3z1fu05
j2shgmd4o6w09yx85avp1iflqubtk3nc
0r1q9engl43uv5poxfjd6zi8cha72ytk

如果我再次尝试相同但这次将长度增加到64(以避免碰撞),它只产生一个长度为36的字符串(其最大值为元素 NOT 重复)。我怎样才能使它可以从数组中选择任何东西?

1 个答案:

答案 0 :(得分:4)

您可以使用openssl扩展程序:

$secret = bin2hex(openssl_random_pseudo_bytes($length * 2));
相关问题