There is a problem. It has to do with Click and Pledge's Hash Implementation. Sometimes, it omits one 0 at random and leaves it as a blank, making it a 39 character string instead of 40.
This is possibly due to a coding bug as referenced here on msdn
I created a workaround for this issue, by finding and replacing each generated hash's 0's with blank spaces, creating an array of possible hashes and then checking the base64_decoded hash from CNP against the array of possibilities.
It's dirty but it works. There was no pattern I could find as to WHEN a 0 would go missing, but from what I have seen it only loses one 0. I assumed at first it was the last 0, but that was not the case.
Code:
<?php function checkHashString($secret_key, $order_number, $amount, $receivedHash, $dump = false){ $hash = base64_decode($receivedHash); echo strlen($hash); $sha = sha1(utf8_encode("[" . $secret_key . "][" . $order_number . "][" . $amount . "]")); $hashes = array($sha); $i = 0; while(strpos($sha, '0', $i) !== false ): $pos = strpos($sha, '0', $i); $hashes[] = substr_replace($sha, '', $pos, 1); $i = $pos + 1; echo $i; endwhile; if($dump): $dump = $hashes; $dump[] = $hash; echo '<pre>'; print_r($dump); echo '</pre>'; endif; return in_array($hash, $hashes); } ?>
Code:
<?php $results[] = checkHashString('letshashitout', 1406301520403881111, '50.00', 'MTkxOWJkMjU5YjIyODJiNTEyYTc1YTcxNzcxMmM2MTFhNGE0ZGEx'); $results[] = checkHashString('test123', 1404211124504441111, '17.00', 'ZTdhYjcwMjRiOWRlYzMxNThhNjdmOTc5ZDVmMTY5OTA5YWUyYTFk'); $results[] = checkHashString('test123', 1404181859539791111, '65.00', 'NjNiZDNiNTc5OTI2NmU2NjVmNmJjZmQ0Yjk2Y2FjMzJiY2I5MWI5'); $results[] = checkHashString('test123', 1404211146064211111, '897.00', 'NTQzNjdmODA1ZTJhN2QyOTg1MTgxNDE1NDM3YzdiZjFiNjNlY2FlOA=='); $results[] = checkHashString('letshashitout', 1406301520403881111, '50.00', 'MTkxOWJkMjU5YjIyODJiNTEyYTc1YTcxNzcxMmM2MTFhNGE0ZGEx=='); echo '<pre>'; print_r($results); echo '</pre>'; ?>
Originally posted by Simon
View Post
Leave a comment: