YII2 What is the default password encryption method? How to change to Bcrypt encryption?

Source: Internet
Author: User
Tags crypt
Because the default encryption method for the Laravel5 password is bcrypt, want to let YII2 also use this encryption way, how to do? Google is hard to find information.

Reply content:

Because Laravel5 password is the default encryption method bcrypt , I want to let YII2 also use this encryption method, how to do? Google is hard to find information.

  public function Generatepasswordhash ($password, $cost = null) {if ($cost = =    = null) {$cost = $this->passwordhashcost; } if (Function_exists (' Password_hash ')) {/** @noinspection phpundefinedconstantinspection */Return Pass    Word_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)!==) {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`
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.