password.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Safe;
  3. use Safe\Exceptions\PasswordException;
  4. /**
  5. * password_hash creates a new password hash using a strong one-way hashing
  6. * algorithm. password_hash is compatible with crypt.
  7. * Therefore, password hashes created by crypt can be used with
  8. * password_hash.
  9. *
  10. *
  11. *
  12. *
  13. * PASSWORD_DEFAULT - Use the bcrypt algorithm (default as of PHP 5.5.0).
  14. * Note that this constant is designed to change over time as new and stronger algorithms are added
  15. * to PHP. For that reason, the length of the result from using this identifier can change over
  16. * time. Therefore, it is recommended to store the result in a database column that can expand
  17. * beyond 60 characters (255 characters would be a good choice).
  18. *
  19. *
  20. *
  21. *
  22. * PASSWORD_BCRYPT - Use the CRYPT_BLOWFISH algorithm to
  23. * create the hash. This will produce a standard crypt compatible hash using
  24. * the "$2y$" identifier. The result will always be a 60 character string.
  25. *
  26. *
  27. *
  28. *
  29. * PASSWORD_ARGON2I - Use the Argon2i hashing algorithm to create the hash.
  30. * This algorithm is only available if PHP has been compiled with Argon2 support.
  31. *
  32. *
  33. *
  34. *
  35. * PASSWORD_ARGON2ID - Use the Argon2id hashing algorithm to create the hash.
  36. * This algorithm is only available if PHP has been compiled with Argon2 support.
  37. *
  38. *
  39. *
  40. *
  41. *
  42. *
  43. *
  44. * salt (string) - to manually provide a salt to use when hashing the password.
  45. * Note that this will override and prevent a salt from being automatically generated.
  46. *
  47. *
  48. * If omitted, a random salt will be generated by password_hash for
  49. * each password hashed. This is the intended mode of operation.
  50. *
  51. *
  52. *
  53. * The salt option has been deprecated as of PHP 7.0.0. It is now
  54. * preferred to simply use the salt that is generated by default.
  55. *
  56. *
  57. *
  58. *
  59. *
  60. * cost (integer) - which denotes the algorithmic cost that should be used.
  61. * Examples of these values can be found on the crypt page.
  62. *
  63. *
  64. * If omitted, a default value of 10 will be used. This is a good
  65. * baseline cost, but you may want to consider increasing it depending on your hardware.
  66. *
  67. *
  68. *
  69. *
  70. *
  71. *
  72. *
  73. * memory_cost (integer) - Maximum memory (in kibibytes) that may
  74. * be used to compute the Argon2 hash. Defaults to PASSWORD_ARGON2_DEFAULT_MEMORY_COST.
  75. *
  76. *
  77. *
  78. *
  79. * time_cost (integer) - Maximum amount of time it may
  80. * take to compute the Argon2 hash. Defaults to PASSWORD_ARGON2_DEFAULT_TIME_COST.
  81. *
  82. *
  83. *
  84. *
  85. * threads (integer) - Number of threads to use for computing
  86. * the Argon2 hash. Defaults to PASSWORD_ARGON2_DEFAULT_THREADS.
  87. *
  88. *
  89. *
  90. *
  91. * @param string $password The user's password.
  92. *
  93. * Using the PASSWORD_BCRYPT as the
  94. * algorithm, will result
  95. * in the password parameter being truncated to a
  96. * maximum length of 72 characters.
  97. * @param int|string|null $algo A password algorithm constant denoting the algorithm to use when hashing the password.
  98. * @param array $options An associative array containing options. See the password algorithm constants for documentation on the supported options for each algorithm.
  99. *
  100. * If omitted, a random salt will be created and the default cost will be
  101. * used.
  102. * @return string Returns the hashed password.
  103. *
  104. * The used algorithm, cost and salt are returned as part of the hash. Therefore,
  105. * all information that's needed to verify the hash is included in it. This allows
  106. * the password_verify function to verify the hash without
  107. * needing separate storage for the salt or algorithm information.
  108. * @throws PasswordException
  109. *
  110. */
  111. function password_hash(string $password, $algo, array $options = null): string
  112. {
  113. error_clear_last();
  114. if ($options !== null) {
  115. $result = \password_hash($password, $algo, $options);
  116. } else {
  117. $result = \password_hash($password, $algo);
  118. }
  119. if ($result === false) {
  120. throw PasswordException::createFromPhpError();
  121. }
  122. return $result;
  123. }