Hasher

The Hasher class is used to generate hashes. It tries to generates salts in multiple custom ways. The methods available on Hasher are:

The following keywords should be noted:

keywords
    
    data    : array or string data expected to be hashed

    secret  : password string for hashing

    length  : length of hash to be returned

    algo    : any hash algorithm or custom function.

    keys    : specific string of characters from which an hash string should be generated

    hashFunc: any acceptable function for hashing. The may be hashing algorithms or custom functions
    
                                        

Initializing class
The hasher tool can be easily initialized as shown below.

Sample: Initializing Hasher
  $hasher  = new Hasher;
                                        

sethash
The sethash method is used to set a data meant for hashing along with an optional secret password.

Syntax: sethash
  $hasher->sethash(data, secret);
                                        

We shall be looking at a series of examples below.

Example: setting hash
  $hasher->sethash('sometext'); // set a string to be hashed
    
  $hasher->sethash(['user' => 'foo']); // set an array to be hashed

  $hasher->sethash(['user' => 'foo'], 'password123'); // set an array to be hashed with a secret password
                                

randomHash
The randomHash() method is used to generate a random hash string

Syntax: randomhash
    $hasher->randomHash(length, keys, algo); 

    
        where:
        
                length - optional length of hash to be returned
                keys   - an optional set of charaters from which hash should be picked 
                algo   - an optional custom function to hash generated keys.
                                        
                                        
Examples: randomHash
  $hasher->randomHash(50);  generate 50 character string

  $hasher->randomHash(50, 'abc');  generate 50 character string from abc

  $hasher->randomHash('', 'abc', 'sha1');  hash 'abc' with sha1 + time
    
  
  When using hash algorithms, length of character returned depends on the hash algorithm itself. 
  This means that using lengths does not have any on code sample 3 above.
  
                                        

hashFunc
The hashFunc() method sets the algorithms for hashing.

Syntax: hashFunc
    $hasher->hashFunc(algo); // supplies data  to be hashed.
                                        
Example: hashFunc
    $hasher->setHash(['foo','bar'],'1234');

    $hasher->hashFunc(['sha1']); //use one algo for hashing

    $hasher->hashFunc(['sha1', 'md5']); //use two algos for hashing
                                        

In the above sample, the array data will be hashed depending on the number of algorithms supplied.

hashify
This method executes the hash code. It works in different formats which are revealed below

Syntax: hashify
    $hasher->hasify(param1, param2); 
        
            where:
            
            @param [int|bool|array] param1 (case 1)
                param1 == (int = 0) => reset hash and return first hash  
                param1 == (int > 0) => number of times for hashing
                param1 == (array | string) => list of hashing algorithms
        
            @param [int] param2 (case 2)
                param2 == (int > 0) => number of times for hashing
                param2 == (int = 0) => reset hash and return first hash 
    
            Note: The hashify function takes one or two arguments.

                One argument execute case 1 
                Two arguments execute case 2
        
                                        
Example 1: hashify
    $hasher->setHash('mydata', 'password');
    $hasher->hashFunc(['sha1','md5']);  // set function(s) to use for hashtype
    
    $hash1 = $hasher->hashify();  //first hash

    $hash2 = $hasher->hashify();  //second hash

    $hash3 = $hasher->hashify();  //third hash

    $hash4 = $hasher->hashify(0); //reset hash to first hash

    $hash5 = $hasher->hashify();  //second hash
     
    Note: In the example above, every hash is rehashed at each call until the 
    counter is set back to 0. $hash4 will return the same data as $hash1 because 
    the counter was reset and $hash5 will return the same data as $hash2
    
                                        
Example 2: hashify
    $hasher->setHash('mydata', 'password');

    $hasher->hashFunc(['sha1','md5']);  //set function(s) to use for hashtype
    
    $hash3 = $hasher->hashify(3); //return exactly 3 hash executions

     
    In the example above the hash returned will be equivalent to 3 exactly three 
    hashes made just as example 1 above.
                                        
                                        
Example 3: hashify
    $hasher->setHash('mydata', 'password');

    //set hash functions within hashify
    $hashArr = $hasher->hashify(['sha1','md5']);
    $hashStr = $hasher->hashify('sha1');

     
    The examples above shows that hash funcs can be supplied within the hashify method itself. 
    When text strings or arrays are supplied as one argument, hashify processes such arguments 
    as hash functions.
    
                                        
Example 4: hashify
    $hasher->setHash('mydata', 'password');
    `
    //set hash functions and number of times of rehash within hashify
    $hashArr = $hasher->hashify(['sha1','md5'], 4); 
    $hashStr = $hasher->hashify('sha1', 4);

     
    When two arguments are supplied, the first argument resolves as hash function while the second argument resolves as 
    number of times for rehashing. When hash functions are declared within the hashify method, it overides 
    the default set.
    
                                        

randomize
This method is used to randomize an hash function. When used, it provides a one-way hash code that cannot be recovered. The randomize method makes sure that the data returned by a hash is never the same at different times.

Syntax: randomize
    $hasher->randomize(bool|string); 
    
        where:

            bool:true  => allow randomize (default)
            bool:false => disallow randomize
            string     => string text used to randomize
    
                                        
Example 1: randomize

    $hasher->setHash(['somearray']);
        
    $hasher->randomize(); //uses time

    $hasher->hashify(); //generates a new hash using current time

                                        
Example 2: randomize
    $hasher->setHash(['somearray']);
        
    $hasher->randomize('sometext'); //uses sometext + time

    $hash = $hasher->hashify('sha1'); //generates a new hash every time with 'sometext' using sha1 algo

    var_dump( $hash ) //output hash