2017-08-11 100 views
0

这是我的PHP问题:对于E-Payement与Postfinance的集成,我需要验证数据发送和接收所有字段的SHA256哈希,并在每个字段之间使用密钥。PHP:SHA256强制utf8

y怎么能确定输入字符串在散列之前会以UTF8的形式出现?

utf8_encode()用于输入字符串,如果我用mb_check_encoding()检查,没关系,我有良好的响应,如果我使用mb_detect_encoding(),响应是“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>"; 

而这里的调试:

STRING => Détection UTF8 OK ! 
HASH => Détection UTF8 OK ! 
String Format : ASCII, Hash Format : ASCII 

如果我只用在地里的数字,它会好起来的......如果我使用信件,它会不会每次没关系...和如果使用有口音的字母......这是错误的任何时间!

在HTML头,我有:

<meta charset="utf-8"/> 

请帮帮我!谢谢 !

+0

嗨!你试过这个吗? https://stackoverflow.com/a/1037368/1461181 – DanielO

+0

_“命令utf8_encode()用于输入字符串”_ - 为什么,它不是UTF-8了吗?我们在这里讨论表格数据,对吗?您的页面已经是UTF-8了,那么您是如何设法让表单数据首先不是以UTF-8发送的呢? – CBroe

+0

或者你只是在讨论'$ pf_post'数据吗?如果这已经不是UTF-8,这意味着你没有将脚本文件本身保存为UTF-8,所以_that_将是你需要纠正的,并且不会在运行时混淆编码... – CBroe

回答

0

下面是一些例子:

$str="éóùùééééè"; // will output 'éóùùééééè' 
$strr =utf8_encode($str); // will output 'éóùùééééè' 

如果应用到UTF8字符串,则函数utf8_encode()将返回一个乱码UTF8输出如图所示的例子。

为确保您使用的是UTF-8,在每个脚本的顶部使用mb_internal_encoding('UTF-8'),您可以使用此forceutf8类。链接https://github.com/neitanod/forceutf8

<?php 
// Tell PHP that we're using UTF-8 strings until the end of the script 
mb_internal_encoding('UTF-8'); 

include('Encoding.php'); //the class from github 

//same strings 
$str = 'éóùùééééè'; 
$str1 = 'éóùùééééè'; //garbled UTF8 of éóùùééééè 

//force input to utf8 
use \ForceUTF8\Encoding; 
echo Encoding::fixUTF8($str).'</br>'; // will output éóùùééééè 
echo Encoding::fixUTF8($str1).'</br>'; // will output éóùùééééè 

$str3 = Encoding::fixUTF8($str); 
$str4 = Encoding::fixUTF8($str1); 


//Then hash 
$hash1 = hash('sha256', $str3); 
$hash1 = hash('sha256', $str4); 

echo $hash1; // will output 45b8151559a5136d58f85ebf51c24f26c47e51f4a89fe2962c8626e99ad64786 
echo $hash2; // will output 45b8151559a5136d58f85ebf51c24f26c47e51f4a89fe2962c8626e99ad64786 

//mb_detect_encoding will ouput always ASCII 
echo mb_detect_encoding($hash1). '</br>'; // will output ASCII 
echo mb_detect_encoding($hash1); //// will output ASCII 

,并在浏览器级别您需要:

<meta charset="UTF-8"> 
+0

谢谢你这个非常完整的演示! –

0

你不应该编码值UTF8两次。我建议只在必要时进行编码。例如:

if (!mb_check_encoding($pf_string, 'UTF-8')) { 
    $pf_string = mb_convert_encoding($pf_string, 'UTF-8'); 
} 
+0

我会尽力谢谢你! –