/** * create * * @param string $password * * @return string */ public function create($password) { $salt = $this->salt ?: str_replace('+', '.', base64_encode(CryptHelper::genRandomBytes(64))); switch ($this->type) { case static::MD5: $salt = '$1$' . $salt . '$'; break; case static::SHA256: $cost = CryptHelper::limitInteger($this->cost, 1000); $salt = '$5$rounds=' . $cost . '$' . $salt . '$'; break; case static::SHA512: $cost = CryptHelper::limitInteger($this->cost, 1000); $salt = '$6$rounds=' . $cost . '$' . $salt . '$'; break; default: case static::BLOWFISH: $salt = CryptHelper::repeatToLength($salt, 22, true); $cost = CryptHelper::limitInteger($this->cost, 4, 31); if (function_exists('password_hash')) { $options = array('cost' => $cost, 'salt' => $salt); return password_hash($password, PASSWORD_BCRYPT, $options); } $prefix = version_compare(PHP_VERSION, '5.3.7') >= 0 ? '$2y$' : '$2a$'; $salt = $prefix . $cost . '$' . $salt . '$'; break; } if (!function_exists('crypt')) { throw new \RangeException("crypt() must be loaded for Password::create method"); } return crypt($password, $salt); }
/** * create * * @param string $password * * @return string */ public function create($password) { $salt = $this->salt ?: str_replace('+', '.', base64_encode(CryptHelper::genRandomBytes(64))); switch ($this->type) { case static::MD5: $salt = '$1$' . $salt . '$'; break; case static::SHA256: $cost = CryptHelper::limitInteger($this->cost, 1000); $salt = '$5$rounds=' . $cost . '$' . $salt . '$'; break; case static::SHA512: $cost = CryptHelper::limitInteger($this->cost, 1000); $salt = '$6$rounds=' . $cost . '$' . $salt . '$'; break; default: case static::BLOWFISH: $prefix = version_compare(PHP_VERSION, '5.3.7') >= 0 ? '$2y$' : '$2a$'; $salt = CryptHelper::repeatToLength($salt, 21); $salt = $prefix . CryptHelper::limitInteger($this->cost, 4, 31) . '$' . $salt . '$'; break; } return crypt($password, $salt); }