Wallogit.com
2017 © Pedro Peláez
Hash Generator and Verifier
A PHP Hash Generator and Verifier, (*1)
This is a wrapper on the PHP function HASH for generating hash digests using sha256 algorithm and also verifying a given hash againist given data., (*2)
PHP v>=5.1.2+, (*3)
The easiest way to install Carbon is via composer. Create the following composer.json file and run the php composer.phar install command to install it., (*4)
"require": {
"edwinmugendi/hashdigest": "master"
}
Why not use composer? Anyway, Download Digester.php from the repo and save the file somewhere in your project., (*5)
<?php
require 'path/to/Digester.php';
use HashDigest\Digester;
$dataToHash = array(
'name'=>'Ali',
'age'=>'12',
'city'=>'Nairobi',
'work'=>'Software Engineer',
'at'=>'Sapama.com'
);
$hash = Digester::digest($dataToHash);
echo 'Generated Hash is ';
echo "\n";
echo $hash;
echo "\n";
$isValid = Digester::isHashValid($hash, $dataToHash);
echo "Hash is ". ($isValid ? 'valid': 'not valid');
The hash is generated by:, (*6)
digest($dataToHash, $algo = 'sha256', $raw_output = false, $separator = '.') function takes a string or array as parameter of data to be hashed eg;<?php
$dataToHash = array(
'name'=>'Ali',
'age'=>'12',
'city'=>'Nairobi',
'work'=>'Software Engineer',
'at'=>'Sapama.com'
);
$hash = Digester::digest($dataToHash);
<?php
$dataToHash = array(
'age'=>'12',
'at'=>'Sapama.com'
'city'=>'Nairobi',
'name'=>'Ali',
'work'=>'Software Engineer'
);
digest function. Hence, the string generated from the above array will be:$stringToHash = '12.Sapama.com.Nairobi.Ali.Software Engineer' ```` 4. Hash the generated string with sha256 algorithm (for PHP use PHP Hash) ```php $hash = '87304e9ab39f1d9c70b9f51f9f3b70fed5d19fbd3917ea3678115c5adffcf0d5';
isHashValid($hash,$dataToHash), you need to pass the pre created hash and the data from which the hash was generated from.true if hash is verified, false otherwiseAPI_SECRET (see below) of data to be passed to an API. Remember to also include the generated hash to the data being POSTed so that the API can generate an hash using the data and try and verify against the POSTed hash.NB: API_SECRET This is code or a random string known only to the API provider and consumer and is included in the data to be hashed to make the hash more difficult to regenerate., (*7)
Happy Coding!, (*8)