Skip to content
lamanskyPublic

About

Encapsulates hash generation, comparison, and hex–binary conversion. Also supports generating random secrets. Includes Doctrine types.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Repository files navigation

Lamansky/Hash

Encapsulates hash generation, comparison, and hex–binary conversion. Also supports generating random secrets. Includes Doctrine types.

Installation

With Composer installed on your computer and initialized for your project, run this command in your project’s root directory:

composer require lamansky/hash

Requires PHP 8.4 or above.

Usage Tutorial

<?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)

In Conjunction with Doctrine

<?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);
    }
}

Security Considerations

  • Don’t test for Hash equality by comparing byteString or hexString directly; doing so can leave your app vulnerable to a timing attack. Rather, use the Hash methods equals() or equalsString(); these utilize PHP’s timing-safe hash_equals function.

Major Release Migration Guide

1.x ⇒ 2.x

  • The minimum supported PHP version is now 8.4 (instead of 7.0).
  • The Hash constructor has been privatized. Instead, use one of the new static constructors: Hash::fromByteString or Hash::fromByteAlignedHexString. Providing the expected number of bits is no longer necessary.
  • Static constructor Hash::generateRandom has been removed. Instead, create an instance of the new FixedLengthHashFactory class and invoke the createRandom method.
  • Static constructor Hash::generate has been removed. Instead, create an instance of the new HashAlgorithm class and invoke the hash method.
  • The Hash class’s toHex method has been replaced with the hexString property.
  • The Hash class’s equals method no longer accepts strings; use the new equalsString method instead.
  • All methods now throw custom exceptions instead of built-in exceptions; revise any catch blocks accordingly.

About

Encapsulates hash generation, comparison, and hex–binary conversion. Also supports generating random secrets. Includes Doctrine types.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages