Here's my problem with PHP: For E-payment integration with Postfinance, I need to verify the data send and received with a SHA-256 hash of all the fields, with a secret key between each one.
How can you be sure that the input string will be in UTF-8 just before the hashing?
The command utf8_encode() is used on the input string, and if I check with mb_check_encoding(), it's okay. I have the good response, and if I use mb_detect_encoding(), the response is "ASCII".
$pf_post = array();
$pf_post['AMOUNT'] = 100;
$pf_post['CURRENCY'] = "CHF";
$pf_post['ORDERID'] = 101;
$pf_post['TITLE'] = "Paiement";
$pf_key = "mytestkey";
foreach (array_keys($pf_post) as $lakey)
{
$pf_string .= strtoupper($lakey) . "=" . strval($pf_post[$lakey]) . $pf_key;
}
$pf_string = utf8_encode($pf_string);
$pf_sign = hash('sha256',$pf_string);
if (mb_check_encoding($pf_string, 'UTF-8')) {
$debug .= "STRING => Détection UTF8 OK !<br>";
}
else {
$debug .= "STRING => Détection UTF8 !!! ERREUR !!!<br>";
}
if (mb_check_encoding($pf_sign, 'UTF-8')) {
$debug .= "HASH => Détection UTF8 OK !<br>";
}
else {
$debug .= "HASH => Détection UTF8 !!! ERREUR !!!<br>";
}
$debug .= "String Format: " . mb_detect_encoding($pf_string) .
", Hash Format: " . mb_detect_encoding($pf_sign) . "<br>";
And here's the debug:
STRING => Détection UTF8 OK !
HASH => Détection UTF8 OK !
String Format : ASCII, Hash Format : ASCII
If I use only digits in the fields, it will be okay ... if I use letters, it will be not every time okay ... and if use letters with accent ... it's wrong anytime!
In HTML header, I have:
<meta charset="utf-8"/>
How can I fix this?