묻고답하기

몇시간 해보다 안되서 여기 남깁니다. password 암호화 된 부분에 대해 좀 풀고 싶습니다.

스카이프 아이디 <brownkoo> 입니다. 방법좀 알려주시는분 사례드릴게요...ㅜ..ㅜ 스트레스 하도받아서 일케 여기 올립니다. 대체 어딜 수정해야될지 모르겠네요.스카이프 연락주셔요~


class Password
{
 /**
  * @brief Return the list of hashing algorithms supported by this server
  * @return array
  */
 public function getSupportedAlgorithms()
 {
  $retval = array();
  if(function_exists('hash_hmac') && in_array('sha256', hash_algos()))
  {
   $retval['pbkdf2'] = 'pbkdf2';
  }
  if(version_compare(PHP_VERSION, '5.3.7', '>=') && defined('CRYPT_BLOWFISH'))
  {
   $retval['bcrypt'] = 'bcrypt';
  }
  $retval['md5'] = 'md5';
  return $retval;
 }

 /**
  * @brief Return the best hashing algorithm supported by this server
  * @return string
  */
 public function getBestAlgorithm()
 {
  $algos = $this->getSupportedAlgorithms();
  return key($algos);
 }

 /**
  * @brief Return the currently selected hashing algorithm
  * @return string
  */
 public function getCurrentlySelectedAlgorithm()
 {
  if(function_exists('getModel'))
  {
   $config = getModel('member')->getMemberConfig();
   $algorithm = $config->password_hashing_algorithm;
   if(strval($algorithm) === '')
   {
    $algorithm = 'md5';  // Historical default for XE
   }
  }
  else
  {
   $algorithm = 'md5';
  }
  return $algorithm;
 }

 /**
  * @brief Return the currently configured work factor for bcrypt and other adjustable algorithms
  * @return int
  */
 public function getWorkFactor()
 {
  if(function_exists('getModel'))
  {
   $config = getModel('member')->getMemberConfig();
   $work_factor = $config->password_hashing_work_factor;
   if(!$work_factor || $work_factor < 4 || $work_factor > 31)
   {
    $work_factor = 8;  // Reasonable default
   }
  }
  else
  {
   $work_factor = 8;
  }
  return $work_factor;
 }

 /**
  * @brief Create a hash using the specified algorithm
  * @param string $password The password
  * @param string $algorithm The algorithm (optional)
  * @return string
  */
 public function createHash($password, $algorithm = null)
 {
  if($algorithm === null)
  {
   $algorithm = $this->getCurrentlySelectedAlgorithm();
  }
  if(!array_key_exists($algorithm, $this->getSupportedAlgorithms()))
  {
   return false;
  }

  $password = trim($password);

  switch($algorithm)
  {
   case 'md5':
    return md5($password);

   case 'pbkdf2':
    $iterations = pow(2, $this->getWorkFactor() + 5);
    $salt = $this->createSecureSalt(12, 'alnum');
    $hash = base64_encode($this->pbkdf2($password, $salt, 'sha256', $iterations, 24));
    return 'sha256:'.sprintf('%07d', $iterations).':'.$salt.':'.$hash;

   case 'bcrypt':
    return $this->bcrypt($password);

   default:
    return false;
  }
 }

