2015-01-01 2 views
2

Codeigniter에 PHPass을 사용하여 암호를 해시하기 만하면됩니다. phpass 웹 사이트에서 zip 파일을 다운로드하고 내용을 추출한 다음 PasswordHash.php 파일을 내 라이브러리 폴더에 복사했습니다. Codeigniter에서 암호를 해시하기 위해 PHPass를 사용

는 그럼 난 내 컨트롤러에서 해당 라이브러리를로드와 암호를 해시 시도했지만 오류

다음했다
Missing argument 1 for PasswordHash::PasswordHash(), called in ... 
Missing argument 2 for PasswordHash::PasswordHash(), called in ... 
Undefined variable: iteration_count_log2 ... 
Undefined variable: portable_hashes ... 

실수 내용은 아래 내 컨트롤러 코드를 확인하고 제발 도와주세요 :

$this->load->library('PasswordHash'); 
$password = $this->input->post('password'); 
$hash = $this->passwordhash->HashPassword($password); 
+0

는 당신이 필요로하는 정보의 유형을 지정하십시오 더 많은 정보 –

+0

을 제공하십시오. 나는 내가 사용하는 코드와 프레임 워크와 내가 얻은 오류를 제공했다. – EducateYourself

+0

왜 암호를 해시로 변환하는 대신 md5() 함수를 사용하지 않습니까? –

답변

8

을 여기 내가 어떻게하는지. 먼저 도우미로 만드십시오.

/* 
|-------------------------------------------------------------------------- 
| Portable PHP password hashing framework 
|-------------------------------------------------------------------------- 
| 
| http://www.openwall.com/phpass/ 
| 
*/ 
define('PHPASS_HASH_STRENGTH', 8); 
define('PHPASS_HASH_PORTABLE', FALSE); 

당신에게 다음 코드를 추가 constants.php 설정에서

$this->load->helper('phpass'); 
    $hasher = new PasswordHash(PHPASS_HASH_STRENGTH, PHPASS_HASH_PORTABLE); 
    $hash_password = $hasher->HashPassword($password); 

/:

<?php (defined('BASEPATH')) OR exit('No direct script access allowed'); 
# 
# Portable PHP password hashing framework. 
# 
# Version 0.3/genuine. 
# 
# Written by Solar Designer <solar at openwall.com> in 2004-2006 and placed in 
# the public domain. Revised in subsequent years, still public domain. 
# 
# There's absolutely no warranty. 
# 
# The homepage URL for this framework is: 
# 
# http://www.openwall.com/phpass/ 
# 
# Please be sure to update the Version line if you edit this file in any way. 
# It is suggested that you leave the main version number intact, but indicate 
# your project name (after the slash) and add your own revision information. 
# 
# Please do not change the "private" password hashing method implemented in 
# here, thereby making your hashes incompatible. However, if you must, please 
# change the hash type identifier (the "$P$") to something different. 
# 
# Obviously, since this code is in the public domain, the above are not 
# requirements (there can be none), but merely suggestions. 
# 
class PasswordHash { 
    var $itoa64; 
    var $iteration_count_log2; 
    var $portable_hashes; 
    var $random_state; 

    function PasswordHash($iteration_count_log2, $portable_hashes) 
    { 
     $this->itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; 

     if ($iteration_count_log2 < 4 || $iteration_count_log2 > 31) 
      $iteration_count_log2 = 8; 
     $this->iteration_count_log2 = $iteration_count_log2; 

     $this->portable_hashes = $portable_hashes; 

     $this->random_state = microtime(); 
     if (function_exists('getmypid')) 
      $this->random_state .= getmypid(); 
    } 

    function get_random_bytes($count) 
    { 
     $output = ''; 
     if (is_readable('/dev/urandom') && 
      ($fh = @fopen('/dev/urandom', 'rb'))) { 
      $output = fread($fh, $count); 
      fclose($fh); 
     } 

     if (strlen($output) < $count) { 
      $output = ''; 
      for ($i = 0; $i < $count; $i += 16) { 
       $this->random_state = 
        md5(microtime() . $this->random_state); 
       $output .= 
        pack('H*', md5($this->random_state)); 
      } 
      $output = substr($output, 0, $count); 
     } 

     return $output; 
    } 

    function encode64($input, $count) 
    { 
     $output = ''; 
     $i = 0; 
     do { 
      $value = ord($input[$i++]); 
      $output .= $this->itoa64[$value & 0x3f]; 
      if ($i < $count) 
       $value |= ord($input[$i]) << 8; 
      $output .= $this->itoa64[($value >> 6) & 0x3f]; 
      if ($i++ >= $count) 
       break; 
      if ($i < $count) 
       $value |= ord($input[$i]) << 16; 
      $output .= $this->itoa64[($value >> 12) & 0x3f]; 
      if ($i++ >= $count) 
       break; 
      $output .= $this->itoa64[($value >> 18) & 0x3f]; 
     } while ($i < $count); 

     return $output; 
    } 

