我试图在php中生成一个随机密码。

但是我得到的都是'a'返回类型是数组类型,我希望它是字符串。对如何修改代码有什么想法吗?

谢谢。

function randomPassword() {
    $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
    for ($i = 0; $i < 8; $i++) {
        $n = rand(0, count($alphabet)-1);
        $pass[$i] = $alphabet[$n];
    }
    return $pass;
}

当前回答

Create a file with this code in it. Call it like in the comments. <?php /** * @usage : * include_once($path . '/Password.php'); * $Password = new Password; * $pwd = $Password->createPassword(10); * return $pwd; * */ class Password { public function createPassword($length = 15) { $response = []; $response['pwd'] = $this->generate($length); $response['hashPwd'] = $this->hashPwd( $response['pwd'] ); return $response; } private function generate($length = 15) { $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*(){}/?,><"; return substr(str_shuffle($chars),0,$length); } private function hashPwd($pwd) { return hash('sha256', $pwd); } } ?>

其他回答

TL; diana:

使用random_int()和下面给定的random_str()。 如果没有random_int(),请使用random_compat。

解释:

由于您正在生成密码,因此需要确保所生成的密码是不可预测的,而确保在实现中出现此属性的唯一方法是使用加密安全的伪随机数生成器(CSPRNG)。

对于随机字符串的一般情况,可以放宽对CSPRNG的要求,但在涉及安全性时则不能。

在PHP中,密码生成的简单、安全且正确的答案是使用RandomLib,不要重复工作。这个库已经被行业安全专家和我自己审计过了。

对于喜欢发明自己的解决方案的开发人员,PHP 7.0.0将为此目的提供random_int()。如果你还在使用PHP 5。x,我们为random_int()写了一个PHP 5的polyfill,这样你就可以在PHP 7发布之前使用新的API。使用random_int()填充可能比编写自己的实现更安全。

有了一个安全的随机整数生成器,生成一个安全的随机字符串比饼更容易:

<?php
/**
 * Generate a random string, using a cryptographically secure 
 * pseudorandom number generator (random_int)
 * 
 * For PHP 7, random_int is a PHP core function
 * For PHP 5.x, depends on https://github.com/paragonie/random_compat
 * 
 * @param int $length      How many characters do we want?
 * @param string $keyspace A string of all possible characters
 *                         to select from
 * @return string
 */
function random_str(
    $length,
    $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
) {
    $str = '';
    $max = mb_strlen($keyspace, '8bit') - 1;
    if ($max < 1) {
        throw new Exception('$keyspace must be at least two characters long');
    }
    for ($i = 0; $i < $length; ++$i) {
        $str .= $keyspace[random_int(0, $max)];
    }
    return $str;
}

安全警告:rand()不是一个加密安全的伪随机数生成器。在其他地方寻找在PHP中生成加密安全的伪随机字符串的方法。

试试这个(使用strlen而不是count,因为count在字符串上总是1):

function randomPassword() {
    $alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890';
    $pass = array(); //remember to declare $pass as an array
    $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
    for ($i = 0; $i < 8; $i++) {
        $n = rand(0, $alphaLength);
        $pass[] = $alphabet[$n];
    }
    return implode($pass); //turn the array into a string
}

Demo

另一个(仅限linux)

function randompassword()
{
    $fp = fopen ("/dev/urandom", 'r');
    if (!$fp) { die ("Can't access /dev/urandom to get random data. Aborting."); }
    $random = fread ($fp, 1024); # 1024 bytes should be enough
    fclose ($fp);
    return trim (base64_encode ( md5 ($random, true)), "=");
}

一句话:

substr(str_shuffle('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789') , 0 , 10 )

你需要strlen($alphabet),而不是常量字母的计数(相当于'alphabet')。

然而,rand并不是一个适合于此目的的随机函数。它的输出可以很容易地预测,因为它隐含地以当前时间作为种子。此外,兰特是不加密安全的;因此,从输出中确定其内部状态相对容易。

相反,从/dev/urandom读取以获得加密随机数据。