hash

Created: 2014-05-18 14:49
Updated: 2014-09-25 16:27
php

README.md

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

Cookies help us deliver our services. By using our services, you agree to our use of cookies Learn more