MediaWiki master
Pbkdf2PasswordUsingOpenSSL.php
Go to the documentation of this file.
1<?php
23declare( strict_types = 1 );
24
25namespace MediaWiki\Password;
26
36 private static $digestAlgos;
37
54 private const DIGEST_ALGOS = [
55 'sha256' => 'sha256',
56 'sha512' => 'sha512',
57 ];
58
59 protected function getDigestAlgo( string $algo ): ?string {
60 if ( !isset( self::$digestAlgos ) ) {
61 self::$digestAlgos = array_intersect( self::DIGEST_ALGOS, openssl_get_md_methods() );
62 }
63 return self::$digestAlgos[$algo] ?? null;
64 }
65
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() );
78 }
79 return $hash;
80 }
81}
82
84class_alias( Pbkdf2PasswordUsingOpenSSL::class, 'Pbkdf2PasswordUsingOpenSSL' );
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Show an error when any operation involving passwords fails to run.
pbkdf2(string $digestAlgo, string $password, string $salt, int $rounds, int $length)
Call the PBKDF2 implementation, which hashes the password.
static getDigestAlgo(string $algo)
Get the implementation specific name for a hash algorithm.