Bcrypt::hash PHP Method

hash() public method

Generate bcrypt hash of string
public hash ( $pw, $salt = NULL, $cost = self::COST ) : string | FALSE
$pw string
$salt string
$cost int
return string | FALSE
    function hash($pw, $salt = NULL, $cost = self::COST)
    {
        if ($cost < 4 || $cost > 31) {
            user_error(self::E_CostArg, E_USER_ERROR);
        }
        $len = 22;
        if ($salt) {
            if (!preg_match('/^[[:alnum:]\\.\\/]{' . $len . ',}$/', $salt)) {
                user_error(self::E_SaltArg, E_USER_ERROR);
            }
        } else {
            $raw = 16;
            $iv = '';
            if (extension_loaded('mcrypt')) {
                $iv = mcrypt_create_iv($raw, MCRYPT_DEV_URANDOM);
            }
            if (!$iv && extension_loaded('openssl')) {
                $iv = openssl_random_pseudo_bytes($raw);
            }
            if (!$iv) {
                for ($i = 0; $i < $raw; $i++) {
                    $iv .= chr(mt_rand(0, 255));
                }
            }
            $salt = str_replace('+', '.', base64_encode($iv));
        }
        $salt = substr($salt, 0, $len);
        $hash = crypt($pw, sprintf('$2y$%02d$', $cost) . $salt);
        return strlen($hash) > 13 ? $hash : FALSE;
    }

Usage Example

Example #1
0
 public function index()
 {
     $classe = strtolower(__CLASS__);
     $function = strtolower(__FUNCTION__);
     $data['classe'] = $classe;
     $data['function'] = $function;
     $data['action'] = base_url() . $classe . '/' . $function;
     $this->form_validation->set_rules($this->validate);
     $this->form_validation->set_message('required', 'O campo "{field}" é obrigatório');
     $this->form_validation->set_message('valid_email', 'O campo {"field}" deve ser um E-mail válido');
     $this->form_validation->set_message('is_unique', '"{field}" inválido');
     $this->form_validation->set_message('max_length', 'O campo "{field}" não pode exceder o tamanho de "{param}" caracteres');
     $this->form_validation->set_message('integer', 'O campo "{field}" deve ser um número');
     if ($this->form_validation->run()) {
         $post = $this->_post();
         $post['password'] = Bcrypt::hash($post['password']);
         $post['date_create'] = date('Y-m-d');
         $id = $this->users_model->insert($post);
         $data['info']['error'] = $id ? 0 : 1;
         $data['info']['message'] = $id ? 'Dados salvos com sucesso.' : 'Ocorreu um erro ao salvar os dados. Por favor tente novamente mais tarde.';
         $this->layout->set_title('Faz, Que Falta - Cadastro')->set_keywords('Faz, Que Falta - Cadastro')->set_description('Faça o seu cadastro na plataforma do Faz, Que Falta e veja a diferença no seu bairro.')->set_view('site/register/index', $data);
     } else {
         $this->layout->set_title('Faz, Que Falta - Cadastro')->set_keywords('Faz, Que Falta - Cadastro')->set_description('Faça o seu cadastro na plataforma do Faz, Que Falta e veja a diferença no seu bairro.')->set_includes('js/mask/jquery.mask.js')->set_includes('js/register.js')->set_view('site/register/index', $data);
     }
 }
All Usage Examples Of Bcrypt::hash