VOOZH about

URL: https://deepwiki.com/hypervel/hashing/2.3-hash-manager

⇱ Hash Manager | hypervel/hashing | DeepWiki


Loading...
Menu

Hash Manager

Purpose and Scope

The HashManager class serves as the central orchestrator for password hashing operations within the hypervel/hashing library. Located at src/HashManager.php1-86 it extends Hypervel\Support\Manager and implements the Hasher interface to provide a unified interface for multiple hashing algorithms.

Key responsibilities include:

  • Driver Factory: Creates and caches instances of algorithm-specific hashers (Bcrypt, Argon2i, Argon2id)
  • Method Delegation: Forwards hashing operations to the appropriate concrete implementation
  • Configuration Integration: Retrieves driver-specific settings from the Hyperf configuration system
  • IDE Support: Declares @mixin \Hypervel\Hashing\Contracts\Hasher at src/HashManager.php11 to provide autocompletion for delegated methods

For details about the underlying hasher interface contract, see Hasher Interface. For information about the base functionality shared across implementations, see Abstract Base Classes. For specific algorithm implementations, see Hashing Algorithms.

Sources: src/HashManager.php1-13

Manager Pattern Implementation

The HashManager extends Hypervel\Support\Manager at src/HashManager.php13 and implements the Hasher interface, following the classic Manager pattern used throughout the Hyperf ecosystem. This design provides:

  • Driver Resolution: The inherited driver() method resolves driver names to concrete instances
  • Singleton Caching: Each driver is instantiated once and cached for subsequent requests
  • Lazy Instantiation: Drivers are only created when first accessed
  • Configuration Access: The $this->config property provides access to Hyperf's configuration system

Class Structure


The driver() method inherited from Manager performs the following operations:

  1. Calls getDefaultDriver() at src/HashManager.php82-85 to determine which driver to use
  2. Checks if the driver has already been instantiated (cached in $this->drivers)
  3. If not cached, calls the appropriate create{Driver}Driver() method
  4. Caches and returns the driver instance

Sources: src/HashManager.php8 src/HashManager.php13 composer.json32

Driver Factory Methods

The HashManager provides three factory methods for creating instances of supported hashing algorithms. Each method retrieves the appropriate configuration section and instantiates the corresponding hasher class.

Supported Drivers

Driver NameFactory MethodHasher ClassConfiguration Key
bcryptcreateBcryptDriver()BcryptHasherhashing.bcrypt
argoncreateArgonDriver()ArgonHasherhashing.argon
argon2idcreateArgon2idDriver()Argon2IdHasherhashing.argon

Factory Implementation


The factory methods are implemented as follows:

  • createBcryptDriver() at src/HashManager.php18-21 creates a BcryptHasher instance with configuration from hashing.bcrypt
  • createArgonDriver() at src/HashManager.php26-29 creates an ArgonHasher instance with configuration from hashing.argon
  • createArgon2idDriver() at src/HashManager.php34-37 creates an Argon2IdHasher instance with configuration from hashing.argon

Important: Both Argon2i and Argon2id drivers use the same configuration key (hashing.argon), as they share identical tuning parameters (memory, time, threads). The only difference between them is the underlying algorithm constant used by PHP.

Sources: src/HashManager.php16-37

Method Delegation

The HashManager implements all methods from the Hasher interface by delegating to the currently active driver. This delegation pattern ensures consistent behavior regardless of which hashing algorithm is being used.

Core Hashing Operations

MethodPurposeDelegation Target
make()Hash a plain text value$this->driver()->make()
check()Verify a value against a hash$this->driver()->check()
info()Get hash algorithm information$this->driver()->info()
needsRehash()Check if hash needs updating$this->driver()->needsRehash()

Method Delegation Flow


The delegation methods are implemented at:

Sources: src/HashManager.php42-69

Additional Utility Methods

Beyond the core Hasher interface methods, HashManager provides additional utility functionality:

Hash Detection

The isHashed() method at src/HashManager.php74-77 determines if a given string is already hashed by checking if password_get_info() returns a non-null algorithm identifier.

Default Driver Configuration

The getDefaultDriver() method at src/HashManager.php82-85 retrieves the default driver name from configuration, defaulting to 'bcrypt' if not specified.

Sources: src/HashManager.php74-85

Configuration Integration

The HashManager integrates deeply with Hyperf's configuration system to provide flexible, runtime-configurable hashing behavior.

Configuration Resolution


Configuration Keys

Configuration PathPurposeDefault Fallback
hashing.driverDefault driver selection'bcrypt'
hashing.bcryptBcrypt-specific options[]
hashing.argonArgon2i/Argon2id options[]

The configuration integration occurs through:

Sources: src/HashManager.php20 src/HashManager.php28 src/HashManager.php36 src/HashManager.php84