Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / password / Pbkdf2PasswordUsingOpenSSL.php
blobf6abb1f93036784b60981f23332e43dbe2d4c773
1 <?php
2 /**
3 * Implements the Pbkdf2PasswordUsingOpenSSL class for the MediaWiki software.
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.
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.
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
20 * @file
23 declare( strict_types = 1 );
25 namespace MediaWiki\Password;
27 /**
28 * A PBKDF2-hashed password, using OpenSSL
30 * @since 1.40
32 class Pbkdf2PasswordUsingOpenSSL extends AbstractPbkdf2Password {
33 /**
34 * @var array<string,string>|null
36 private static $digestAlgos;
38 /**
39 * List of hash algorithms we support and OpenSSL's names for them.
41 * We include only the algorithms that make sense to support, rather than
42 * all potentially available algorithms. In particular, we do not include:
44 * - Broken algorithms, such as "md5" and "sha1"
45 * - Algorithms no longer available by default, such as "whirlpool"
46 * - Algorithms that perform especially poorly on server CPUs relative
47 * to other available hardware (as of 2022, this includes "sha3-512";
48 * see <https://keccak.team/2017/is_sha3_slow.html>)
49 * - Variants for which there is no reason for use, such as "sha384"
50 * (a truncated "sha512" that starts with a different initial state)
52 * The array keys should match the algorithm names known to hash_pbkdf2().
54 private const DIGEST_ALGOS = [
55 'sha256' => 'sha256',
56 'sha512' => 'sha512',
59 protected function getDigestAlgo( string $algo ): ?string {
60 if ( self::$digestAlgos === null ) {
61 self::$digestAlgos = array_intersect( self::DIGEST_ALGOS, openssl_get_md_methods() );
63 return self::$digestAlgos[$algo] ?? null;
66 protected function pbkdf2(
67 string $digestAlgo,
68 string $password,
69 string $salt,
70 int $rounds,
71 int $length
72 ): string {
73 // Clear error string
74 while ( openssl_error_string() !== false );
75 $hash = openssl_pbkdf2( $password, $salt, $length, $rounds, $digestAlgo );
76 if ( !is_string( $hash ) ) {
77 throw new PasswordError( 'Error when hashing password: ' . openssl_error_string() );
79 return $hash;
83 /** @deprecated since 1.43 use MediaWiki\\Password\\Pbkdf2PasswordUsingOpenSSL */
84 class_alias( Pbkdf2PasswordUsingOpenSSL::class, 'Pbkdf2PasswordUsingOpenSSL' );