Hasher class is used to generate
hashes. It tries to generates salts in multiple custom ways.
The methods available on Hasher are:
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
$hasher = new Hasher;
sethash method is used to set a data meant for hashing along with
an optional secret password.
$hasher->sethash(data, secret);
$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() method is used to generate a random hash string
$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.
$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() method sets the algorithms for hashing.
$hasher->hashFunc(algo); // supplies data to be hashed.
$hasher->setHash(['foo','bar'],'1234');
$hasher->hashFunc(['sha1']); //use one algo for hashing
$hasher->hashFunc(['sha1', 'md5']); //use two algos for hashing
$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
$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
$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.
$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.
$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 method makes sure that
the data returned by a hash is never the same at different times.
$hasher->randomize(bool|string);
where:
bool:true => allow randomize (default)
bool:false => disallow randomize
string => string text used to randomize
$hasher->setHash(['somearray']);
$hasher->randomize(); //uses time
$hasher->hashify(); //generates a new hash using current time
$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