master

laravel/framework

Last updated at: 29/12/2023 09:23

HashManager.php

TLDR

The provided file HashManager.php is a part of the Illuminate\Hashing namespace in the Demo Projects project. It extends the Manager and implements the Hasher contract. The class HashManager provides methods for creating instances of different hashing drivers, getting information about hashed values and performing various hashing operations.

Methods

createBcryptDriver

This method creates an instance of the BcryptHasher class using the configuration options specified in the hashing.bcrypt array.

createArgonDriver

This method creates an instance of the ArgonHasher class using the configuration options specified in the hashing.argon array.

createArgon2idDriver

This method creates an instance of the Argon2IdHasher class using the configuration options specified in the hashing.argon array.

info

This method retrieves information about the given hashed value by calling the corresponding method on the current driver.

make

This method generates a hash of the given value using the current driver and options.

check

This method checks if the given plain value matches the hashed value using the current driver and options.

needsRehash

This method determines if the given hashed value needs to be rehashed based on the current driver and options.

isHashed

This method checks if a given string is already hashed by checking the algorithm info from password_get_info.

getDefaultDriver

This method retrieves the default driver name from the configuration options (hashing.driver).

Classes

None

<?php

namespace Illuminate\Hashing;

use Illuminate\Contracts\Hashing\Hasher;
use Illuminate\Support\Manager;

/**
 * @mixin \Illuminate\Contracts\Hashing\Hasher
 */
class HashManager extends Manager implements Hasher
{
    /**
     * Create an instance of the Bcrypt hash Driver.
     *
     * @return \Illuminate\Hashing\BcryptHasher
     */
    public function createBcryptDriver()
    {
        return new BcryptHasher($this->config->get('hashing.bcrypt') ?? []);
    }

    /**
     * Create an instance of the Argon2i hash Driver.
     *
     * @return \Illuminate\Hashing\ArgonHasher
     */
    public function createArgonDriver()
    {
        return new ArgonHasher($this->config->get('hashing.argon') ?? []);
    }

    /**
     * Create an instance of the Argon2id hash Driver.
     *
     * @return \Illuminate\Hashing\Argon2IdHasher
     */
    public function createArgon2idDriver()
    {
        return new Argon2IdHasher($this->config->get('hashing.argon') ?? []);
    }

    /**
     * Get information about the given hashed value.
     *
     * @param  string  $hashedValue
     * @return array
     */
    public function info($hashedValue)
    {
        return $this->driver()->info($hashedValue);
    }

    /**
     * Hash the given value.
     *
     * @param  string  $value
     * @param  array  $options
     * @return string
     */
    public function make($value, array $options = [])
    {
        return $this->driver()->make($value, $options);
    }

    /**
     * Check the given plain value against a hash.
     *
     * @param  string  $value
     * @param  string  $hashedValue
     * @param  array  $options
     * @return bool
     */
    public function check($value, $hashedValue, array $options = [])
    {
        return $this->driver()->check($value, $hashedValue, $options);
    }

    /**
     * Check if the given hash has been hashed using the given options.
     *
     * @param  string  $hashedValue
     * @param  array  $options
     * @return bool
     */
    public function needsRehash($hashedValue, array $options = [])
    {
        return $this->driver()->needsRehash($hashedValue, $options);
    }

    /**
     * Determine if a given string is already hashed.
     *
     * @param  string  $value
     * @return bool
     */
    public function isHashed($value)
    {
        return password_get_info($value)['algo'] !== null;
    }

    /**
     * Get the default driver name.
     *
     * @return string
     */
    public function getDefaultDriver()
    {
        return $this->config->get('hashing.driver', 'bcrypt');
    }
}