    function gensalt_private($input) 
    { 
     $output = '$P$'; 
     $output .= $this->itoa64[min($this->iteration_count_log2 + 
      ((PHP_VERSION >= '5') ? 5 : 3), 30)]; 
     $output .= $this->encode64($input, 6); 

     return $output; 
    } 

    function crypt_private($password, $setting) 
    { 
     $output = '*0'; 
     if (substr($setting, 0, 2) == $output) 
      $output = '*1'; 

     $id = substr($setting, 0, 3); 
     # We use "$P$", phpBB3 uses "$H$" for the same thing 
     if ($id != '$P$' && $id != '$H$') 
      return $output; 

     $count_log2 = strpos($this->itoa64, $setting[3]); 
     if ($count_log2 < 7 || $count_log2 > 30) 
      return $output; 

     $count = 1 << $count_log2; 

     $salt = substr($setting, 4, 8); 
     if (strlen($salt) != 8) 
      return $output; 

     # We're kind of forced to use MD5 here since it's the only 
     # cryptographic primitive available in all versions of PHP 
     # currently in use. To implement our own low-level crypto 
     # in PHP would result in much worse performance and 
     # consequently in lower iteration counts and hashes that are 
     # quicker to crack (by non-PHP code). 
     if (PHP_VERSION >= '5') { 
      $hash = md5($salt . $password, TRUE); 
      do { 
       $hash = md5($hash . $password, TRUE); 
      } while (--$count); 
     } else { 
      $hash = pack('H*', md5($salt . $password)); 
      do { 
       $hash = pack('H*', md5($hash . $password)); 
      } while (--$count); 
     } 

     $output = substr($setting, 0, 12); 
     $output .= $this->encode64($hash, 16); 

     return $output; 
    } 

    function gensalt_extended($input) 
    { 
     $count_log2 = min($this->iteration_count_log2 + 8, 24); 
     # This should be odd to not reveal weak DES keys, and the 
     # maximum valid value is (2**24 - 1) which is odd anyway. 
     $count = (1 << $count_log2) - 1; 

     $output = '_'; 
     $output .= $this->itoa64[$count & 0x3f]; 
     $output .= $this->itoa64[($count >> 6) & 0x3f]; 
     $output .= $this->itoa64[($count >> 12) & 0x3f]; 
     $output .= $this->itoa64[($count >> 18) & 0x3f]; 

     $output .= $this->encode64($input, 3); 

     return $output; 
    } 

    function gensalt_blowfish($input) 
    { 
     # This one needs to use a different order of characters and a 
     # different encoding scheme from the one in encode64() above. 
     # We care because the last character in our encoded string will 
     # only represent 2 bits. While two known implementations of 
     # bcrypt will happily accept and correct a salt string which 
     # has the 4 unused bits set to non-zero, we do not want to take 
     # chances and we also do not want to waste an additional byte 
     # of entropy. 
     $itoa64 = './ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; 

     $output = '$2a$'; 
     $output .= chr(ord('0') + $this->iteration_count_log2/10); 
     $output .= chr(ord('0') + $this->iteration_count_log2 % 10); 
     $output .= '$'; 

     $i = 0; 
     do { 
      $c1 = ord($input[$i++]); 
      $output .= $itoa64[$c1 >> 2]; 
      $c1 = ($c1 & 0x03) << 4; 
      if ($i >= 16) { 
       $output .= $itoa64[$c1]; 
       break; 
      } 

      $c2 = ord($input[$i++]); 
      $c1 |= $c2 >> 4; 
      $output .= $itoa64[$c1]; 
      $c1 = ($c2 & 0x0f) << 2; 

      $c2 = ord($input[$i++]); 
      $c1 |= $c2 >> 6; 
      $output .= $itoa64[$c1]; 
      $output .= $itoa64[$c2 & 0x3f]; 
     } while (1); 

     return $output; 
    } 

    function HashPassword($password) 
    { 
     $random = ''; 

     if (CRYPT_BLOWFISH == 1 && !$this->portable_hashes) { 
      $random = $this->get_random_bytes(16); 
      $hash = 
       crypt($password, $this->gensalt_blowfish($random)); 
      if (strlen($hash) == 60) 
       return $hash; 
     } 

     if (CRYPT_EXT_DES == 1 && !$this->portable_hashes) { 
      if (strlen($random) < 3) 
       $random = $this->get_random_bytes(3); 
      $hash = 
       crypt($password, $this->gensalt_extended($random)); 
      if (strlen($hash) == 20) 
       return $hash; 
     } 

     if (strlen($random) < 6) 
      $random = $this->get_random_bytes(6); 
     $hash = 
      $this->crypt_private($password, 
      $this->gensalt_private($random)); 
     if (strlen($hash) == 34) 
      return $hash; 

     # Returning '*' on error is safe here, but would _not_ be safe 
     # in a crypt(3)-like function used _both_ for generating new 
     # hashes and for validating passwords against existing hashes. 
     return '*'; 
    } 

