Encapsulates hash generation, comparison, and hex–binary conversion. Also supports generating random secrets. Includes Doctrine types.
With Composer installed on your computer and initialized for your project, run this command in your project’s root directory:
composer require lamansky/hashRequires PHP 8.4 or above.
<?php
use Lamansky\Hash\FixedLengthHashFactory;
use Lamansky\Hash\Hash;
use Lamansky\Hash\HashAlgorithm;
// Create Hash object by hashing a string
$sha256 = new HashAlgorithm('sha256');
$hash = $sha256->hash('test');
var_dump($hash instanceof Hash); // bool(true)
// Convert Hash object to hexadecimal
$hex = $hash->hexString;
echo $hex; // 9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08
// Convert hexadecimal string back to Hash object
$hash2 = Hash::fromByteAlignedHexString($hex);
// Convert hex to Hash, but require 256 bits, throwing error otherwise
$hash256Factory = FixedLengthHashFactory::asMeasuredInBits(256);
$hash3 = $hash256Factory->createFromString($hex);
// Generate random 256-bit secrets
$a = $hash256Factory->createRandom();
$b = $hash256Factory->createRandom();
// Timing-safe comparison between two Hashes
var_dump($a->equals($b)); // bool(false)
// Timing-safe comparison between Hash and string
$hex = $a->hexString;
var_dump($a->equalsString($hex)); // bool(true)<?php
use Doctrine\DBAL\Types\Type;
use Lamansky\Hash\Doctrine\Hash256Type;
use Lamansky\Hash\Doctrine\Hash384Type;
use Lamansky\Hash\Doctrine\Hash512Type;
Type::addType('hash256', Hash256Type::class); // 256-bit hash
Type::addType('hash384', Hash384Type::class); // 384-bit hash
Type::addType('hash512', Hash512Type::class); // 512-bit hash<?php
use Doctrine\ORM\Mapping as ORM;
use Lamansky\Hash\FixedLengthHashFactory;
use Lamansky\Hash\Hash;
#[ORM\Entity]
class Session {
#[ORM\Column(type: 'hash256')]
protected Hash $secret;
public function __construct() {
$this->secret = FixedLengthHashFactory::asMeasuredInBits(256)->createRandom();
}
public function secretEquals(string $other) : bool {
return $this->secret->equalsString($other);
}
}- Don’t test for
Hashequality by comparingbyteStringorhexStringdirectly; doing so can leave your app vulnerable to a timing attack. Rather, use theHashmethodsequals()orequalsString(); these utilize PHP’s timing-safehash_equalsfunction.
- The minimum supported PHP version is now 8.4 (instead of 7.0).
- The
Hashconstructor has been privatized. Instead, use one of the new static constructors:Hash::fromByteStringorHash::fromByteAlignedHexString. Providing the expected number of bits is no longer necessary. - Static constructor
Hash::generateRandomhas been removed. Instead, create an instance of the newFixedLengthHashFactoryclass and invoke thecreateRandommethod. - Static constructor
Hash::generatehas been removed. Instead, create an instance of the newHashAlgorithmclass and invoke thehashmethod. - The
Hashclass’stoHexmethod has been replaced with thehexStringproperty. - The
Hashclass’sequalsmethod no longer accepts strings; use the newequalsStringmethod instead. - All methods now throw custom exceptions instead of built-in exceptions; revise any catch blocks accordingly.