 /**
  * @brief Check if a password matches a hash
  * @param string $password The password
  * @param string $hash The hash
  * @param string $algorithm The algorithm (optional)
  * @return bool
  */
 public function checkPassword($password, $hash, $algorithm = null)
 {
  if($algorithm === null)
  {
   $algorithm = $this->checkAlgorithm($hash);
  }

  $password = trim($password);

  switch($algorithm)
  {
   case 'md5':
    return md5($password) === $hash || md5(sha1(md5($password))) === $hash;

   case 'mysql_old_password':
    return (class_exists('Context') && substr(Context::getDBType(), 0, 5) === 'mysql') ?
     DB::getInstance()->isValidOldPassword($password, $hash) : false;

   case 'mysql_password':
    return $hash[0] === '*' && substr($hash, 1) === strtoupper(sha1(sha1($password, true)));

   case 'pbkdf2':
    $hash = explode(':', $hash);
    $hash[3] = base64_decode($hash[3]);
    $hash_to_compare = $this->pbkdf2($password, $hash[2], $hash[0], intval($hash[1], 10), strlen($hash[3]));
    return $this->strcmpConstantTime($hash_to_compare, $hash[3]);

   case 'bcrypt':
    $hash_to_compare = $this->bcrypt($password, $hash);
    return $this->strcmpConstantTime($hash_to_compare, $hash);

   default:
    return false;
  }
 }

 /**
  * @brief Check the algorithm used to create a hash
  * @param string $hash The hash
  * @return string
  */
 function checkAlgorithm($hash)
 {
  if(preg_match('/^\$2[axy]\$([0-9]{2})\$/', $hash, $matches))
  {
   return 'bcrypt';
  }
  elseif(preg_match('/^sha[0-9]+:([0-9]+):/', $hash, $matches))
  {
   return 'pbkdf2';
  }
  elseif(strlen($hash) === 32 && ctype_xdigit($hash))
  {
   return 'md5';
  }
  elseif(strlen($hash) === 16 && ctype_xdigit($hash))
  {
   return 'mysql_old_password';
  }
  elseif(strlen($hash) === 41 && $hash[0] === '*')
  {
   return 'mysql_password';
  }
  else
  {
   return false;
  }
 }

 /**
  * @brief Check the work factor of a hash
  * @param string $hash The hash
  * @return int
  */
 function checkWorkFactor($hash)
 {
  if(preg_match('/^\$2[axy]\$([0-9]{2})\$/', $hash, $matches))
  {
   return intval($matches[1], 10);
  }
  elseif(preg_match('/^sha[0-9]+:([0-9]+):/', $hash, $matches))
  {
   return max(0, round(log($matches[1], 2)) - 5);
  }
  else
  {
   return false;
  }
 }

 /**
  * @brief Generate a cryptographically secure random string to use as a salt
  * @param int $length The number of bytes to return
  * @param string $format hex or alnum
  * @return string
  */
 public function createSecureSalt($length, $format = 'hex')
 {
  // Find out how many bytes of entropy we really need
  switch($format)
  {
   case 'hex':
    $entropy_required_bytes = ceil($length / 2);
    break;
   case 'alnum':
   case 'printable':
    $entropy_required_bytes = ceil($length * 3 / 4);
    break;
   default:
    $entropy_required_bytes = $length;
  }

  // Cap entropy to 256 bits from any one source, because anything more is meaningless
  $entropy_capped_bytes = min(32, $entropy_required_bytes);

  // Find and use the most secure way to generate a random string
  $is_windows = (defined('PHP_OS') && strtoupper(substr(PHP_OS, 0, 3)) === 'WIN');
  if(function_exists('openssl_random_pseudo_bytes') && (!$is_windows || version_compare(PHP_VERSION, '5.4', '>=')))
  {
   $entropy = openssl_random_pseudo_bytes($entropy_capped_bytes);
  }
  elseif(function_exists('mcrypt_create_iv') && (!$is_windows || version_compare(PHP_VERSION, '5.3.7', '>=')))
  {
   $entropy = mcrypt_create_iv($entropy_capped_bytes, MCRYPT_DEV_URANDOM);
  }
  elseif(function_exists('mcrypt_create_iv') && $is_windows)
  {
   $entropy = mcrypt_create_iv($entropy_capped_bytes, MCRYPT_RAND);
  }
  elseif(!$is_windows && @is_readable('/dev/urandom'))
  {
   $fp = fopen('/dev/urandom', 'rb');
   $entropy = fread($fp, $entropy_capped_bytes);
   fclose($fp);
  }
  else
  {
   $entropy = '';
   for($i = 0; $i < $entropy_capped_bytes; $i += 2)
   {
    $entropy .= pack('S', rand(0, 65536) ^ mt_rand(0, 65535));
   }
  }

  // Mixing (see RFC 4086 section 5)
  $output = '';
  for($i = 0; $i < $entropy_required_bytes; $i += 32)
  {
   $output .= hash('sha256', $entropy . $i . rand(), true);
  }

  // Encode and return the random string
  switch($format)
  {
   case 'hex':
    return substr(bin2hex($output), 0, $length);
   case 'binary':
    return substr($output, 0, $length);
   case 'printable':
    $salt = '';
    for($i = 0; $i < $length; $i++)
    {
     $salt .= chr(33 + (crc32(sha1($i . $output)) % 94));
    }
    return $salt;
   case 'alnum':
   default:
    $salt = substr(base64_encode($output), 0, $length);
    $replacements = chr(rand(65, 90)) . chr(rand(97, 122)) . rand(0, 9);
    return strtr($salt, '+/=', $replacements);
  }
 }