    function CheckPassword($password, $stored_hash) 
    { 
     $hash = $this->crypt_private($password, $stored_hash); 
     if ($hash[0] == '*') 
      $hash = crypt($password, $stored_hash); 

     return $hash == $stored_hash; 
    } 
} 

/* End of file phpass_helper.php */ 
/* Location: ./application/helpers/phpass_helper.php */ 

호출, 인스턴스화, 헬퍼를로드, HashPassword 기능을 phpass 도우미를 사용하는 인수 1과 2가 누락되었다는 오류가 나타나는 이유를 이제 이해해야합니다. PasswordHash 클래스를 인스턴스화 할 때 두 개의 인수를 제공해야합니다. phpspass를 CodeIgniter 라이브러리 파일로 만들 수도 있습니다. PasswordHash 함수를 __constructor로 변경하고 라이브러리를 호출 할 때 두 개의 인수를 제공하십시오. 나는 그걸 알아 내도록 너를 떠날거야.

+0

고맙습니다 :) 제가 아는 한, phpass는 소금을 사용하지만 데이터베이스에 소금을 저장할 필요가 없습니다. 내가 필요한 것은 암호 해시 암호입니다. 예, 해시 된 암호를 저장하는 데 사용해야하는 길이 varchar를 알려주십시오. – EducateYourself

+0

나는 또한 동일한 단어의 해시 된 버전이 매번 다를 수 있다는 이상한 사실을 발견했다. 로그인 인증을 어떻게 구성 할 수 있습니까? – EducateYourself

+0

다음 줄을 사용하여 유효성 검사를 수행해야합니다. $ hasher-> CheckPassword ($ password, $ hash) – EducateYourself

3

PHP 5.5 이상을 실행하는 경우 PHP의 기본 제공 password hashing functions을 사용하는 것이 좋습니다. IMHO를 사용하면 제 3 자 /자가 작성자가 필요하지 않습니다.

추가 알고리즘으로 특정 알고리즘을 지정할 때나 PHP가 기본 알고리즘을 변경할 때 쉽게 해싱 알고리즘을 전환 할 수 있습니다. PHP의 기본값 (random salted, 알고리즘 비용이 10 인 CRYPT_BLOWFISH)을 사용한다고 가정하면 다음 코드를 통해 어떻게 작동하는지 알 수 있습니다.

if (password_verify($userInput, $storedHash)) { 
    // The password provided by the user in $userInput 
    // matches the hash we stored (in a database) $storedHash 

    if(password_needs_rehash($storedHash, PASSWORD_DEFAULT)) { 
     $newHash = password_hash($userInput, PASSWORD_DEFAULT); 

     // store new hash in database here 
    } 

    // your what-to-do-on-login-code here 

} else { 

    // your what-to-do-on-login-failure-code here 

} 
:

password_hash($newPassword, PASSWORD_DEFAULT); 

의 결과를 사용하여 암호를 검사 할 때 암호과 같이 재탕해야하는 경우, 즉시 확인 당신이 데이터베이스에 저장을위한 해시를 만들려면

좋고 깨끗합니다.

+0

답변 해 주셔서 감사합니다. 내가 5.5 또는 더 높은 PHP를 사용하기 시작할 것입니다 귀하의 답변을 사용합니다. 사실, 나는 codeigniter를 사용하며, 아직 PHP 5.5를 지원하지 않는다는 것을 알고 있습니다. – EducateYourself

+0

@EducateYourself 낮은 PHP 버전의 경우 호환성 라이브러리 사용 https://github.com/ircmaxell/password_compat – PeeHaa

0

우리는 당신의 CodeIgniter의 프로젝트에 PHPass 통합을 시작하기 전에,이 링크에서 PHPass를 다운로드 : 응용 프로그램/설정/autoload.php에 http://carlofontanos.com/wp-content/uploads/2014/12/phppass.rar

이동 코드를 찾아서 :

$autoload['libraries'] = array(); 

것은 다음 추가를 phpass 라이브러리를 사용하여 CodeIgniter에 액세스 할 때마다 자동으로로드됩니다.

$autoload['libraries'] = array('phppass/passwordhash'); 

당신은 다음과 같은 모델 또는 컨트롤러에있는 라이브러리를 구성 할 수 있습니다 자동로드 PHPass하지 않으려면 :

$this->load->library('phppass/passwordhash'); 

PasswordHash 클래스는 암호에 소금을 추가하고 8 개 패스와 함께 해시 MD5. MD5는 모든 플랫폼에서 지원되므로 기본적으로 사용됩니다.

암호를 생성하려면이 링크를 계속 읽고, 암호를 비교 : http://carlofontanos.com/using-phpass-library-in-codeigniter/