Merge "Http::getProxy() method to get proxy configuration"
[lhc/web/wiklou.git] / includes / password / PasswordFactory.php
index 86a3fef..3383fe3 100644 (file)
@@ -40,9 +40,9 @@ final class PasswordFactory {
         * @see PasswordFactory::register
         * @see Setup.php
         */
-       private $types = array(
-               '' => array( 'type' => '', 'class' => 'InvalidPassword' ),
-       );
+       private $types = [
+               '' => [ 'type' => '', 'class' => 'InvalidPassword' ],
+       ];
 
        /**
         * Register a new type of password hash
@@ -112,7 +112,7 @@ final class PasswordFactory {
         */
        public function newFromCiphertext( $hash ) {
                if ( $hash === null || $hash === false || $hash === '' ) {
-                       return new InvalidPassword( $this, array( 'type' => '' ), null );
+                       return new InvalidPassword( $this, [ 'type' => '' ], null );
                } elseif ( $hash[0] !== ':' ) {
                        throw new PasswordError( 'Invalid hash given' );
                }
@@ -156,7 +156,7 @@ final class PasswordFactory {
         */
        public function newFromPlaintext( $password, Password $existing = null ) {
                if ( $password === null ) {
-                       return new InvalidPassword( $this, array( 'type' => '' ), null );
+                       return new InvalidPassword( $this, [ 'type' => '' ], null );
                }
 
                if ( $existing === null ) {
@@ -188,4 +188,37 @@ final class PasswordFactory {
                        return $password->needsUpdate();
                }
        }
+
+       /**
+        * Generate a random string suitable for a password
+        *
+        * @param int $minLength Minimum length of password to generate
+        * @return string
+        */
+       public static function generateRandomPasswordString( $minLength = 10 ) {
+               // Decide the final password length based on our min password length,
+               // stopping at a minimum of 10 chars.
+               $length = max( 10, $minLength );
+               // Multiply by 1.25 to get the number of hex characters we need
+               // Generate random hex chars
+               $hex = MWCryptRand::generateHex( ceil( $length * 1.25 ) );
+               // Convert from base 16 to base 32 to get a proper password like string
+               return substr( Wikimedia\base_convert( $hex, 16, 32, $length ), -$length );
+       }
+
+       /**
+        * Create an InvalidPassword
+        *
+        * @return InvalidPassword
+        */
+       public static function newInvalidPassword() {
+               static $password = null;
+
+               if ( $password === null ) {
+                       $factory = new self();
+                       $password = new InvalidPassword( $factory, [ 'type' => '' ], null );
+               }
+
+               return $password;
+       }
 }