 /**
  * @brief Generate a temporary password using the secure salt generator
  * @param int $length The number of bytes to return
  * @return string
  */
 public function createTemporaryPassword($length = 16)
 {
  while(true)
  {
   $source = base64_encode($this->createSecureSalt(64, 'binary'));
   $source = strtr($source, 'iIoOjl10/', '@#$%&*-!?');
   $source_length = strlen($source);
   for($i = 0; $i < $source_length - $length; $i++)
   {
    $candidate = substr($source, $i, $length);
    if(preg_match('/[a-z]/', $candidate) && preg_match('/[A-Z]/', $candidate) &&
     preg_match('/[0-9]/', $candidate) && preg_match('/[^a-zA-Z0-9]/', $candidate))
    {
     return $candidate;
    }
   }
  }
 }

 /**
  * @brief Generate the PBKDF2 hash of a string using a salt
  * @param string $password The password
  * @param string $salt The salt
  * @param string $algorithm The algorithm (optional, default is sha256)
  * @param int $iterations Iteration count (optional, default is 8192)
  * @param int $length The length of the hash (optional, default is 32)
  * @return string
  */
 public function pbkdf2($password, $salt, $algorithm = 'sha256', $iterations = 8192, $length = 24)
 {
  if(function_exists('hash_pbkdf2'))
  {
   return hash_pbkdf2($algorithm, $password, $salt, $iterations, $length, true);
  }
  else
  {
   $output = '';
   $block_count = ceil($length / strlen(hash($algorithm, '', true)));  // key length divided by the length of one hash
   for($i = 1; $i <= $block_count; $i++)
   {
    $last = $salt . pack('N', $i);  // $i encoded as 4 bytes, big endian
    $last = $xorsum = hash_hmac($algorithm, $last, $password, true);  // first iteration
    for($j = 1; $j < $iterations; $j++)  // The other $count - 1 iterations
    {
     $xorsum ^= ($last = hash_hmac($algorithm, $last, $password, true));
    }
    $output .= $xorsum;
   }
   return substr($output, 0, $length);
  }
 }

 /**
  * @brief Generate the bcrypt hash of a string using a salt
  * @param string $password The password
  * @param string $salt The salt (optional, auto-generated if empty)
  * @return string
  */
 public function bcrypt($password, $salt = null)
 {
  if($salt === null)
  {
   $salt = '$2y$'.sprintf('%02d', $this->getWorkFactor()).'$'.$this->createSecureSalt(22, 'alnum');
  }
  return crypt($password, $salt);
 }

