Bcrypt.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\helper\hash;
  12. class Bcrypt
  13. {
  14. /**
  15. * Default crypt cost factor.
  16. *
  17. * @var int
  18. */
  19. protected $rounds = 10;
  20. public function make($value, array $options = [])
  21. {
  22. $cost = isset($options['rounds']) ? $options['rounds'] : $this->rounds;
  23. $hash = password_hash($value, PASSWORD_BCRYPT, ['cost' => $cost]);
  24. if ($hash === false) {
  25. throw new \RuntimeException('Bcrypt hashing not supported.');
  26. }
  27. return $hash;
  28. }
  29. public function check($value, $hashedValue, array $options = [])
  30. {
  31. if (strlen($hashedValue) === 0) {
  32. return false;
  33. }
  34. return password_verify($value, $hashedValue);
  35. }
  36. public function setRounds($rounds)
  37. {
  38. $this->rounds = (int)$rounds;
  39. return $this;
  40. }
  41. }