any one can help me out of this problem?
i'm getting error of 'call of undefined function hash_equals()
'
here is my code:
$username = 'Admin';
$password = 'sample1Pasword';
$dbh = new PDO('mysql:host=localhost;dbname=test', $USER, $PASSWORD);
$sth = $dbh->prepare('
SELECT
hash
FROM users
WHERE
username = :username
LIMIT 1
');
$sth->bindParam(':username', $username);
$sth->execute();
$user = $sth->fetch(PDO::FETCH_OBJ);
// Hashing the password with its hash as the salt returns the same hash
if ( hash_equals($user->hash, crypt($password, $user->hash)) ) {
// Ok!
}else{
//user not found
}
I dont know what's going on, i just search for this function but it givin me a problem instead. sorry for my bad english. Thank you!
In php.net docs we can find a replacement function for older php versions :
if(!function_exists('hash_equals'))
{
function hash_equals($str1, $str2)
{
if(strlen($str1) != strlen($str2))
{
return false;
}
else
{
$res = $str1 ^ $str2;
$ret = 0;
for($i = strlen($res) - 1; $i >= 0; $i--)
{
$ret |= ord($res[$i]);
}
return !$ret;
}
}
}
Otherwise, you could use this library : https://github.com/ircmaxell/password_compat
Use phpinfo()
and check your PHP version. PHP versions prior to 5.6 don't have hash_equals
function built-in. Upgrade to the latest version of PHP (or at least to 5.6) to use this function.
Solution taken from Facebook sdk: https://github.com/facebook/php-graph-sdk/blob/5.x/src/Facebook/polyfills.php
/**
* Copyright 2017 Facebook, Inc.
*
* You are hereby granted a non-exclusive, worldwide, royalty-free license to
* use, copy, modify, and distribute this software in source code or binary
* form for use in connection with the web services and APIs provided by
* Facebook.
*
* As with any software that integrates with the Facebook platform, your use
* of this software is subject to the Facebook Developer Principles and
* Policies [http://developers.facebook.com/policy/]. This copyright notice
* shall be included in all copies or substantial portions of the software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
/**
* @see https://github.com/sarciszewski/php-future/blob/master/src/Security.php#L37-L51
*/
if (!function_exists('hash_equals')) {
function hash_equals($knownString, $userString)
{
if (function_exists('mb_strlen')) {
$kLen = mb_strlen($knownString, '8bit');
$uLen = mb_strlen($userString, '8bit');
} else {
$kLen = strlen($knownString);
$uLen = strlen($userString);
}
if ($kLen !== $uLen) {
return false;
}
$result = 0;
for ($i = 0; $i < $kLen; $i++) {
$result |= (ord($knownString[$i]) ^ ord($userString[$i]));
}
// They are only identical strings if $result is exactly 0...
return 0 === $result;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With