Pbkdf2Password.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. /**
  3. * Implements the Pbkdf2Password class for the MediaWiki software.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * A PBKDF2-hashed password
  24. *
  25. * This is a computationally complex password hash for use in modern applications.
  26. * The number of rounds can be configured by $wgPasswordConfig['pbkdf2']['cost'].
  27. *
  28. * @since 1.24
  29. */
  30. class Pbkdf2Password extends ParameterizedPassword {
  31. protected function getDefaultParams() {
  32. return [
  33. 'algo' => $this->config['algo'],
  34. 'rounds' => $this->config['cost'],
  35. 'length' => $this->config['length']
  36. ];
  37. }
  38. protected function getDelimiter() {
  39. return ':';
  40. }
  41. public function crypt( $password ) {
  42. if ( count( $this->args ) == 0 ) {
  43. $this->args[] = base64_encode( random_bytes( 16 ) );
  44. }
  45. $hash = hash_pbkdf2(
  46. $this->params['algo'],
  47. $password,
  48. base64_decode( $this->args[0] ),
  49. (int)$this->params['rounds'],
  50. (int)$this->params['length'],
  51. true
  52. );
  53. if ( !is_string( $hash ) ) {
  54. throw new PasswordError( 'Error when hashing password.' );
  55. }
  56. $this->hash = base64_encode( $hash );
  57. }
  58. }