devhelp/hash

Introduces HashGenerator class that can be used to register and use different hash algorithms

1.0 2014-09-25 16:26 UTC

This package is not auto-updated.

Last update: 2024-11-09 16:42:45 UTC


README

Build Status SensioLabsInsight

Installation

Composer is preferred to install Devhelp/Hash, please check composer website for more information.

$ composer require 'devhelp/hash:dev-master'

Purpose

Devhelp/Hash introduces HashGenerator class that can be used to register and use different hash algorithms. It supports all PHP core hash algorithms by default, but you can register custom ones as \Closure or Devhelp\Hash\Algorithm\HashAlgorithmInterface

Usage

Using built-in php core algorithms

it uses hash function under the hood

$generator = new \Devhelp\Hash\HashGenerator();

$generator->generate('sha256', 'some_data'); // returns hash generated by sha256 algorithm

$generator->generate('sha256', 'some_data', array('raw_output' => true);

Registering custom algorithms

class CustomHashAlgorithm implements \Devhelp\Hash\Algorithm\HashAlgorithmInterface
{
    public function hash($data, array $options = array())
    {
        //...
    }
}

$myAlgorithm = new CustomHashAlgorithm();
$myClosure = function ($data, $options) {
    //...
};

$generator = new \Devhelp\Hash\HashGenerator();

$generator->register('my_algorithm', $myAlgorithm);
$generator->register('my_closure', $myClosure);

$generator->generate('my_algorithm', 'some_data'); //returns hash generated by CustomHashAlgorithm class
$generator->generate('my_closure', 'some_data'); //returns hash generated by $myClosure \Closure

Overriding built-in PHP core algorithms

Only thing to do is to define custom algorithm and register in under the same name as the core algorithm

$generator = new \Devhelp\Hash\HashGenerator();

$generator->register('sha512', new CustomHashAlgorithm());

$generator->generate('sha512', $data); //returns hash generated by CustomHashAlgorithm class

Credits

Brought to you by : Devhelp.pl