YII2預設的密碼加密方式是什嗎?怎麼改成BCrypt加密?

來源:互聯網
上載者:User
因為Laravel5的密碼預設加密方式是bcrypt,希望讓YII2也使用這種加密方式,怎麼做?Google都難找到資料。

回複內容:

因為Laravel5的密碼預設加密方式是bcrypt,希望讓YII2也使用這種加密方式,怎麼做?Google都難找到資料。

public function generatePasswordHash($password, $cost = null){    if ($cost === null) {        $cost = $this->passwordHashCost;    }    if (function_exists('password_hash')) {        /** @noinspection PhpUndefinedConstantInspection */        return password_hash($password, PASSWORD_DEFAULT, ['cost' => $cost]);    }    $salt = $this->generateSalt($cost);    $hash = crypt($password, $salt);    // strlen() is safe since crypt() returns only ascii    if (!is_string($hash) || strlen($hash) !== 60) {        throw new Exception('Unknown error occurred while generating hash.');    }    return $hash;}
public function validatePassword($password, $hash){    if (!is_string($password) || $password === '') {        throw new InvalidParamException('Password must be a string and cannot be empty.');    }    if (!preg_match('/^\$2[axy]\$(\d\d)\$[\.\/0-9A-Za-z]{22}/', $hash, $matches)        || $matches[1] < 4        || $matches[1] > 30    ) {        throw new InvalidParamException('Hash is invalid.');    }    if (function_exists('password_verify')) {        return password_verify($password, $hash);    }    $test = crypt($password, $hash);    $n = strlen($test);    if ($n !== 60) {        return false;    }    return $this->compareString($test, $hash);}

 /**     * @var string strategy, which should be used to generate password hash.     * Available strategies:     * - 'password_hash' - use of PHP `password_hash()` function with PASSWORD_DEFAULT algorithm.     *   This option is recommended, but it requires PHP version >= 5.5.0     * - 'crypt' - use PHP `crypt()` function.     * @deprecated Since version 2.0.7, [[generatePasswordHash()]] ignores [[passwordHashStrategy]] and     * uses `password_hash()` when available or `crypt()` when not.     */
  也就說在2.0.7之後就預設會使用`password_hash`,如果不存在此方法會使用`crypt`
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.