ArgonHasher.php 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace Illuminate\Hashing;
  3. use RuntimeException;
  4. use Illuminate\Contracts\Hashing\Hasher as HasherContract;
  5. class ArgonHasher extends AbstractHasher implements HasherContract
  6. {
  7. /**
  8. * The default memory cost factor.
  9. *
  10. * @var int
  11. */
  12. protected $memory = 1024;
  13. /**
  14. * The default time cost factor.
  15. *
  16. * @var int
  17. */
  18. protected $time = 2;
  19. /**
  20. * The default threads factor.
  21. *
  22. * @var int
  23. */
  24. protected $threads = 2;
  25. /**
  26. * Create a new hasher instance.
  27. *
  28. * @param array $options
  29. * @return void
  30. */
  31. public function __construct(array $options = [])
  32. {
  33. $this->time = $options['time'] ?? $this->time;
  34. $this->memory = $options['memory'] ?? $this->memory;
  35. $this->threads = $options['threads'] ?? $this->threads;
  36. }
  37. /**
  38. * Hash the given value.
  39. *
  40. * @param string $value
  41. * @param array $options
  42. * @return string
  43. */
  44. public function make($value, array $options = [])
  45. {
  46. $hash = password_hash($value, PASSWORD_ARGON2I, [
  47. 'memory_cost' => $this->memory($options),
  48. 'time_cost' => $this->time($options),
  49. 'threads' => $this->threads($options),
  50. ]);
  51. if ($hash === false) {
  52. throw new RuntimeException('Argon2 hashing not supported.');
  53. }
  54. return $hash;
  55. }
  56. /**
  57. * Check if the given hash has been hashed using the given options.
  58. *
  59. * @param string $hashedValue
  60. * @param array $options
  61. * @return bool
  62. */
  63. public function needsRehash($hashedValue, array $options = [])
  64. {
  65. return password_needs_rehash($hashedValue, PASSWORD_ARGON2I, [
  66. 'memory_cost' => $this->memory($options),
  67. 'time_cost' => $this->time($options),
  68. 'threads' => $this->threads($options),
  69. ]);
  70. }
  71. /**
  72. * Set the default password memory factor.
  73. *
  74. * @param int $memory
  75. * @return $this
  76. */
  77. public function setMemory(int $memory)
  78. {
  79. $this->memory = $memory;
  80. return $this;
  81. }
  82. /**
  83. * Set the default password timing factor.
  84. *
  85. * @param int $time
  86. * @return $this
  87. */
  88. public function setTime(int $time)
  89. {
  90. $this->time = $time;
  91. return $this;
  92. }
  93. /**
  94. * Set the default password threads factor.
  95. *
  96. * @param int $threads
  97. * @return $this
  98. */
  99. public function setThreads(int $threads)
  100. {
  101. $this->threads = $threads;
  102. return $this;
  103. }
  104. /**
  105. * Extract the memory cost value from the options array.
  106. *
  107. * @param array $options
  108. * @return int
  109. */
  110. protected function memory(array $options)
  111. {
  112. return $options['memory'] ?? $this->memory;
  113. }
  114. /**
  115. * Extract the time cost value from the options array.
  116. *
  117. * @param array $options
  118. * @return int
  119. */
  120. protected function time(array $options)
  121. {
  122. return $options['time'] ?? $this->time;
  123. }
  124. /**
  125. * Extract the threads value from the options array.
  126. *
  127. * @param array $options
  128. * @return int
  129. */
  130. protected function threads(array $options)
  131. {
  132. return $options['threads'] ?? $this->threads;
  133. }
  134. }