BcryptPassword.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /**
  3. * Implements the BcryptPassword 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 Bcrypt-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['bcrypt']['cost'].
  27. *
  28. * @since 1.24
  29. */
  30. class BcryptPassword extends ParameterizedPassword {
  31. protected function getDefaultParams() {
  32. return [
  33. 'rounds' => $this->config['cost'],
  34. ];
  35. }
  36. protected function getDelimiter() {
  37. return '$';
  38. }
  39. protected function parseHash( $hash ) {
  40. parent::parseHash( $hash );
  41. $this->params['rounds'] = (int)$this->params['rounds'];
  42. }
  43. /**
  44. * @param string $password Password to encrypt
  45. *
  46. * @throws PasswordError If bcrypt has an unknown error
  47. * @throws MWException If bcrypt is not supported by PHP
  48. */
  49. public function crypt( $password ) {
  50. if ( !defined( 'CRYPT_BLOWFISH' ) ) {
  51. throw new MWException( 'Bcrypt is not supported.' );
  52. }
  53. // Either use existing hash or make a new salt
  54. // Bcrypt expects 22 characters of base64-encoded salt
  55. // Note: bcrypt does not use MIME base64. It uses its own base64 without any '=' padding.
  56. // It expects a 128 bit salt, so it will ignore anything after the first 128 bits
  57. if ( !isset( $this->args[0] ) ) {
  58. $this->args[] = substr(
  59. // Replace + with ., because bcrypt uses a non-MIME base64 format
  60. strtr(
  61. // Random base64 encoded string
  62. base64_encode( random_bytes( 16 ) ),
  63. '+', '.'
  64. ),
  65. 0, 22
  66. );
  67. }
  68. $hash = crypt( $password,
  69. sprintf( '$2y$%02d$%s', (int)$this->params['rounds'], $this->args[0] ) );
  70. if ( !is_string( $hash ) || strlen( $hash ) <= 13 ) {
  71. throw new PasswordError( 'Error when hashing password.' );
  72. }
  73. // Strip the $2y$
  74. $parts = explode( $this->getDelimiter(), substr( $hash, 4 ) );
  75. $this->params['rounds'] = (int)$parts[0];
  76. $this->args[0] = substr( $parts[1], 0, 22 );
  77. $this->hash = substr( $parts[1], 22 );
  78. }
  79. }