Logika Password

  • Upload
    wedus

  • View
    214

  • Download
    0

Embed Size (px)

DESCRIPTION

Php

Citation preview

=============== 1 =====================function validate_credentials() {$this->load->library('form_validation');$this->form_validation->set_rules('username', 'Username', 'required|max_length[30]|xss_clean');$this->form_validation->set_rules('password', 'Password', 'required|max_length[30]|xss_clean|callback_login_check');if ($this->form_validation->run($this) == FALSE) {$data = array( 'username' => form_error('username','',''), 'password' => form_error('password','',''), 'hasil' => 'gagal' );echo json_encode($data);} else {$username = strtolower($this->input->post('username'));$this->load->model('mdl_users');$mysql_query = 'SELECT a.* , b.Branch_name, b.sm_idFROM users aLEFT JOIN branches b ON (b.Branch_id = a.Branch_id)WHERE username="'.$username.'"';$query = $this->_custom_query($mysql_query)->first_row();//Ambil Informasi Struktur Organisasi$dataInfo = array ('my_title_organization' => '','my_user_id' => $query->User_id,);$my_organization_tree = $this->get_my_organization_tree($dataInfo);$data = array('username' => $username,'User_id' => $query->User_id,'is_logged_in' => true, 'hasil' => 'sukses', 'User_role_id' => $query->User_role_id, 'is_ajax' => true, 'my_title' => $query->Title_organization_id, 'Branch_id' => $query->Branch_id, 'Branch_name' => $query->Branch_name, 'organization_tree' => $my_organization_tree['strukturAtasan'], 'Title_organization_id' => $query->Title_organization_id, 'sm_id' => $query->sm_id, ); $this->session->set_userdata($data); // Modules::run('template/index');echo json_encode($data);}}=============== 2 =====================function login_check($password) {$username = $this->input->post('username',TRUE);$this->load->model('mdl_users');$result = $this->mdl_users->password_check($username,$password);if ($result == FALSE) {$this->form_validation->set_message('login_check', 'You did not enter a correct username and/or password');return FALSE;} else {return TRUE;}}=============== 3 =====================function password_check($username,$password) {$table = $this->get_table();$this->db->where('username', $username);$query = $this->db->get($table);if($query->num_rows() > 0) {$row = $query->row(1);$hashed_password = $row->Password;} else return FALSE; if (Modules::run('site_security/validate_password',$hashed_password,$password)) {return TRUE;} else return FALSE;}=============== 4 =====================function validate_password ($hashed_password,$password) {$salt = substr($hashed_password, 0, 64);$hash = substr($hashed_password, 64, 64);$password_hash = hash('sha256',$salt.$password);return ($password_hash == $hash);}=============== BUAT PASSWORD =====================function hash_password ($password) {$salt = bin2hex(mcrypt_create_iv(32,MCRYPT_DEV_URANDOM));$hash = hash('sha256',$salt.$password);return $salt.$hash;}