PHP code example of lamansky / hash

1. Go to this page and download the library: Download lamansky/hash library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

lamansky / hash example snippets



use Lamansky\Hash\Hash;

//Hash a string
echo Hash::generate('md5', 'test')->toHex(); //098f6bcd4621d373cade4e832627b4f6

//Generate random 128-bit and 256-bit tokens
$a = Hash::generateRandom(128);
$b = Hash::generateRandom(256);

//Timing-safe comparison
var_dump($a->equals($b)); //bool(false)

//Convert Hash to hex string
$hex = $a->toHex();
echo $hex; //4df3fa5a70eaa4097740b46017d428e2

//Compare Hash with string
var_dump($a->equals($hex)); //bool(true)

//Turn string back into a Hash
$c = new Hash(128, $hex);

var_dump($c->equals($a)); //bool(true)


use Doctrine\DBAL\Types\Type;
Type::addType('hash256', 'Lamansky\Hash\Doctrine\Hash256Type'); //256-bit hash
Type::addType('hash384', 'Lamansky\Hash\Doctrine\Hash384Type'); //384-bit hash
Type::addType('hash512', 'Lamansky\Hash\Doctrine\Hash512Type'); //512-bit hash


use Lamansky\Hash\Hash;

/**
 * @Entity
 */
class Session {

    /**
    * @Column(type="hash256")
    */
    protected $token;

    public function __construct() {
        $this->token = Hash::generateRandom(256);
    }

    public function tokenEquals($other) : bool {
        return $this->token->equals($other);
    }
}