 /**
  * @brief Compare two strings in constant time
  * @param string $a The first string
  * @param string $b The second string
  * @return bool
  */
 function strcmpConstantTime($a, $b)
 {
  $diff = strlen($a) ^ strlen($b);
  $maxlen = min(strlen($a), strlen($b));
  for($i = 0; $i < $maxlen; $i++)
  {
   $diff |= ord($a[$i]) ^ ord($b[$i]);
  }
  return $diff === 0;
 }
}
/* End of file : Password.class.php */
/* Location: ./classes/security/Password.class.php */

글쓴이 제목 최종 글
XE 공지 글 쓰기,삭제 운영방식 변경 공지 [16] 2019.03.05 by 남기남
댑펑 잘 되다가 db 접속 오류가 발생하였습니다. 라고 나오는 경우... [4] 2015.07.17 by 댑펑
와이군1 확장변수 안에 셀에 백그라운드 색상지정이 궁금합니다 file  
poltwo Naver Analystic은 어떻게 쓰나요?  
항순 컨텐츠 확장 위젯... 자동으로 정렬 최신화 안되나요? [2] 2015.07.17 by 항순
항순 if 문 안에 if 문 못쓰나욥..? [6] 2015.07.17 by 항순
리키한 메뉴 클릭시 팝업 창 띄우기 좀 알려주세요  
파란종이 질문드립니다.~~ [1] file 2015.07.17 by yeonghak
inJin 서로 다른 사이트맵을 구분하는 조건문이 궁금합니다 [3] file 2015.07.17 by inJin
JinkPark socialxe 에 로그인 폼 수정파일은 어디에 있나요? [2] file 2015.07.17 by JinkPark
JAYLEEKOREA 쇼핑몰 모듈 질문  
foryx Sketch5 게시판에서 메뉴들 마우스 오버시 이상한 이미지가 겹쳐서 보입니다. [1] file 2015.07.17 by DoorWeb
홈페이지어려워요 폼 질문 [7] 2015.07.17 by 홈페이지어려워요
팝스애플 에디터 컴포넌트 만들 때 쓰이는 transHTML,transContent은 뭔가요?  
김진교369 scanstyle does nothing.... [3] 2015.07.17 by 김진교369
김진교369 404 Oops, Sorry. Page is not found! [3] 2015.07.17 by 김진교369
팝스애플 default/editor.html 파일에 자바스크립트를 추가하려고 합니다. [1] 2015.07.17 by 팝스애플
팝스애플 에디터 컴포넌트 문제입니다... 고수님들의 답변 기다리겠습니다.. [1] 2015.07.17 by 팝스애플
이주연743 호스팅 업체에 짧은주소 허용을 신청하려면 [1] 2015.07.17 by 불금
이주연743 XET OneColor 레이아웃 사용시 default url is null  
초보해외유저 흐름of이미지 위젯 코드 수정 문의 [7] file 2015.07.17 by 반걸
항순 2차 분류(카테고리) 겹침 문제.. 어떻게 해야 할까요.. [2] file 2015.07.17 by 항순
앙마랏 이미지 프로세스 memory_limit 질문드려요. [1] 2015.07.17 by DoorWeb
samsick92 id="wrap" 과 관련된 질문드립니다. 답변 부탁드려요ㅠㅠ [3] file 2015.07.17 by 나다미
아옭옭 ssl 페이지 적용에 관하여 질문이 있습니다. [2] 2015.07.16 by 아옭옭
jun0214 포인트에 따라서 글 열람 되게 안되게 지정하는 방법좀 알려주세요  
rewolf cond 조건문이요 [1] 2015.07.16 by BJ람보
nabill 4일에 걸처 다만들엇는데 ㅠㅠ 관리자 페이지가 안들어가져요 [3] file 2015.07.16 by BJ람보
이쩡2 md5 암호화 해제 관련질문 [3] 2015.07.16 by BJ람보
이쩡2 암호화 관련 질문[사례드릴게요] [1] 2015.07.16 by AJKJ
반걸 비밀댓글 갯수구하는법 [1] 2015.07.16 by 반걸