Skip to content

Instantly share code, notes, and snippets.

@VottusCode
Last active May 15, 2024 05:27
Show Gist options
  • Save VottusCode/6e9817fc650cde030e2037c3f9b4a45f to your computer and use it in GitHub Desktop.
Save VottusCode/6e9817fc650cde030e2037c3f9b4a45f to your computer and use it in GitHub Desktop.

Revisions

  1. VottusCode revised this gist Jul 1, 2023. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions app-Facades-Hash.php
    Original file line number Diff line number Diff line change
    @@ -12,8 +12,7 @@
    *
    * @package App\Facades
    *
    * @author Mia <[email protected]>
    * @createdAt 24/04/2021 22:50
    * @author Mia Vališová <[email protected]>
    */
    class Hash extends Facade implements Hasher {

    @@ -69,7 +68,7 @@ public function info($hashedValue): ?array
    */
    public function make($value, array $options = []): string
    {
    $salt = Str::random(self::SALT_LENGTH);
    $salt = !empty($options['salt']) && is_string($options['salt']) ? $options($options['salt']) : ? Str::random(self::SALT_LENGTH);
    return '$SHA$' . $salt . '$' . hash('sha256', hash('sha256', $value) . $salt);
    }

  2. VottusCode revised this gist Apr 24, 2021. 1 changed file with 10 additions and 2 deletions.
    12 changes: 10 additions & 2 deletions app-Facades-Hash.php
    Original file line number Diff line number Diff line change
    @@ -80,16 +80,24 @@ public function make($value, array $options = []): string
    * same salt that the hash has. If both hashes match,
    * the password is correct.
    *
    * Returns false if the hash is invalid.
    *
    * @param string $value
    * @param string $hashedValue
    * @param array $options
    * @return bool
    */
    public function check($value, $hashedValue, array $options = []): bool
    {
    $parts = explode('$', $hashedValue);
    $split = explode('$', $hashedValue);
    if (count($split) !== 4) return false; // Invalid hash

    $hashAndSalt = $split[3];
    $salt = array_key_exists("salt", $options) && is_string($options["salt"])
    ? $options["salt"]
    : $split[2];

    return count($parts) === 4 && $parts[3] === $this->make($value, [ "salt" => $parts[2] ]);
    return $hashAndSalt === $this->make($value, [ "salt" => $salt ]);
    }

    /**
  3. VottusCode created this gist Apr 24, 2021.
    107 changes: 107 additions & 0 deletions app-Facades-Hash.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,107 @@
    <?php declare(strict_types=1);

    namespace App\Facades;

    use Illuminate\Contracts\Hashing\Hasher;
    use Illuminate\Support\Facades\Facade;
    use Illuminate\Support\Str;

    /**
    * Hasher implementation that supports
    * the AuthMe SHA256 hash.
    *
    * @package App\Facades
    *
    * @author Mia <[email protected]>
    * @createdAt 24/04/2021 22:50
    */
    class Hash extends Facade implements Hasher {

    const SALT_LENGTH = 16;

    /**
    * Gets the name of the Facade.
    *
    * @return string
    */
    protected static function getFacadeAccessor()
    {
    return 'shaHash';
    }

    /**
    * Returns the information about the hash.
    *
    * Array shape:
    * [
    * "rawHash" => // the raw hash,
    * "salt" => // the salt
    * ]
    *
    * Returns null if the hash is invalid.
    *
    * @param string $hashedValue
    * @return array|null
    */
    public function info($hashedValue): ?array
    {
    $parts = explode('$', $hashedValue);
    return count($parts) === 4 ? [
    "rawHash" => $parts[2],
    "salt" => $parts[3]
    ] : null;
    }

    /**
    * Makes a hash with a random salt.
    *
    * Additionally, you can pass a salt in the
    * options array.
    *
    * Options shape (optional):
    * [
    * "salt" => // Specify the salt
    * ]
    *
    * @param string $value
    * @param array $options
    * @return string
    */
    public function make($value, array $options = []): string
    {
    $salt = Str::random(self::SALT_LENGTH);
    return '$SHA$' . $salt . '$' . hash('sha256', hash('sha256', $value) . $salt);
    }

    /**
    * Checks whether the password matches the hash.
    *
    * This is done by hashing the password again with the
    * same salt that the hash has. If both hashes match,
    * the password is correct.
    *
    * @param string $value
    * @param string $hashedValue
    * @param array $options
    * @return bool
    */
    public function check($value, $hashedValue, array $options = []): bool
    {
    $parts = explode('$', $hashedValue);

    return count($parts) === 4 && $parts[3] === $this->make($value, [ "salt" => $parts[2] ]);
    }

    /**
    * This function always returns false as the hash
    * never needs rehash.
    *
    * @param string $hashedValue
    * @param array $options
    * @return bool
    */
    public function needsRehash($hashedValue, array $options = []): bool
    {
    return false;
    }
    }